Email This List Email This List Print This List Print This List

Sched­ule backups of web­sites

The most com­mon way to auto­mat­ic­ally run scripts at sched­uled times is through cron. For example, you can use cron to make sched­uled web requests to a spe­cif­ic URL on your web­site. This is a reli­able way to per­form peri­od­ic work, such as mail­ing users or gen­er­at­ing reports.

Word­Press and Drupal have imple­men­ted sys­tems that will ini­ti­ate back­ground pro­cessing of their sched­uled tasks without cron. You do not need to con­fig­ure cron jobs for Word­Press and Drupal.

View­ing Crontabs

Each sys­tem user has their own list of sched­uled tasks. This list is called acrontab. To view a crontab, SSH in to your serv­er and run the com­mand:

crontab -l

Edit­ing Crontabs

To edit a sys­tem user­’s crontab, run the com­mand:

crontab -e

The first time you edit a crontab, you’ll be asked which edit­or to use. You should choose the default option unless you prefer a dif­fer­ent edit­or.

Once you select your edit­or, you’ll be edit­ing the crontab. There are some com­ments at the top of the crontab. You can cre­ate your own com­ments by start­ing lines with a hash (#).

This is what you’ll see in your edit­or:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task

To add a cron job that makes a web request every hour, scroll to the bot­tom of the file and add the line:

@hourly wget -q -O - "http://example.com/cron.php"

If you’ve used the default edit­or, you can save your changes by press­ing CTRL‑Ofol­lowed by Enter. To exit, press CTRL‑X.

The Format of Crontab Entries

Each line in the crontab con­tains two parts: how often to run the com­mand fol­lowed by the com­mand to run.

How Often to Run the Com­mand

You can use any of the fol­low­ing for con­fig­ur­ing how often to run a com­mand:

@hourly
Once an hour at the begin­ning of the hour
@daily
Once a day at mid­night
@weekly
Once a week at mid­night on Sunday morn­ing
@monthly
Once a month at mid­night on the morn­ing of the first day of the month
@yearly
Once a year at mid­night on the morn­ing of Janu­ary 1
@reboot
At star­tup

The Com­mand to Run

The format of com­mands in the crontab is exactly the same as if you were run­ning the com­mand through SSH.

Log­ging Out­put from Crontab

You can log out­put from each crontab entry by redir­ect­ing com­mand out­put to a log file.

For example, the fol­low­ing will log all mes­sages and errors to the file/srv/users/serverpilot/apps/APPNAME/cron.log.

@hourly wget -q -O - "http://example.com/cron.php" >>/srv/users/serverpilot/apps/APPNAME/cron.log 2>&1

Email­ing Out­put from Crontab

You can also con­fig­ure cron to email out­put and error mes­sages to you. To do this, add a MAILTO address at the top of the crontab.

MAILTO="you@example.com"
@hourly wget -q -O - "http://example.com/cron.php" >>/srv/users/serverpilot/apps/APPNAME/cron.log 2>&1

Advanced Crontab Schedul­ing

You can also sched­ule cron jobs to execute at spe­cif­ic times and spe­cif­ic days of the week. To do this, instead of using @hourly or @daily as the time to run a com­mand, you can spe­cify the time as the minute of the hour, the hour of the day, the day of the month, the month of the year, and the day of the week.

For example, the fol­low­ing will run our cron job at 1:35 a.m. on the 7th and 20th day of each month.

35 1 7,20 * * wget -q -O - "http://example.com/cron.php"

An aster­isk (*) means all val­ues rather than a spe­cif­ic value.

Debug­ging Crontab Com­mands

When writ­ing or debug­ging scripts to be run by cron, you can always run your scripts manu­ally through SSH.

If you think your script isn’t work­ing, SSH into your serv­er, view your crontab to see the exact com­mand cron is run­ning, and run the same com­mand in your shell.

Debug­ging will also be easi­er if you’ve logged each cron job’s out­put.

Related Post

admin has written 133 articles