stevecline
Posts: 4
Joined: Thu Mar 19, 2020 8:27 pm

Python Script Not Executing From crontab

Thu Mar 26, 2020 2:49 pm

I am trying to set up crontab to run a simple script every day at the same time (i.e. print "Hello World!")

I have modified the sudo crontab file as follows:

Code: Select all

40 8 * * *  sudo python3 /home/pi/Documents/schedule_job.py

The script in the "Documents" directory is simple:

Code: Select all

print("Hello World!")

It runs fine from the terminal and when I check the time using date I am getting a timestamp that should align with the schedule but when the set time arrives, it is not running.

Any suggestions?

pcmanbob
Posts: 8352
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Script Not Executing From crontab

Thu Mar 26, 2020 3:13 pm

Anything you run from cron is run in the background so there is no output to the screen.

If you change your cron line like this you should find the Hello World! is added the file /home/pi/output.txt each time the program is run, which proves your program is running.

Code: Select all

40 8 * * *  python3 /home/pi/Documents/schedule_job.py >> /home/pi/output.txt 2>&1
and you don't need to run your python program with root ( sudo ) privileges
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

stevecline
Posts: 4
Joined: Thu Mar 19, 2020 8:27 pm

Re: Python Script Not Executing From crontab

Thu Mar 26, 2020 3:36 pm

That worked! Thanks!

By the way, what does 2>&1 do at the end?

pcmanbob
Posts: 8352
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Script Not Executing From crontab

Thu Mar 26, 2020 3:44 pm


There are two places programs send output to: Standard output (stdout) and Standard Error (stderr);
You can redirect these outputs to a different place (like a file);
File descriptors are used to identify stdout (1) and stderr (2);
command > output is just a shortcut for command 1> output;
You can use &[FILE_DESCRIPTOR] to reference a file descriptor value;
Using 2>&1 will redirect stderr to whatever value is set to stdout (and 1>&2 will do the opposite).
So in this case we are redirecting both outputs to the file /home/pi/output.txt
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Paeryn
Posts: 2837
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Python Script Not Executing From crontab

Thu Mar 26, 2020 8:05 pm

pcmanbob wrote:
Thu Mar 26, 2020 3:44 pm

There are two places programs send output to: Standard output (stdout) and Standard Error (stderr);
You can redirect these outputs to a different place (like a file);
File descriptors are used to identify stdout (1) and stderr (2);
command > output is just a shortcut for command 1> output;
You can use &[FILE_DESCRIPTOR] to reference a file descriptor value;
Using 2>&1 will redirect stderr to whatever value is set to stdout (and 1>&2 will do the opposite).
So in this case we are redirecting both outputs to the file /home/pi/output.txt
And note that position matters, &1 refers to the current stdout at that moment. If you accidentally put the stderr redirection before the stdout redirection then stderr would be written to the original stdout and not the new stdout.

In the following example stderr will be redirected to whatever stdout was before stdout was redirected to output.txt

Code: Select all

my-program 2>&1 >output.txt  # likely not what you wanted
She who travels light — forgot something.

pcmanbob
Posts: 8352
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Script Not Executing From crontab

Thu Mar 26, 2020 10:34 pm

I have always found that 2>&1 directs any output be it print or echo produced and any error messages to the named file, which is normally exactly what you want when trying to capture output from a Cron job .

Because you directing stderr (2) to stdout (1) which is being directed to the named file or often /dev/null when the output is not actually wanted.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”