There are a lot of articles over the internet that exist about the Python Cron schedule. Most of them are the working solutions and I’ve been setting up cron jobs for decades now, but unsuspectedly I’ve got a bug with my beloved sudo chrontab -e
command and this is the reason I’ve written this article.
If you can’t schedule your cronjob over crontab-e
– read this until the end and your cronjob will “rise from the ashes”, I guarantee you. If it’s not – you’re welcome to leave the comment below and I will try to help you personally and don’t forget about bro_dev_@twitter
The right structure of the crontab command for a python script on Linux:
* * * * * user /path/to/python/executive/program/ /path/to/python/script.py
I do also recommend putting a crontab command into systems’ /etc/crontab
rather than user’s crontab -e
My problem with the “crontab -e” command
I have been working as a root
user on my Ubuntu 20.04 LTS system. As you may know, to set up a cron job user should send this command:sudo crontab -e
and fill the command in the opened window, in my case(the wrong command, please don’t use it):* * * * * python /home/pythonscripts/scrapper.py
The previous code set up the cron job to work every minute(I’ve done it only for testing purposes).
Crontab -e Errors
- Wrong username error under
systemctl status cron
Never forget to mention your username under cron command:* * * * * root python /home/pythonscripts/scrapper.py
- Path to my Python executive program
* * * * * root /usr/bin/python3 /home/pythonscripts/scrapper.py
Just check out your /user/bin/ folder withls /usr/bin
command and find which python versions do you have, mine:
In my case, when I do python *.py
from the console – the code executes within Python2, but while my code was written for the Python3 version.
After all those manipulations I’ve got these in my systemctl status cron
:
Jan 05 18:47:01 server CRON[161807]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 05 18:47:01 server CRON[161816]: (root) CMD (root /usr/bin/python3 /home/pythonscripts/scrapper.py)
Jan 05 18:47:01 server CRON[161807]: pam_unix(cron:session): session closed for user root
Cron opened the session for my command and closed it the same second. I’ve got a 10 seconds timeout inside my scrapper.py and of course, my code did not execute, it did not run, not even once.
The only possible solution that I’ve found: switch from user’s cron to system’s cron. I’ve pasted my cron command into /usr/crontab/
and voila – everything works!
Possible Linux errors with cron scheduling
In my case: I have forgotten to make the python script available for execution and cron was unable to run it.
Never forget to sudo chmod +x script.py
before putting it to the cron.
This article is part of a Workaround paragraph from the “Automate everything with Python” article, take a look if you did not saw it yet. I will explain how to save 5 hours per month by using Python automation possibilities.
If you guys have any trouble with configuring asterisks for your cron job, visit crontab.guru interactive website, it has all the explanations about time in cron commands.
if your cron doesn’t work within your OS – feel free to drop a comment and don’t forget to mention your OS and error – I will be happy to help you, as much as I can and together we will be able to properly schedule your crontab command for a python script.
Leave a Reply