Crontab Failed to Run Python Script at Reboot

crontab failed to run python script at reboot

Your program creates the file in the current working directory. cron jobs run in the invoking user's home directory; thus your cron job writes the file in the home directory of root (probaby /root on Debian-based platforms).

Once you create a file as root, it is only writable by root (unless you specifically set permissions to make it world-writable, or assign write access to a specific user group)

Probably change your script to write to /home/pi/log.txt (if that's where you want the file) and make sure the file already exists, or maybe switch to the pi user before creating it if you are running as root. (Once the file exists with the correct owner and permissions, root can append to it without changing the owner or permissions.)

Tangentially, there is no need to use sudo in a cron Job which is already running with full root privileges.

Crontab fails to execute Python script

The main.py script calls some methods from other modules under python_prj, does that matter?

Yes, it does. All modules need to be findable at run time. You can accomplish this in several ways, but the most appropriate might be to set the PYTHONPATH variable in your crontab.

You might also want to set the MAILTO variable in crontab so you get emails with any tracebacks.

[update] here is the top of my crontab:

www:~# crontab -l

DJANGO_SETTINGS_MODULE=djangocron.settings
PATH=...
PYTHONPATH=/home/django
MAILTO="cron-notices@example.com"
...
# m h dom mon dow command
10-50/10 * * * * /home/django/cleanup_actions.py
...

(running cleanup actions every 10 minutes, except at the top of the hour).

Crontab not executing a Python script?

What happens when you type

/home/me/project/myscript.py into the shell?

Can you explicitly use /usr/bin/python in your crontbb command?

Can you either use an absolute path to your test.db or cd to the correct directory then execute your python script?

This is helpful to have debug statements in your python and log some data. Crontab can be very tricky to debug.

Unable to run python script with shebang from /usr/local/bin

From the error:

bash: ./usr/local/bin/check_bios.py: No such file or directory

the cron job was looking for the check_bios.py script in the ./usr/local/bin path.

From that, I guessed that the cron job was:

? ? ? ? ? ./usr/local/bin/check_bios.py

where ? represents the time values (deliberately left them as ? since they weren't mentioned in the post, nor are they relevant to the issue).

I believe it should be:

? ? ? ? ? . /usr/local/bin/check_bios.py


Related Topics



Leave a reply



Submit