<\/span><\/h2>\nBefore we begin with setting up crontab in Linux, let’s connect to your VPS via SSH and update all your system software to the latest version available.<\/p>\n
On Ubuntu system, you can run the following command:<\/p>\n
apt-get update && apt-get upgrade<\/pre>\nand on CentOS you can run:<\/p>\n
yum update<\/pre>\n<\/span>2. Check if cron is installed<\/span><\/h2>\nIn order to use the cron utility, you need to make sure that the cron package is installed on your server. On Ubuntu server, you can run the following command:<\/p>\n
dpkg -l cron<\/pre>\nOn CentOS you can check with:<\/p>\n
rpm -q cronie<\/pre>\n<\/span>3. Install the cron package<\/span><\/h2>\nIf the cron package is not installed on your server then you can install it with the package manager:<\/p>\n
On Ubuntu:<\/p>\n
apt-get install cron<\/p>\n
On CentOS:<\/p>\n
yum install cronie<\/pre>\n<\/span>4.Verify if the cron service is running<\/span><\/h2>\nTo check whether the cron service is running on your system, you can use the following command:<\/p>\n
On Ubuntu:<\/p>\n
systemctl status cron<\/pre>\nOn CentOS:<\/p>\n
systemctl status crond<\/pre>\n<\/span>5. Configure cron jobs<\/span><\/h2>\nIn order to set up cron jobs, you need to modify the \/etc\/crontab file. Please note that this file can only be modified by the root user.
\nYou can edit the crontab file with your favorite text editor, for example:<\/p>\n
nano \/etc\/crontab<\/pre>\nThe content of this file usually looks like this:<\/p>\n
SHELL=\/bin\/bash\r\nPATH=\/sbin:\/bin:\/usr\/sbin:\/usr\/bin\r\nMAILTO=root\r\n\r\n# For details see man 4 crontabs\r\n\r\n# Example of job definition:\r\n# .---------------- minute (0 - 59)\r\n# | .------------- hour (0 - 23)\r\n# | | .---------- day of month (1 - 31)\r\n# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...\r\n# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat\r\n# | | | | |\r\n# * * * * * user-name command to be executed\r\n37 * * * * root run-parts \/etc\/cron.hourly\r\n23 5 * * * root run-parts \/etc\/cron.daily\r\n19 3 * * 0 root run-parts \/etc\/cron.weekly\r\n23 0 6 * * root run-parts \/etc\/cron.monthly<\/pre>\nAs you can see the crontab file already contain an explanation about how to define your own jobs. The syntax is the following:<\/p>\n
minute hour day month day_of_week username command<\/pre>\nAn asterisk (*) in the crontab can be used to specify all valid values, so if you need a command to be executed every day at midnight, you can add the following cron job:<\/p>\n
0 0 * * * root \/sample_command >\/dev\/null 2>&1<\/pre>\nSpecific users can also create cron jobs. User-specific cron jobs are located in \/var\/spool\/cron\/username.<\/p>\n
When you create cron jobs for specific users you do not need to specify the username in the cron job. The syntax for user-specific cronjobs should look like this:<\/p>\n
minute hour day month day_of_week command<\/pre>\n