Debianhelp.co.uk


Scheduling Backup Jobs using at and crontab in debian

You can schedule a command or a script using two tools

crontab : Schedules tasks once or repeatedly.

       at : Schedules tasks once.

Scheduling With the crontab Command

You can use the crontab command to run commands at regular times. For example, you could schedule a backup of your files every Friday. Commands can be scheduled to the minute.

If you want to schedule a command to run only once, look into the at command.

The crontab command works by reading a crontab file. A crontab file is a plain text file that lists all the jobs you have scheduled.

Each line of the crontab file consists of the command with all its options and arguments, and the date and time to run it.

By default, all users can set up their own crontab files. However, the root user can use two files to control access:

/etc/cron.allow: If this file exists, only users listed in it can use crontab.

/etc/cron.deny: If this file exists, users listed in it cannot use crontab.

All crontab files are stored in the /var/spool/cron/crontabs/ directory. However, since regular users do not have read or write permission to this directory, they must use the crontab command to schedule jobs.

Cron, as supplied in Debian, has two purposes:

To run system jobs on a daily/weekly/monthly basis

To allow users to setup their own schedules

The system schedules are setup when the package is installed, via the creation of some special directories:

/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

Except for hte first one which is special, these directories allow scheduling of system-wide jobs in a coarse manner. Any script which is executable and placed inside them will run at the frequency which its name suggests.

For example if you place a script inside /etc/cron.daily it will be executed once per day, every day.

The time that the scripts run in those system-wide directories is not something that an administration typically changes, but the times can be adjusted by editing the file /etc/crontab. The format of this file will be explained shortly.

The normal manner which people use cron is via the crontab command. This allows you to view or edit your crontab file, which is a per-user file containing entries describing commands to execute and the time to execute them.

To display user crontab file you run the following command

#crontab -l

root can view any users crontab file by adding "-u username", for example:

#crontab -u test -l # List test's crontab file.

The format of these files is as follows. Each line is a collection of six fields separated by spaces.

The fields are:

The number of minutes after the hour (0 to 59)
The hour in military time (24 hour) format (0 to 23)
The day of the month (1 to 31)
The month (1 to 12)
The day of the week(0 or 7 is Sun, or use name)
The command to run

Crontab file Format or syntax would be like this

* * * * * Command to be executed
- - - - -
| | | | |
| | | | +----- Day of week (0-6)
| | | +------- Month (1 - 12)
| | +--------- Day of month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Min (0 - 59)

To use a crontab file

crontab options user or crontab -e . Only the root user can specify another user's crontab file. The crontab file opens in the vim editor. The first time that you use a file, it is blank Enter the file, and save it.

Crontab Options

-e - Opens a user's crontab file in the vi text editor.

file - Creates a crontab file.

-l user - Views a user's crontab file.

-r user - Removes a user's crontab file.

-u user - Sets which user's crontab file is affected.

Editing and Reading a crontab File

To edit a crontab, type crontab -e user . The file opens in the default editor for the current user.

Each line in a crontab file schedules a single task.

If you need to use a percentage sign, put a back slash in front of it: % . Otherwise, it is read as the start of a new line, and the command does not run.

Each line consists of six columns, each one separated by a line or a tab.

Each of the first five columns is an option for setting the date and time. Each of the first five columns can specify:

A particular date or time.
A series of dates or times, with a comma between them.
A range of dates or time, beginning with the start, with a hyphen between them.

The sixth column is the command to run, including options and arguments. No quotation marks are need to enclose the command.

Disable Email notifications in Crontab

There are a couple of ways you can remedy this behaviour

At the top of the crontab file, place the following command

MAILTO=”" (That’s a null or just two quotes back to back)

Redirect the output of the offending program in your crontab
20 6 * * 1-5 /home/grim/alarm >& /dev/null

Method #1 will suppress ALL email from your crontab. It does this by telling cron to mail your output to nobody. It will simply vanish from your system. You may want this, you may not. If you’re debugging a script that keeps acting up, the output cron mails will definitely be of some help.

Method #2 allows you to selectively redirect particular programs output to the bitbucket. /dev/null is the *NIX equivalent to a “Big Black Hole”™ on your computer. So if, say, you want the output from your alarm, but not from your email reminders just append >& /dev/null to the affected commands. Alternately, if you’d still like to receive mail about execution errors but not standard output you could append 1> /dev/null instead of >& /dev/null. This is an either/or situation. You just want one or the other, not both.

Enable Email notifications in Crontab

if you want to enable email notifications in crontab at the top of the crontab file, place the following command

MAILTO=”emaiid@domain.com"

Crontab examples

For example, 0 12 * * * /usr/share/bin/alarm would run a script called alarm every day at noon.

The sixth column is the command to run, including options and arguments. No quotation marks are need to enclose the command.

For example, 0 12 * * * /usr/share/bin/alarm would run a script called alarm every day at noon.
Some more exabples

# Run the `something` command every hour on the hour
0 * * * * /sbin/something

# Run the `nightly` command at ten minutes past midnight every day
10 0 * * * /bin/nightly

# Run the `monday` command every monday at 2 AM
0 2 * * 1 /usr/local/bin/monday

# Use a range of hours matching 1, 2, 3 and 4AM
* 1-4 * * * /bin/some-hourly

# Use a set of hours matching 1, 2, 3 and 4AM
* 1,2,3,4 * * * /bin/some-hourly

For more options and how to use check crontab man page

Scheduling With the at Command

You can use the at command to schedule a command or script to run a single time. The command includes several utilities.

If you want to schedule a command more than once, look into the crontab command.

By default, only the root user can use the at command. However, the root user can use two files to allow regular users the command:

/etc/at.allow: If this file exists, only users listed in it can use the command.

/etc/at.deny: If this file exists, users listed in it cannot use the command.

The at command depends on a plain text file. This file lists commands or scripts, one per line. The end of the file must be marked by <EOT> on a line by itself.

The time for a task can be set using several formats. The simplest is a 24 hour clock in the hh:mm format, followed by the date in the mm: dd:yyyy format.

You can enter commands by entering the name of a plain text file in which commands or scripts are listed, one per line. Type: at -f file command time date .

at command syntax

at TIME [ DATE ]

By entering the time and entering the commands at an at prompt, one per line. The end of the file must be marked by <EOT> on a line by itself. Type: at time date . This method is especially useful if you are entering multiple commands.

The at Command (Scheduling): Options

-c task - Displays listed tasks.

-d task - Deletes the specified tasks.

-f file - Read tasks from the file entered.

-l  - Lists all tasks scheduled for the current user.

-m - Sends mail to the user when the task is run.

The at Command (Scheduling): Time Formats

day - Sets the day of the week.

hh:mm - Sets the time.

midnight - Sets the time to 12:00 am .

mmddyyyy - Sets the date.

noon - Sets the time to 12:00 pm .

now - Runs the command immediately or at the same time on the specified day.

teatime - Sets the time to 4:00 pm .

today - Runs the command today.

tomorrow - Runs the command tomorrow.

For more options and how to use check at man page

Using the at Utilities

The at command can be controlled by two utilities:

atrm : Deletes tasks. Type: #atrm job .

atq : Lists tasks for the current user, or all jobs if used by the root user. Type: #atq