Python Forum
Python Script Spawned by Cron or Systemd doesn't write files..?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Script Spawned by Cron or Systemd doesn't write files..?
#1
Hi all,

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?

My script:
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(30)
The camera class:
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)
My crontab:
@reboot python3 /var/www/camera/main.py > /var/log/cron/crontab.log 2>&1
Reply
#2
Dance I figured it out, I never specified a working directory. When I setup the application as a service, there is a "WorkingDirectory" assignment that I was missing. Once specified, it's working as it should.

[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
Reply
#3
Please mark as solved (this helps moderators when scanning so many queries)
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  ModuleNotFoundError only if script is launched by cron Charles33 8 5,078 Jan-12-2024, 12:55 AM
Last Post: caicaifan
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,003 Jun-29-2023, 11:57 AM
Last Post: gologica
Question Need help for a python script to extract information from a list of files lephunghien 6 1,032 Jun-12-2023, 05:40 PM
Last Post: snippsat
  script to calculate data in csv-files ledgreve 0 1,055 May-19-2023, 07:24 AM
Last Post: ledgreve
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 894 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Is this possible to write a script for checking data from website? WanW 2 1,081 Jun-02-2022, 02:31 AM
Last Post: Larz60+
  Is it possible to write a python script to block twitter feeds? cubangt 0 837 Apr-07-2022, 04:14 PM
Last Post: cubangt
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,670 Jan-08-2022, 12:13 PM
Last Post: law
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,636 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020