Cron is a powerful tool for scheduling tasks on a UNIX-like system. It allows you to automate tasks such as backups, data processing, and system maintenance. By using crontab files, you can define when and how often tasks should be run.
What is a Crontab?
A crontab is a file that contains a list of cron jobs. Each cron job consists of a scheduling expression and a command to execute. The scheduling expression determines when the job will be run. The command specifies the task that will be performed.
Cron Scheduling Expressions
A cron scheduling expression is a string of five fields that represent the minute, hour, day of the month, month, and day of the week. Each field can take on a range of values, or an asterisk (*) can be used to represent all possible values.
Example Cron Scheduling Expressions
Here are some examples of cron scheduling expressions:
Scheduling Expression | Explanation |
---|---|
* * * * * |
Every minute |
0 * * * * |
Every hour |
30 8 * * * |
Every day at 8:30 |
0 0 1 * * |
Every first day of the month at 0:00 |
0 2 * * 1 |
Every Monday at 2:00 |
45 8 1 1 * |
8:45 on the 1st of January |
0 */2 * * * |
Every two hours |
0 * * * 1,3,5 |
Every hour on Mondays, Wednesdays, and Fridays |
Creating a Cron Job
To create a cron job, simply add a new line to your crontab file. The line should consist of the scheduling expression followed by the command to execute. For example, to create a job that runs every day at 8:30 and prints the current date to a file, you would add the following line to your crontab file:
30 8 * * * echo "Hello world!" >> /tmp/cronjob-output.txt
Saving the Crontab File
Once you have made changes to your crontab file, you must save the file for the changes to take effect. You can do this by pressing Ctrl+O
and then Enter
.
Checking the Output of a Cron Job
To check the output of a cron job, you can view the file that the job writes to. For example, to view the output of the cron job that we created in the previous example, you would type the following command:
cat /tmp/cronjob-output.txt
Additional Cron Scheduling Expressions
Here are a few more examples of cron scheduling expressions:
Scheduling Expression | Explanation |
---|---|
0 8-17 * * * |
Hourly from hour 8 to 17 |
0 8 * * 1 |
Every Monday at 8am |
30 15 * * * |
Daily on a specific time |
Running a Cron Job Multiple Times Per Minute
To make a cron job run multiple times per minute, you need to set up multiple cron jobs scheduled for the same time but with different delays. In this case, to make them run every 30 seconds, the first job will be executed immediately, while the other one waits for 30 seconds:
* * * * * echo "First execution this minute"
* * * * * (sleep 30; echo "Second execution this minute")
Running a Cron Job on the First Day of the Month
To run a cron job on the first day of the month, you can use the following scheduling expression:
0 0 1 * *
Accessing Other User’s Crontabs
In case you need to access a crontab of another user, you can do so by executing the crontab command as another user using the sudo command:
sudo crontab -e -u www-data
In this case, we can open the crontab of the www-data user. It is also possible to simply switch to the user using the su command, however this method does not work with disabled users or users without shell access.
su [Username]
crontab -e
Conclusion
Cron is a powerful and flexible tool that can be used to automate a wide variety of tasks. By learning how to use cron scheduling expressions, you can schedule your tasks