Cron is a scheduling utility in Linux distributions that can be used to executes tasks at specified intervals. It allows you to run almost any commands or scripts at a specific time and date, or at a recurring interval.
Cron is a very useful tool for a system administrators to automate repetitive tasks of any sort, such as system updates, sending emails, checking system resource usage, creating backups, and so on. Realistically, anything that can be automated can be added to Cron so that it executes without any user involvement.
Cron runs in the background and constantly checks the
/etc/crontab file, and the
/var/spool/cron/ directories. Each user has their own separate crontab file.
In this tutorial, we will show you how to create and run Cron jobs on a Linux VPS. The same steps should work on almost all Linux machines, regardless of whether it’s a server or a desktop computer.
- A Linux VPS with root access enabled, or a user with sudo privileges. Our VPSes all come with root access included for free.
First, you will need to log into your server. If it’s a local computer, all you need to do is log in and open a terminal session as the root user. If the computer is only remotely accessible, or if it’s a VPS, then you’ll need to log in by using SSH. You can do that by entering this command:
ssh root@IP_Address -p Port_Number
Remember to replace “root” with your username if you are not using the root user. Change “IP_Address” and “Port_Number” according to your server’s IP address and SSH port number.
Once you are logged in, you can then proceed with the next step.
The basic syntax of the crontab file is shown below:
Minutes Hours Days Month Dayofweek Username /path-of-command
A brief explanation of each segment of the syntax is shown below:
Minutes: The allowed values for this field are 0–59 and is used to execute jobs at every specified minute.
Hours: The allowed values for this field are 0-23. This segment is used to execute jobs at every specified hour.
Days: The allowed values for this field are 0-31. The days segment is used to execute jobs on a specific day of each month.
Month: The allowed value for this field is 0-12. We can specify a month here and the command will execute at the month specified.
Dayofweek: Allowed values for this field are 0-6. This field is used to specify a day of the week. 0 is Sunday, and 6 is Saturday.
Username: The name of the user.
/path-of-command: The name of the script or command you want to schedule.
You can manage the crontab service with different options.
To create a new crontab or edit an existing crontab, run the following command:
The crontab file will open in your default text editor, where you’ll be able to add, edit, or remove entries from the crontab. Save and close the file, and cron will automatically use the new settings from the crontab file.
To edit a specific user’s crontab, you can run the following command:
crontab -u username -e
Replace username with the name of the user you need.
To display the content of your crontab file, run the following command:
To remove your crontab file, run the following command:
This will prevent cron from running anything from that file on your system for your user.
Restrict Access to the Crontab File
You can restrict access to crontab file using the
For example, to grant access to the crontab file for only the root user, create a new
/etc/cron.d/cron.allow file using your preferred text editor. We’ll be using
nano, but you can use any editor you like:
Then add the following line:
Save and close the file when you are finished. Now the crontab file can only be used by the root user account. You can add additional names on new lines if you’d like them to have access. Note that only the names in this list will have access to the crontab file.
Cron Job Examples
In this section, we will show you how to schedule a task to run at different time intervals.
For example, you can run a test.sh script every 7 minutes of every hour of every day. To do so, add the following line in your crontab file:
*/7 * * * * root /bin/sh /root/test.sh
To run a test.sh script at 03:10 PM from Monday to Thursday, add the following line in your crontab file:
10 15 * * 1-4 root /bin/sh /root/test.sh
Note that the hours field is 24-hour based time, so 3PM would be 15.
We can run the date command on the 5th of February at 08:50 AM. Add the following line in your crontab file:
50 08 05 02 * root date
You can also run a test.sh script every day at 09:00 AM and 11:00 AM. Add the following line in your crontab file:
00 09,11 * * * root /bin/sh /root/test.sh
You can add extra numbers separated by commas to specify additional hours.
To run a test.sh script every day at every hour between the hours of 10 AM and 6 PM, add the following line in your crontab file:
00 10-18 * * * root /bin/sh /root/test.sh
You can also run this test.sh script on the 4th day of every month at 6 AM. You can add the following line in your crontab file for that:
00 06 04 * * root /bin/sh /root/test.sh
If you want to run your test.sh script on the first Monday of each month at 8 AM, add the following line in your crontab file:
00 08 1-7 * 1 root /bin/sh /root/test.sh
To run a test.sh script just once a day, you can add the following line in your crontab file:
@daily root /bin/sh /root/test.sh
And finally, if you want to run your test.sh script once a week, just add the following line in your crontab file:
@weekly root /bin/sh /root/test.sh
For all of the examples involving numbers (e.g. for the day of the month or the hour), you can change the numbers to any value that you need (as long as the value is within the range of acceptable values). You can also combine any of the rules to create your own custom intervals.
Setting up tasks to run automatically is easy if you use one of our Fully Managed Linux VPS services. All you need to do is ask our admins to set up any task to be run at any interval, and they’ll set everything up so that the task is completed exactly when you need it to. Very simple, with no technical knowledge needed. They’re available 24/7 and can help you with almost anything related to server management and support.
If you found this tutorial helpful, feel free to leave a comment down below telling us what you use your Cron jobs for. You can also share this post with your friends by using the share shortcuts on this page. Thank you.