February 4, 2019
February 4, 2019
Are you regularly doing some tasks in Linux, like mailing disk usage to yourself, archiving logs, deleting temp directories, etc.? In today’s world time is precious, why waste it on tasks which could be scheduled to run on a recurring basis? Cron is here to help! Cron is a time-based job scheduler, system administrators use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. This tutorial will show how to setup Cron Job on Linux VPS.
Before you begin this guide you’ll need the following:
Cron service uses Crontab (Cron table) file which is usually located in
/var/spool/cron/ directories. Each line in the crontab represents a scheduled job. Just run this command in your shell to list all cron jobs:
If the file is empty, do not worry, it seems that you do not have any scheduled jobs. An example of cron job would be like:
0 1 * * * /usr/local/cpanel/scripts/cpbackup
In this sample, a cPanel backup script is executed every hour. As you see, the actual job line consists of six fields:
┌───────────── min (0 - 59) │ ┌────────────── hour (0 - 23) │ │ ┌─────────────── day of month (1 - 31) │ │ │ ┌──────────────── month (1 - 12) │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to │ │ │ │ │ Saturday, or use names; 7 is also Sunday) │ │ │ │ │ │ │ │ │ │ 0 1 * * * /usr/local/cpanel/scripts/cpbackup
If you still have doubts, check out manual pages of Cron by executing
man cron on your console.
Edit the crontab by running following command:
IMPORTANT! Do not ever edit the crontab file directly, always use the
crontab -e command.
0 0 1 * *– once a month on the first day
0 0 * * 0– once a week
0 0 * * *– once a day at midnight
20 22 3 * *– every 3rd day of each month at 22:20
*/30 * * * *– every 30 minutes
0 8 * * 1,2,3,4,5from Monday to Friday at 08:00
Following the schedule do not forget to write the actual path to your script. You can generate any timetable you want via http://crontab-generator.org
Once you are finished, close the crontab by writing this command in the editor:
From now on you have the knowledge how to setup cron jobs in order to automate scripts or commands on Linux. You can specify an exact time, date or just reoccurrence.