schedule python code with crontab

Properly schedule a crontab command for a Python script on any Linux system

by

in

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

  1. Wrong username error under systemctl status cron
    Never forget to mention your username under cron command:
    * * * * * root python /home/pythonscripts/scrapper.py
  2. Path to my Python executive program
    * * * * * root /usr/bin/python3 /home/pythonscripts/scrapper.py
    Just check out your /user/bin/ folder with ls /usr/bin command and find which python versions do you have, mine:
crontab command for a python script, Properly  schedule a crontab command for a Python script  on any Linux system
ls /usr/bin command result


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.


Comments

9 responses to “Properly schedule a crontab command for a Python script on any Linux system”

  1. […] need to visit those websites and spend my precious time. Recently I wrote a post about how you can properly schedule a cron job on the Ubuntu server, please take a look before we go to the next […]

  2. […] Learn more on how you can properly set up a crontab job with Ubuntu VPS […]

  3. 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.

    1. Any guarantees that you’re not a bot?!

  4. […] out how you can easily schedule the python code to run on Ubuntu or any other Linux distros. If you want to schedule the “Automatic WordPress analytics […]

  5. LMAO you had a hard way but still do this wrong😄

    1. it doesn’t work otherwise on my VPS. Can you do it better, if yes – let me know how

      1. 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!

        1. I just made it run with the absolute path.

Leave a Reply

Your email address will not be published. Required fields are marked *