Cron is a job scheduling utility present in Unix like systems. The crond daemon enables cron functionality and runs in background. The cron reads the crontab (cron tables) for running predefined scripts.
The script to automatize
This is a simple script only made for the purpose of this example: you can replace it by watever you want. This script basically appends the current date and time to a text file.
# script.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
from os.path import exists
from datetime import datetime
FILE = "{absolute_path_to_file}/output.txt"
def writedate():
line = 1
if exists(FILE):
file = open(FILE, "r")
lines = file.readlines()
line = len(lines) + 1
file = open(FILE, "a")
date = str(line) + ': Cronjob executed on ' + str(datetime.now()) + '\n'
file.write(date)
file.close()
if __name__ == '__main__':
writedate()
The line #!/usr/bin/env python3
is for telling the system that python3 is the python compiler to be used for this script. The next comment line set the characters encoding.
Add a new cronjob for this script
Edit the crontab file located in /etc
folder:
$ cd /etc
/etc$ sudo nano crontab -e
In Nano, save the file with Ctrl+O
and close it with Ctrl+X
Add the following line at the end of the file to run the script every minute:
*/1 * * * * username asolute/path/to/script/script.py
Where username
is your username on this linux system. The path to the script (asolute/path/to/script
) must be absolute or the cron job will fail. To remove the CRON job, simply remove this line from the file.
Get the CRON logs to verify that everything is working fine:
$ systemctl status cron.service
If your script contains errors, the logs list is more likely to contain this error: (CRON) info (No MTA installed, discarding output)
Tip: you can check the actual time (with seconds) on Linux with this simple command: date
The output file should look like this:
1: Cronjob executed on 09/13/2022, 22:17:01
2: Cronjob executed on 09/13/2022, 22:18:01
3: Cronjob executed on 09/13/2022, 22:19:01
4: Cronjob executed on 09/13/2022, 22:20:01
5: Cronjob executed on 09/13/2022, 22:21:01
6: Cronjob executed on 09/13/2022, 22:22:01
...
Cron syntax
Below is the summary of the cron job syntax.
* * * * * username absolute/path/to/script/script.py
_ _ _ _ _ ________ _________________________________
| | | | | | |
| | | | | username of the Command or Script
| | | | | user on this to Execute
| | | | | linux system
| | | | |
| | | | Day of the Week(0-6)
| | | |
| | | Month of the Year(1-12)
| | |
| | Day of the Month(1-31)
| |
| Hour(0-23)
|
Min(0-59)
Examples:
*/1 * * * *
: Every minute* */3 * * *
: Every 3 hours5 0 * 8 *
: At 00:05 in August.5 4 * * 6
: At 04:05 on Sunday.0 22 * * 1-5
: At 22:00 on every day-of-week from Monday through Friday.
Other usefull commands
crontab -l
: list all the cron jobs for the current user.crontab -u username -l
: list another user's crons.crontab -u username -e
: edit another user's crons.
Deploy a Cron Job online
We recommand to use Heroku to deploy your Python cron job script online.
- For a deployment on Heroku, follow this tutorial
- For a deployment on Cloudways, follow this one
Good post, very clear Thanks