Have you ever regularly performed some tasks in Linux, such as archiving logs, deleting temp directories and so on?
Well if so, has it ever crossed your mind that is there a way to make these tasks done automatically?
Yes, of course, you can use Cron Job. In this article, we will show you how to set up a Cron Job on a Linux VPS.
Cron is a Linux utility that schedules commands or scripts on your server to run automatically at a specified time and date. A cron job is a scheduled task in itself. Cron jobs can be very useful for automating repetitive tasks.
For example, you can set up a cron job to delete temporary files every week to save your disk space.
Scripts that run as cron jobs are usually used to modify files or databases. However, it can also perform other tasks that do not change data on the server, such as sending email notifications.
Cron Job Elements
Most cron jobs include three components:
- A script that will be called or run.
- A command that executes the script repeatedly.
- Action or Output of the script (depending on what the script does).
How to Write Cron Syntax
The correct way to write cron is important to know so that you can schedule commands as desired. This crontab file has 5 fields.
Time Interval
This field is marked with (*) asterisks. Each asterisk indicates a time setting, when the task is run repeatedly.
Cron jobs can be set to run based on minutes, hours, days of the week, days of the month, months, or a combination of all.
Check out the image below for more details.
- Minute: This field determines what minute the task will run. The number range is from 0 to 59.
- Hour: This field determines at what time the task will be run. The numbers range from 0 to 23.
- Day of the month (Day): This field specifies on which day the task will be executed. The number starts from the 1st to the 31st.
- Month: This field specifies which month your command will be executed. The number is from month 1 to 12.
- Day of the week : This field specifies on which day of the week the task will be executed. The number is from day 0 to 7.
Syntax and Operators
But besides the time interval, you also need to know the characters that exist so that you can use them appropriately when creating commands.
Star Sign (*): Setting the schedule and time the command is executed.
Comma (,): Run commands at >2 different times.
Hyphen (-): This allows you to specify a range of values.
Slash (/) : Allows you to specify a value that will be repeated during a specific interval.
Cron Syntax Example
By now you understand how to write cron syntax, right? Now in this section we will provide some example commands so that you understand better. Please refer to some of the commands below.
0 5,17 * * * /bin/sh backup.sh
The command is to run database backups at 2 times or 2 times a day. 5am and 5pm.
0 */5 * * * /scripts/monitor.sh
The order is to monitor every 5 hours.
*/20 * * * * /home/user/script.sh
The above command runs a cron job in the home directory every 20 minutes.
0 0 * * 3 * /bin/sh
The above command is to run a database backup every Wednesday at midnight.
How to Set up Cron Job on VPS
Before you set up a Cron Job, you need SSH access to the VPS. Please log in first.
Enter the Cron Directory
The Cron service uses the Crontab file which is usually located at
- /etc/crontab,
- /var/cron/tabs,
- /var/spool/cron/.
Each line in crontab represents a scheduled job. To open your cron job file and view its contents, please run the command below in your shell:
crontab -l
If it's empty, don't worry. You probably don't have any scheduled tasks. If you do have a cron job, it will look like the image below:
Set up Cron Job
Now if you want to schedule tasks to be performed automatically, you can edit crontab by running the following command:
crontab -e
Then you add or enter the command you want to run along with the schedule. After that Save. You can check the Crontab Generator to make it easier to set up the schedule.
Conclusion
So that was the discussion about Cron jobs and how to set up cron jobs to automate scripts or commands on a Linux VPS. Thank you for following until the end, hopefully this article can be useful.