Launching a python script automatically at a pre-defined time is very easy on raspberry pi using the corn tab. Just open a terminal and get into
crontab -e
If it's first time you will be asked to select an editor, select nano. Move to the bottom of the comment lines and add the time, date and code you want to execute in the following format
More info: https://www.raspberrypi.org/documentation/linux/usage/cron.md
Here I have set the corn to launch my script called script_name.py at 3:30PM every day. The comment line above shows the format in which we can specify the time and date Since i need it every day and every month I have used * for the other three values. After editing write out by ctrl+O and then exit by ctrl+X. Now it might seem very easy but I faced a lot of catches, I am sharing the same with you
0. Difference between "crontab -e" and "sudo crontab -e"
Edit only the crontab -e file do not edit the sudo corntab -e. Sudo edits the root crontab which I found to be unstable and does not work well when another user is logged in on GUI. Remember that every user on your pi will have a crontab, so make sure you are editing the one in which you are logged in. Also Linux security101 do not use sudo if there is no need of it.
1. View the output of code executed in corn
The python script executed here runs in the background so we will not be able to see the results without logging them into file. To log the output of your code (print statements) into a file use the following line
30 15 * * * python3 /home/pi/script_name.py >> output.log 2>&1
This will log the print values from the code into a file called output.log located in the same directory as your python script. The command 2>&1 will also log the error into the file
2. Testing the python code on corn
During testing, it might be tedious to change the time at which the script should launch every single time. So for quick de-bugging, you can set the corn to launch the command every new minute by
* * * * * python3 /home/pi/script_name.py >> output.log 2>&1
This will launch the script every minute
3. Execute multiple lines on corn in Pi
Sometimes we might need to execute multiple commands at a specific time to do that
0 8 * * * cd /home/pi; python /home/pi/test.py >> output.log 2>&1
Here at 8:00AM two commands "cd /home/pi" and "python /home/pi/test.py>>output.log 2>&1" will be executed. It is recommended to create a separate bash file if you have multiple commands to be executed.
4. Playing music or accessing external files while executing the python script from corn
Since corn command is not run from the root directory it might be a problem to access the external files in the same directory that your program might need. to do that also get into that particular directory and then launch your python code as shown above.
Playing audio needs an additional line XDG_RUNTIME_DIR="/run/user/1000" to be placed in the corn file. I made my PI_IVR code to execute automatically like below
5. Shutting down or restarting pi on time using corntab
Apart from launching scripts, it is also possible to shut down or restarts your pi automatically using the corn tab like below
To shut down your pi automatically
0 21 * * * sudo shutdown -h now
To restart your pi automatically
0 21 * * * sudo shutdown -r now
Hope this thread will help you work with corn easily on PI. If you have any problems with PI post them here
More troubleshooting help: https://garyhall.org.uk/troubleshooting-cron-raspberry-pi.html#:~:text=….