cron - at

cron - at

On a Linux sytem many tasks are scheduled for regular execution, like rotating logs, doing backups, maintenance scripts, and cron is the daemon which is responsible for executing these scheduled and recurring commands (every day, every week, etc.)

By default, all users can schedule the execution of tasks. Each user has thus their own crontab file in which they can record scheduled commands. It can be edited by running crontab -e (its
content is stored in the /var/spool/cron/crontabs/user file).

You can restrict access to cron by creating an authorization file (whitelist) in /etc/cron.allow, in which you indicate the only users authorized to schedule commands. All others will automatically be blocked of this feature. If you write usernames in /etc/cron.deny, it will block crontab for those users.


Example of a crontab file:

#min hour day mon dow command
# Download data every night at 7:25 pm
25 19 * * * $HOME/bin/get.pl
# 8:00 am, on weekdays (Monday through Friday)
00 08 * * 1-5 $HOME/bin/dosomething


Each significant line of a crontab describes a scheduled command with the six (or seven) following fields:
• the value for the minute (number from 0 to 59);
• the value for the hour (from 0 to 23);
• the value for the day of the month (from 1 to 31);
• the value for the month (from 1 to 12);
• the value for the day of the week (from 0 to 7, 1 corresponding to Monday, Sunday being represented by both 0 and 7
• the user name under whose identity the command must be executed
 (this is only in the /etc/crontab file and in the fragments located in /etc/cron.d/, but not in the users’ own crontab files);
• the command to execute (when the conditions defined by the first five columns are met).

Each value can be expressed in the form of a list of possible values (separated by commas). The syntax a-b describes the interval of all the values between a and b. The syntax a-b/c describes the interval with an increment of c (example: 0-10/2 means 0,2,4,6,8,10). An asterisk * is a wildcard, representing all possible values.


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

Special cron features:

The cron package includes by default some additional directories, and scripts placed into those directories will be executed based on the name of the dir:
• programs in the /etc/cron.hourly/ directory once per hour;
• programs in /etc/cron.daily/ once per day;
• programs in /etc/cron.weekly/ once per week;
• programs in /etc/cron.monthly/ once per month.

Many Debian packages rely on this service: by putting maintenance scripts in these directories,they ensure optimal operation of their services.

cron recognizes some abbreviations which replace the first five fields in a crontab entry. They correspond to the most classic scheduling options:
• @yearly: once per year (January 1, at 00:00);
• @monthly: once per month (the 1st of the month, at 00:00);
• @weekly: once per week (Sunday at 00:00);
• @daily: once per day (at 00:00);
• @hourly: once per hour (at the beginning of each hour).
• @reboot: just after rebooting the computer


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

at Command

The at executes a command at a specified moment in the future. It takes the desired time and date as command-line parameters, and the command to be executed in its standard input. The command will be executed as if it had been entered in the current shell. at even takes care to retain the current environment, in order to reproduce the same conditions when it executes the command. The time is indicated by following the usual conventions: 16:12 or 4:12pm

$ at 09:00 27.07.15 <<END
> echo ”Testin in progress!” \
> | mail test@debian.org
> END
warning: commands will be executed using /bin/sh
job 31 at Mon Jul 27 09:00:00 2015


To cancel an at tasks run atrm task-number. The task number is indicated by the at command when you scheduled it, but you can find it again with the atq command, which gives the current list of scheduled tasks.

/etc/at.allow and /etc/at.deny files work the same way as cron.allow and cron.deny.

No comments:

Post a Comment