Doing repetitive tasks is tedious and boring. It is the feeling that we all have experienced but but it doesn’t have to be that way. One such popular way of automatic scheduling repetitive tasks, is using Cron jobs. This article is all about set up cron job in linux and scheduling the cron jobs to run the scripts automatically.
You may have this task, like –
- taking the database backup everyday, or
- sending a reminder emails to customers at a specific time of the day, or
- clean up the log files or junk files at the end of the day.
All these things are not exciting if we do it manually each and every day. Writing a small script and executing it everyday is quite possible with cron jobs.
What is a Cron job?
Cron is a powerful feature of unix-based operating system to automate and schedule tthe repetitive tasks. With a little bit of configuration, you can get a cron job up and running automating your manual tasks that helps you frees up frrom this work so that we all can focus on the more important things.
We can define N number of cron jobs and they are all defined in a crontab files and controlled by cron daemon.
Before we proceed on how do we set up a cron job in linux, let’s understand the syntax of cron job.
Cron job syntax
Crontab syntax typically consists of five important parts. As depicted in this picture below, we need to specify the time, date or week you need the cron job to be scheduled. All these five parts are separated by spaces.
- minute: Define the minute when we want the task to run. It allows 0 – 59 values
- hour: The second one denotes the hours at which this tasks has to be scheduled. Allowed values are between 0 to 23
- day of the month: Specify the day of the month (1-31)
- month: This field is used to define the month of the job schedule. Typically ranges between 1 to 12.
- day of the week: You can specify the day of the week with this parameter. We can feed in 0 to 7 values for this.
And finally, we can define the script to run at these time intervals.
Here is the example script that takes the backup of the database at 12:30 PM on the first day of every month –
30 12 1 * * python3 database_backup_script.py
How to Set up cron job in Linux?
Now we know how does a cron syntax looks like, let’s go ahead and set-up a cron job in linux. Here is the step-wise guide –
- Open Terminal on your Linux operating system.
- Run crontab -e command to open the crontab file.
- Paste your cron job syntax and the script (something like below) to the file
- Now save the file and close it.
0 4 * * * python3 /root/trending_app_scrapper.py >> /tmp/trending_apps_scrapper.log 2>&1
Here we are taking the output to trending_apps_scrapper.log file. You can view the log file to verify if the script is running properly as intended to run.
Once the cron job is saved, daemon reads the file and executes the tasks as defined in the specified intervals. It also continuously monitors if there are any changes to the cron job in the background.
How to View all the scheduled Cron jobs?
Once you have set up the cron job, you can verify it by running the command below in the terminal –
This should list down all the cron jobs you have configured.
Note that this will only list down the jobs. You cannot edit the job from here. You need to use the crontab -e command again to modify the job.
Things to keep in mind while configuring Cron jobs
- Check the file location: While configuring the cron job, give the absolute path to the scripts and log files. It’s important to give the full path to the scripts since PATH variable is not set the same way as user’s login path.
- Test the script once before configuring: Run the script manually to the terminal to ensure it’s working as expected. You can take the command and run to check if there are any syntax errors or permission issues.
- Use online tools like crontab.guru to get the correct syntax. It helps you to validate the syntax in a readable format.
- Make sure your scripts has proper permissions to execute the commands
- It’s always a good idea to store the output to a log file. This way, if any error occurs, it would be easy to troubleshoot the problem and resolve it.
- If any of your cron job fails due to any error, it’s good to have an alert system and re-try mechanism in place so so that it will not impact the business logic.
So that’s all from our end. You can follow the same steps to schedule a cron job in any of the linux distributions like Ubuntu, Kbuntu etc.
Say good bye to repetitive tasks with scheduling and cron jobs!!