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.pyI do also recommend putting a crontab command into systems’
/etc/crontab rather than user’s
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 with
ls /usr/bincommand 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: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 05 18:47:01 server CRON: (root) CMD (root /usr/bin/python3 /home/pythonscripts/scrapper.py)
Jan 05 18:47:01 server CRON: 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.
9 thoughts on “Properly schedule a crontab command for a Python script on any Linux system”
I ran into this page accidentally, surprisingly, this is a amazing blog :-). The site owner has carried out a superb job of putting it together, the info here is really insightful. You just secured yourself a guarenteed reader.
Any guarantees that you’re not a bot?!
LMAO you had a hard way but still do this wrong😄
it doesn’t work otherwise on my VPS. Can you do it better, if yes – let me know how
You forgot use virtual environment. In this case you create executable shell-script and call it from crontab -e without absolute path /bin/sh etc!
I just made it run with the absolute path.