How to Set Up Cron Jobs in Linux
Sooner or later, you might need to use a cron job to schedule various different repetitive tasks in linux in order to automate your process. For example, you might want to run a backup of your database every day at midnight or you might want to run a script that checks for new emails every hour.
In linux, you can use the cron system to schedule a cron job to run at a specific time.
What is cron?
Cron is a scheduling system within Unix-like systems. A daemon called the crond
runs in the background and enables the cron functionality. The cron daemon is responsible for scheduling tasks to run at specific times. This daemon reads your system's crontab
file, or cron table, to read the details of your tasks.
What is cron job?
A cron job is any task that you configured and scheduled. As mentioned before, cron jobs are useful for automating repetitive tasks, workflows, routine tasks, or anything else that you want to automate. If you want to check what users on your machine are already using cron jobs, you can check the same file that crond
uses.
Change your directory to this:
BASH/var/spool/cron/crontabs
BASHls -lrt
BASH-rw------- 1 alan crontab 1167 Oct 29 16:29 alan
That will show you all the users that have configured cron jobs. If you want to check the cron jobs of the user you are currently on, you can do the following:
BASHcrontab -l
How to schedule a cron job
Configuring a cron job is simple and follows a structured syntax. This syntax is straight forward but very powerful, allowing you to define precisely what you want to do and when. Before we get started, ensure that you have the cron service installed and running. To do so, run the following command:
BASHsudo systemctl status cron.service
If it is not running, you can install it using a package manager of your choice.
Here is a list of the most important flags for the crontab
command:
-l
: List all the cron jobs-e
: Edit the cron jobs, this includes adding, deleting, editing, and re-ordering cron jobs-r
: Remove the cron jobs-u username
: This is the flag that will be used to specify the user.
Not that these flags can be combined, for example, you edit another user's cron jobs by running
BASHcrontab -u username -e
Here is an example of a cron job:
BASH# m h dom mon dow command
* * * * * sh script.sh
The first 5 fields are the minute, hour, day of month, month, and day of week, respectively. The last field is the command to run when the cron job is executed.
- Minute: The cron job will be executed at the minute specified. You can use
*
to specify every minute. Valid values are 0-59. - Hour: The cron job will be executed at the hour specified. You can use
*
to specify every hour. Valid values are 0-23. - Day of month: The cron job will be executed at the day of month specified. You can use
*
to specify every day. Valid values are 1-31. - Month: The cron job will be executed at the month specified. You can use
*
to specify every month. Valid values are 1-12. - Weekday: The cron job will be executed at the weekday specified. You can use
*
to specify every weekday. Valid values are 0-6.
The command to run in our example was sh script.sh
. This tells our system to use the bash shell to run the command. The sh
is a shortcut for /bin/sh
. The script.sh
is the name of the script that will be executed. This can be any file that you have created.
Here's a summary of the syntax:
BASH* * * * * sh script.sh
| | | | | └── Command to execute
| | | | └── Day of the Week (0-6)
| | | └── Month (1-12)
| | └── Day (1-31)
| └── Hour (0-23)
└── Minute (0-59)
Cron job examples
Now that we've covered the syntax, let's see some examples of cron jobs configurations.
10 0 * * *
: This cron job will run 10 minutes after midnight every day.30 2 * * 6
: This cron job will run on Sunday at 02:30.0 10 * * 1-5
: This cron job will run every weekday at 10:00.5 * * * *
: This cron job will run every hour on every 5th minute, every day.10 0 * 3 *
: This cron job will run in March at 00:10.
If you want to learn more about this syntax, and create custom ones of your own, we recommend you this handy tool called the crontab guru.
Set up a cron job
Now that you know the syntax, let's set up a cron job. Let's create a cron job simply outputs the current time to a text file. This will help us validate that the cron job is executing as we configured it. Let's create a script called date.sh
.
First create the file:
BASHtouch date.sh
Then add the following code to the file:
BASHecho "The current time is: $(date)" >> date.txt
Now we must change the permissions of the file so that it is executable. Use the chmod
command to do so.
BASHchmod +x date.sh
Finally, use the crontab
command to add the cron job to the cron table.
BASHcrontab -e
Your cron job should look something like this:
BASH*/1 * * * * /bin/sh /root/date.sh
This cron job will run every single minute. This allows us to quickly see results. Make sure that the path is accurate for your system then save the file and exit the crontab editor.
Wait a few minutes, then check the output of our date.txt
file.
BASHcat date.txt
You should see something like this:
BASHThe current time is: Sun Dec 05 16:29:00 EDT 2021
The current time is: Sun Dec 05 16:30:00 EDT 2021
The current time is: Sun Dec 05 16:31:00 EDT 2021
The current time is: Sun Dec 05 16:32:00 EDT 2021
The current time is: Sun Dec 05 16:33:00 EDT 2021
You can confirm this by checking the current system time, which should be the same as the time in the date.txt
file.
BASHdate
BASHSun Dec 05 16:33:00 EDT 2021
Troubleshoot Cron jobs
There might come a time when you need to troubleshoot a cron job. It is easy to misconfigure them or they might fail to run at all.
The first thing you should do is check the syntax of the cron job. Once again, you can use the crontab
command to check the syntax of the cron job and adding the -l
flag.
If that looks good to you, then you can move on to checking the logs of the cron job. This will help you determine if the cron job is running as expected. You can look at the logs in /var/log/syslog
or /var/log/cron.log
. These files will log the time the job was run, the user, and the command executed. The logs will be prefixed with the word CRON
so you can't miss it. Cross-reference these timestamps with your syntax to confirm that it is running at the times specified.
Here's an example log entry:
BASHDec 5 16:35:01 localhost CRON[1719754]: (root) CMD (/bin/sh /root/date.sh)
The last thing you can do to help troubleshoot your cron job is to redirect the output of the command to a file. That way, you can see what the command is doing and whether or not it succeeded. To do so, simply modify the command in the cron job, like so:
BASH*/1 * * * * /bin/sh /root/date.sh &> /root/cron/date.txt
In our example, we are outputting the results of the command to a file called date.txt
. We can then check the file to see if the cron job is working as expected.
Conclusion
As we learned in this tutorial, cron jobs are extremely helpful for running scripts at specific times. They help us automate tasks that we would otherwise have to do manually. With this knowledge, you can now create cron jobs to automate your tasks and make them run at specific times. Thank you for reading this tutorial and happy coding!
- How to Install Node on Windows, macOS and Linux
- Getting Started with Svelte
- How to Serve Static Files with Nginx and Docker
- How to deploy a Deno app using Docker
- Getting Started with Deno
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting Started with Moment.js
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up a Local Web Server using Node.js