Crontab won't start my Python script

Posted on 2013-09-13
Medium Priority
Last Modified: 2016-08-13
I set up a script to run in Crontab on my raspberry pi. When the pi boots the script won't start. If I run the script from the terminal like this:


it runs great. I need to run in Crontab as a background.

Any ideas?
Question by:dynamicOne
  • 2
LVL 17

Expert Comment

ID: 39491813
Without showing your crontab entry it is hard to know.

Some things to check:
1. When something runs from crontab it does not have the same environment as when you run from your user shell.  Check that there are no environment variables that are required to be set up prior to it running from cron.  You may need another wrapper script.
2. Your path will most likely be different to the one being used by Cron.  Check the paths are fully described in your python script.
3. Is your python environment available from cron.  Do you do some setup in your profile configuration that is not being replicated from cron.

Author Comment

ID: 39491886
Sorry about that, I thought I put my entry in there.

sudo crontab -e

@reboot python /var/www/garagestatus2.py &
LVL 17

Expert Comment

ID: 39492056
Put the full path to the python command on the crontab line.
LVL 38

Accepted Solution

Gerwin Jansen, EE MVE earned 2000 total points
ID: 39493082
Your crontab entry is not complete and you don't need the & btw.

Entry should look like this:
0 8 * * * <full path to>/python /var/www/garagestatus2.py

Open in new window

This would start your .py script every day at 8am. In case your script generates output, you should 'catch' the output like this:
0 8 * * * </full path to>/python /var/www/garagestatus2.py >> </full path to logging folder>/logfile

Open in new window

for example:
0 8 * * * /usr/bin/python /var/www/garagestatus2.py >> /home/someuser/garagestatus2.log

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question