Jan 032012

sleep —- Wait a set number of seconds, doing nothing

watch —- Run a program at set intervals
at —- Schedule a job for a single, future time
crontab — Schedule jobs for many future times

If you need to launch programs at particular times or at regular intervals, Linux provides several scheduling tools at various degrees of complexity.

sleep time_specification

The sleep command simply waits a set amount of time. The given time specification can be an integer (meaning seconds) or an integer followed by the letter s (also seconds), m (minutes), h (hours), or d (days).

$ sleep 5m Do nothing for 5 minutes

sleep is useful for delaying a command for a set amount of time:

$ sleep 10 && echo ‘Ten seconds have passed.’

(10 seconds pass) Ten seconds have passed.

watch [options] command

The watch program executes a given command at regular intervals; the default is every two seconds. The command is passed to the shell (so be sure to quote or escape any special characters), and the results are displayed in a full-screen mode, so you can observe the output conveniently and see what has changed. For example, watch -n 60 date executes the date command once a minute, sort of a poor man’s clock. Type ^C to exit.

Useful options
-n seconds Set the time between executions, in seconds.
-d Highlight differences in the output, to emphasize what has changed from one execution to the next.

at [options] time_specification

The at command runs a shell command once at a specified time:

$ at 7am next sunday

at> echo Remember to go shopping | Mail smith

at> lpr $HOME/shopping-list

at> ^D


job 559 at 2012-09-14 21:30
The time specifications understood by at are enormously flexible.

In general, you can specify:
• A time followed by a date (not a date followed by a time)
• Only a date (assumes the current clock time)
• Only a time (assumes the very next occurrence, whether today or tomorrow)
• A special word like now, midnight, or teatime (16:00)
• Any of the above followed by an offset, like “+ 3 days”

Dates are acceptable in many forms: december 25 2012, 25 december 2012, december 25, 25 december, 12/25/2012, 25.12.2012, 20121225,today, thursday, next thursday, next month, next year, and more.
Month names can be abbreviated to three letters (jan, feb, mar, …).Times are also flexible: 8pm, 8 pm, 8:00pm, 8:00 pm, 20:00, and 2000 are equivalent. Offsets are a plus or minus sign followed by whitespace and an amount of time: + 3 seconds, + 2 weeks, – 1 hour, and so on.*
If you don’t specify a part of the date or time, at copies the missing information from the system date and time. So “next year” means one year from right now, “thursday” means the upcoming Thursday at the current clock time, “december 25” means the next upcoming December 25, and “4:30pm” means the very next occurrence of 4:30 p.m. in the future.
Your command is not evaluated by the shell until execution time, so wildcards, variables, and other shell constructs are not expanded until then. Also, your current environment (see printenv) is preserved within each job so it executes as if you were logged in. Aliases, however, aren’t available to at jobs, so don’t include them.

To list your at jobs, use atq (“at queue”):

$ atq

559 2012-09-14 07:00 a smith

To delete an at job, run atrm (“at remove”) with the job number:

$ atrm 559

Useful options:
-f filename Read commands from the given file instead of standard input.
-c job_number Print the job commands to standard output.

crontab [options] [file]


The crontab command, like at, schedules jobs for specific times.

However, crontab is for recurring jobs, such as “Run this command at midnight on the second Tuesday of each month.” To make this work, you edit and save a file (called your crontab file):

$ crontab -e

which automatically gets installed in a system directory (/var/spool/cron). Once a minute, a Linux process called cron wakes up, checks your crontab file, and executes any jobs that are due.

$ crontab -e

Edit your crontab file in your default editor ($EDITOR)

$ crontab -l

Print your crontab file on standard output

$ crontab -r

Delete your crontab file

$ crontab myfile

Install the file myfile as your crontab file
The superuser can add the option -u username to work with other users’ crontab files.

Crontab files contain one job per line. (Blank lines and comment lines beginning with “#” are ignored.) Each line has six fields, separated by whitespace. The first five fields specify the time to run the job, and the last is the job command itself.

Minutes of the hour
Integers between 0 and 59. This can be a single number (30), a sequence of numbers separated by commas (0,15,30,45), a range (20–30), a sequence of ranges (0-15,50-59), or an asterisk to mean “all.” You can also specify “every nth time” with the suffix /n; for instance, both */12 and 0-59/12 mean 0,12,24,36,48 (i.e., every 12 minutes).

Hours of the day
Same syntax as for minutes.

Days of the month
Integers between 1 and 31; again, you may use sequences, ranges, sequences of ranges, or an asterisk.
Months of the year
Integers between 1 and 12; again, you may use sequences, ranges, sequences of ranges, or an asterisk. Additionally, you may use three-letter abbreviations (jan, feb, mar, …), but not in ranges or sequences.

Days of the week
Integers between 0 (Sunday) and 6 (Saturday); again, you may use sequences, ranges, sequences of ranges, or an asterisk.
Additionally, you may use three-letter abbreviations (sun, mon, tue, …), but not in ranges or sequences.

Command to execute
Any shell command, which will be executed in your login environment, so you can refer to environment variables like $HOME and expect them to work. Use only absolute paths to your commands (e.g., /usr/bin/who instead of who) as a general rule.

* * * * * Every minute
45 * * * * 45 minutes after each hour (1:45, 2:45, etc.)
45 9 * * * Every day at 9:45 am
45 9 8 * * The eighth day of every month at 9:45 am
45 9 8 12 * Every December 8 at 9:45 am
45 9 8 dec * Every December 8 at 9:45 am
45 9 * * 6 Every Saturday at 9:45 am
45 9 * * sat Every Saturday at 9:45 am
45 9 * 12 6 Every Saturday in December, at 9:45 am
45 9 8 12 6 Every December 8 AND every Saturday, at 9:45 am
© Incase of any copyright infringements please check copyrights page for faster resolutions.

Leave a Reply

Show Buttons
Hide Buttons