Python Script Spawned by Cron or Systemd doesn't write files..?

JohnnyRobot

New Member
Joined
May 24, 2019
Messages
4
Reaction score
2
Credits
0
Hi all,

I'm not really sure where this question should go. I'm not even sure if it's a Linux issue or a Python issue.

I've written a Raspberry Pi application (Ubuntu Mate) that's essentially a timed camera. Every 30 seconds, it snaps an image and writes it to disk.

When I start the application manually from the terminal, it works as expected.

When I configure the script to execute automatically at startup, (using crontab or systemd - It needs to be executed as root) the script runs without any error, but no images are written to the disk.

Any idea of what may be going on?

*** I'll be providing more info shortly
 


More info as mentioned...

My crontab:
Code:
@reboot python3 /var/www/camera/main.py > /var/log/cron/crontab.log 2>&1

My python script:
Code:
import time
from camera import *
import cv2

count = 0

while True:
    # Open camera
    Camera01.GiGe.Open()

    # Turn on Light
    Camera01.Light.fill((255, 255, 255, 255))

    # Snap Image
    image = Camera01.GiGe.GrabOne(1000)

    # Convert image to OpenCV Array
    image = cv2.cvtColor(image.Array, cv2.COLOR_BAYER_BG2BGR)

    # Save image to disk
    cv2.imwrite('saved_images/image'+str(count)+'.png', image)

    # Turn off Light
    Camera01.Light.fill((0, 0, 0, 0))

    # Close camera
    Camera01.GiGe.Close()

    # Increment counter
    count = count + 1

    time.sleep(5)

Supplement (Camera class)
Code:
import neopixel
import board
from pypylon import pylon

class Camera01:
    GiGe = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
    Light = neopixel.NeoPixel(board.D18, 16, bpp=4, auto_write=True)
 
I studied Python a bit last year but haven't looked at it since then, so I probably can't help much with the code, but for anyone to truly be able to help you with the code, you'd need to post it so we could look at it. I do a lot of coding at work in VBScript and VBA (don't hate me for it, the apps need to run on workstations at work). One thing that I do to help figure out where things are going awry is to add lines to print something to the screen (here at line x). And that way I can figure out where it's broken. Since you have it running fine manually, the print to screen might be a bit tough, but you could create a log file and have it print to it instead. So on load write "starting" to /camera files/mylog (this should really be in the same directory as you want your camera files to go to), then before it takes the first picture write "preparing to snap a pic", then after the pic is done write "preparing to copy to disk", then after putting it in the directory write "finished". Putting the log in the same directory as the photo destination will ensure that there are no permissions issues. No log, must be permissions. If the log gets created but doesn't have each line in, you'll see where it failed.
 
when you post your code, use the + link in the reply toolbar and use Code, that way we won't see the code unless we want to look at it. That way the entire page won't be filled with code except when we expand it.
 
when you post your code, use the + link in the reply toolbar and use Code, that way we won't see the code unless we want to look at it. That way the entire page won't be filled with code except when we expand it.

Yeah, I reproduced the issue with a smaller application that's easier for others to follow. I posted that code about an hour ago, it's still awaiting moderator approval.
 
Looking at your code, I think the issue might be that the system doesn't know where to store the images. I don't see anywhere, where it states the location of saved_images. The line makes it look like it's in the current directory but I don't see where you've stated where the current directory is located (cv2.imwrite('saved_images/image'+str(count)+'.png', image)). When you run it manually, you're probably in the directory of the script, but when you use Cron, you're not, unless I'm mistaken. I haven't scripted much in Linux or used Cron.
 
I figured it out, it was along the lines of what you were saying. I setup the application as a service, there is a "WorkingDirectory" assignment that I was missing. Once specified, it's working as it should.

Code:
[Unit]
Description=Time lapse camera application

[Service]
WorkingDirectory=/var/www/camera/
User=root
ExecStart=/usr/bin/python3 /var/www/camera/main.py
Restart=always

[Install]
WantedBy=multi-user.target

Thank you!
 


Top