Member-only story
Scheduling Python Scripts on Linux
Automate your tasks easily with cron
Scheduling scripts can be an absolute necessity for a huge number of tasks. From data collection for AI to weekly blog newsletters, schedules are absolutely everywhere.
A significant portion of schedulable jobs is hosted on Linux machines and operated by Python scripts. Today, we will take a look at how we can automate script schedules using cron in Linux.
Note: The examples used here are specific to Fedora distributions (specifically CentOS). However, the process (minus the cron installation) is essentially the same for most other popular distributions as far as I am aware.
If something isn’t the same, let me know in the comments and I can update the article for others. Thanks!
Cron Installation
It is very likely that you already have cron installed on your Linux machine, but to check, we can open the terminal and type:
crontab -l
This will return a list of processes currently controlled by cron. Don’t worry if you don’t see anything. This just means you have no cron processes defined!
However, if this returned something like:
You need to install cron using the commands given in the table below:
Here, the installation command will vary depending on your distribution. I cannot confirm whether all of these will work 100% without testing them myself. Please let me know if you find anything different from what is shown here.
The Python Part
This part is mostly up to you, but we will use a simple script that will print the time for our example. We can create and edit the file with vim /home/test/datetimer.py
:
from datetime import datetimeprint(datetime.now())
The script is saved as /home/test/datetimer.py
and uses the user’s installation of…