How to use crontab for scripts periodically running in background

November 6, 2015 at 4:44 am

From definition Cron is a time-based job scheduler in Unix-like computer operating systems.
Using cron you can schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.

Crontab is file on your server with cron jobs to run periodically.
By default cron searches at /var/spool/cron/ directory for crontab files.
This is default directory for CentOS/Red Hat/RHEL/Fedora/Scientific Linux.
For Debian / Ubuntu Linux this can be /var/spool/cron/crontabs/
For FreeBSD/OpenBSD/NetBSD – /var/cron/tabs/

You may have multiple files in directory, each relates to username that relates to domain name,
For example if your domain’s username is “webik”, crotab file location should be /var/spool/cron/webik.

To access and update crontab file use SSH command crontab -e
Here you have commands to edit example “webik” crontab file

cd /var/spool/cron
crontab -u webik -e

And commands to view example “webik” crontab file

cd /var/spool/cron
crontab -u webik -v

 

Cron job syntax

 

10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1

This is simple cron job, which executes php script each 10 minutes.

The first part is “10 * * * *”. This is where we define a timer for schedule.
The rest of the line is the command to execute.

 

Timing Syntax

It consists of five parts: minute, hour, day of month, month, day of week

*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 7) (0 or 7 are Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

Few examples of timer
Every 15 minutes:

*/15 * * * * [command]

Every half hour:

0,30 * * * * [command]

Once per hour:

0 * * * * [command]

Once per hour at minute 15 (i.e. 00:15, 01:15, 02:15 etc.):

15 * * * * [command]

Every six hours

0 6 * * * [command]

Every six hours at minute 15 (i.e. 00:15, 06:15, 12:15 etc.):

15 6 * * * [command]

Once a day, at 2:30am:

30 2 * * * [command]

once a month, on the second day of the month at midnight (i.e. January 2nd 12:00am, February 2nd 12:00am etc.):

0 0 2 * * [command]

Every hour on Monday:

0 * * * 1 [command]

Useful cron jobs for scripts like AVS

It’s important to backup your database even daily.
1. First create directory “/dbbackup” in root of your server and chmod it 777, login SSH as ROOT and enter commands below

mkdir /dbbackup ; chmod 777 /dbbackup

Cron job to backup database every day at midnight is:

0 0 * * * /usr/bin/mysqldump -uDB_USER -pDB_PASSWORD --opt DB_NAME > /dbbackup/backup.sql

Of course replace DB_USER, DB_PASSWORD, DB_NAME with your database user, password and database name. You can also rename “backup.sql” to some more unique database backup filename.

You can also create cron job for any other php script. For example many AVS script users have sitemap generators from Nuevolab. You can create cron jobs to generate refresh sitemap everyday.

0 0 * * * php /home/username/public_html/maps/sitemap.php > /dev/null 2>&1
0 0 * * * php /home/username/public_html/maps/mrss.php > /dev/null 2>&1

Please note that you must use relative path to generator file, according to what it is on your server.

If you have cPanel installed on server, managing cron jobs is much more simplified.
Just open cPanel for your domain and at bottom click “Cron jobs” icon. In next window you can select cron period and enter command to execute. Very easy!

If you created CRON job manually, by editing crontab, don’t forget to restart CRON service.

If you are using Redhat (RHEL)/Fedora/CentOS Linux use the following commands.

# /etc/init.d/crond restart

OR RHEL/CentOS 5.x/6.x user:

# service crond restart

OR RHEL/Centos Linux 7.x user:

# systemctl restart crond.service

If you are using Debian or Ubuntu or Mint Linux the following commands.

# /etc/init.d/cron restart

or

$ sudo /etc/init.d/cron restart

or

$ sudo service cron restart

————————————————–

Often you need to run CRON job as a specific user.
Let’s say this is user “webik”. You can create Crontab for a user webik using this command:

crontab -e webik

Inside new crontab file you can enter now cron job executed by user webik.
Once all done, don’t forget to initialize new crontab running this command:

crontab webik

and don’t forget to restart CRON service!

========================

I hope this lesson will get you familiar with cron jobs now and allows you to use this useful feature for any task on your server.

Regards.