How to automate a script with cron jobs?
Automation means a process or a system that operates by itself with negligible input by the user using it. In today’s world, automation is a necessity. Manual work is extensive and slow. Even simple tasks such as pulling updates from a repository often slow down the developer. If not done at the right time, it could cost a lot and create hassles such as merge conflicts and similar issues. For simple tasks that are done repetitively, automation comes into play and reduces issues produced by these minor tasks and lets the user focus on the main tasks at hand. In this article, we will be discussing how to automate simple tasks that are time-dependent using a famous software known as Cron jobs.
What is Cron?
Cron is a CLI (command line interface) tool that is based on Unix-like operating systems. There are some common questions that confuse the developers trying to start using cron. Some of them are:
What is the difference between Cron and Cronjob?
What is Crontab? Is it the same as Cron?
And many more…
Cron vs Crontab vs Cronjob
- Cron is a software tool that runs continuously in the background and only wakes up at specific times to perform a scheduled task.
- Basically, a crontab (CRON TABle) is a file that contains the list of scheduled tasks known as CRON entries or cron jobs that are to be run at specified times.
- A cron job is an execution instruction that specifies the date and time with the task to run at the time.
Simply put, cron is a software tool that runs a task or an instruction command called cron job which is stored in a file known as crontab.
Installing Cron
For windows, cron is not available. However, a well-known job scheduler is already present inside windows known as ‘Scheduled Tasks’. Use the wizard inside the application to create scheduled tasks.
In both Linux and WSL2, use the following commands:
sudo apt update
sudo apt install cron
Code language: Bash (bash)
Setting Up Cron
Syntax for writing cron jobs
Cron has a special syntax in order to execute the commands at the specified time. It consists of two main sections. The first section consist of five parts describing the time period of the cron job. The next section contains the command to run for the cron job.
* * * * * [command to be executed]
| | | | |
| | | | day of the week [0-6]
| | | month [1-12]
| | day of the month [1-31]
| hours [0-23]
minutes [0-59]
Code language: Bash (bash)
For help in writing the schedule for the cron job, to practice writing in cron, or to generate a cron job right away, visit: CrontabGuru @ https://crontab.guru/ .
Creating a cron job
To set up cron jobs, use the following commands:
# Edit the crontab file to add cron jobs
crontab -e
# List all the cron jobs inside crontab file
crontab -l
# List all cron jobs of another user
crontab -l -u [username]
# Edit the cron jobs of another user
crontab -e -u [username]
Code language: Bash (bash)
Note: The star ‘*’ symbol denotes all possible values.
Written below is a cron job for add a date inside a file at a daily basis. Create a script named date-script.sh and add the following lines below:
#!/bin/sh
echo `date` >> date-sheet.txt
Code language: Bash (bash)
Additionally, insert a cron job to fire up this script daily at 12 midnight:
0 0 * * * /bin/sh ~/date-script.sh
Code language: Bash (bash)
This cron job is added to the crontab using “crontab -e”. Afterwards, the cron daemon is started to schedule the tasks received.
Note that the script written for execution should be having ‘execute’ rights inside that folder for the user. The user must be having the correct permissions, otherwise the tasks would not be performed. You may use this command to check the execution rights for that script:
ls -lrt
Code language: Bash (bash)
This command will show all the files and folders present along with their permissions for that user.
Starting Cron
First let’s check whether cron is running, using:
sudo systemctl status cron.service
Code language: Bash (bash)
In WSL2, there some issues may arise similar to this one:
So, for WSL2 users, use:
sudo service cron status
Code language: Bash (bash)
If the cron is not running, then start the cron using these commands:
sudo systemctl start crond.service
# OR
sudo service cron start
Code language: Bash (bash)
Note: Restart the cron software as to reflect the cron jobs into the cron if the cron daemon was running when crontab was edited.
sudo systemctl restart crond.service
# OR
sudo service cron restart
Code language: Bash (bash)
To store the output of the command result, edit the cron job like this:
Limitations of Cron
- After editing the crontab, the cron daemon must be restarted. This can lead to negligencies which can cause whole tasks completely left out.
- If due for any reason the cron is shut down, there is no alert, if not manually by other sources manually, provided by the cron. This could lead to tasks being left suspended without them being noticed.
- Although it is obvious but to be loud and clear, the cron tasks scheduled can only run when the computer is kept turned on.
- Cron daemon takes up a little bit of RAM and also consumes electricity due to a task running continuously in the background.
- Cron only performs time-bound tasks and hence no event-bound tasks can be performed by it. Cron can not be used to work along with any script, it only runs other scripts on particular time periods.
Conclusion
Cron is a very powerful tool and very light indeed. It does not require too much knowledge in cron to set up a cron job and start scheduling the tasks. For users with only tiny but repetitive non-important tasks, Cron is the perfect tool to use as a Job Scheduler.
Sharing is caring
Did you like what Aman Ahmed Siddiqui wrote? Thank them for their work by sharing it on social media.