Difference between revisions of "Cron"

From ArchWiki
Jump to: navigation, search
(rm temporary i18n template)
(Partiality; re-wrote intro, installation; cleaner formating. This page needs help! :) I don't know enough about cronie to be able to do this effectively.)
Line 10: Line 10:
 
{{Article summary end}}
 
{{Article summary end}}
  
Cron is a powerful job scheduler for GNU/Linux and many other operating systems. It automates recurring tasks by executing commands at a given time. It has a wide range of potential applications; most simple recurring tasks, from backups to e-mail retrieval, can be automated using cron, saving users time and headaches.
+
From [http://en.wikipedia.org/wiki/Cron Wikipedia]:
  
==Installation==
+
'''''cron''' is the time-based job scheduler in Unix-like computer operating systems. cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration...''
  
There are multiple cron implementations available from which users may choose. {{Pkg|cronie}} is available in [core] and is installed as part of the '''base''' group, you can check it is correctly installed with:
+
== Installation ==
  
  # pacman -S --needed cronie
+
{{Pkg|cronie}} is installed by default as parted of the '''base''' group. Other cron implementations exist if preferred, Gentoo's [http://www.gentoo.org/doc/en/cron-guide.xml Cron Guide] offers comparisons.  For example, {{AUR|bcron}} or {{AUR|vixie-cron}} offer a wider range of features and configuration options.
  
Until May 2011, the default cron implementation for Arch Linux was {{Pkg|dcron}} (Dillon's Cron), which anyway is still supported and can be [[pacman|installed]] from [[official repositories]].
+
{{Note|Until May 2011, the default cron implementation for Arch Linux was {{Pkg|dcron}} (Dillon's Cron), which is still supported and can be installed from the [[official repositories]].}}
  
Alternatively, users may wish to install {{Pkg|fcron}} from [community] or {{AUR|bcron}} or {{AUR|vixie-cron}} from the [[AUR]]; all offer a wider range of features and configuration options.
+
== Configuration ==
  
The [http://www.gentoo.org/doc/en/cron-guide.xml Gentoo Linux Cron Guide] offers a comparison between these implementations.
 
 
==Initial configuration==
 
 
{{Out of date|The following sections of this article are still based on {{Pkg|dcron}}: you are invited to check the validity of the contents below and fix what is out of date.}}
 
{{Out of date|The following sections of this article are still based on {{Pkg|dcron}}: you are invited to check the validity of the contents below and fix what is out of date.}}
  
===Users & autostart===
+
=== Users & autostart ===
Cron should work "out-of-the-box" for most Arch Linux users. In order to use crontab, users must be members of a designated group, but in Arch Linux, that group is ''users'', of which all users should already be members. If for whatever reason some users are not members of this group, they can be added to it with the command:
+
  
  # gpasswd -a ''username'' users
+
cron should be working upon login on a new system to run root scripts. This can be check by looking at the log in {{ic|/var/log/}}. In order to use crontab application (editor for job entries), users must be members of a designated group ''users'' or ''root'', of which all users should already be members.  To ensure cron starts on boot, add the ''crond'' daemon to the [[Daemon#Starting_on_Boot|daemons]] array of [[rc.conf]].
  
and they should then be able to edit their own crontabs.
+
=== Handling errors of jobs ===
  
To ensure cron starts on boot, add the ''crond'' daemon to the daemons array of [[rc.conf]]. See [[Daemon#Starting_on_Boot]] for details.
+
Errors can occur during execution of jobs.  When this happens, cron registers '''stderr''' output of job as e-mail and attempts to send it to the users mail spools via '''sendmail''' command.
  
===Handling errors of jobs===
+
To log this message use the '''-M''' option in the configuration file ({{ic|crontd}}), then write a script or install rudimentary SMTP subsystem ('''esmtp''' in this example):
Errors can occur during execution of jobs. When this happens cron register '''stderr''' output of job as e-mail and try to send it by default via '''sendmail''' command.
+
To log this message you can use '''-M''' option of crontd and write you own script or install rudimentary SMTP subsystem ('''esmtp''' in this example):
+
  
 
  # pacman -S esmtp procmail
 
  # pacman -S esmtp procmail
  
After installation create file {{ic|/etc/esmtprc}} with this content:
+
After installation the SMTP program, configure it's routing ({{ic|/etc/esmtprc}} '''esmtp''' example):
  
 
  identity myself@myisp.com
 
  identity myself@myisp.com
Line 51: Line 45:
 
         default
 
         default
 
  mda "/usr/bin/procmail -d %T"
 
  mda "/usr/bin/procmail -d %T"
 +
 
Procmail needs root privileges to work in delivery mode but it is not an issue if you are running the cronjobs as root.
 
Procmail needs root privileges to work in delivery mode but it is not an issue if you are running the cronjobs as root.
  
Line 78: Line 73:
 
Now just repeat the test with {{ic|message.txt}} exactly as before.
 
Now just repeat the test with {{ic|message.txt}} exactly as before.
  
==Crontab format==
+
== Crontab format ==
  
 
The basic format for a crontab is:
 
The basic format for a crontab is:
Line 96: Line 91:
 
Will execute the script {{Ic|i_love_cron.sh}} at five minute intervals from 9 AM to 4:55 PM every weekday of the month except during the summer months (June, July, and August). More examples and advanced configuration techniques can be found below.
 
Will execute the script {{Ic|i_love_cron.sh}} at five minute intervals from 9 AM to 4:55 PM every weekday of the month except during the summer months (June, July, and August). More examples and advanced configuration techniques can be found below.
  
==Basic commands==
+
== Basic commands ==
  
 
Crontabs should never be edited directly; instead, users should use the ''crontab'' program to work with their crontabs. To be granted access to this command, user must be a member of the users group (see gpasswd command).
 
Crontabs should never be edited directly; instead, users should use the ''crontab'' program to work with their crontabs. To be granted access to this command, user must be a member of the users group (see gpasswd command).
Line 132: Line 127:
 
And restart terminal/s
 
And restart terminal/s
  
==Examples==
+
== Examples ==
  
 
The entry:
 
The entry:
Line 152: Line 147:
 
Will execute the script {{Ic|i_love_cron.sh}} at five minute intervals from 9 AM to 5 PM (excluding 5 PM itself) every weekday (Mon-Fri) of every month except during the summer (June, July, and August).
 
Will execute the script {{Ic|i_love_cron.sh}} at five minute intervals from 9 AM to 5 PM (excluding 5 PM itself) every weekday (Mon-Fri) of every month except during the summer (June, July, and August).
  
==More information==
+
== More information ==
  
 
The cron daemon parses a configuration file known as ''crontab''. Each user on the system can maintain a separate crontab file to schedule commands individually. The root user's crontab is used to schedule system-wide tasks (though users may opt to use {{ic|/etc/crontab}} or the {{ic|/etc/cron.d}} directory, depending on which cron implementation they choose).
 
The cron daemon parses a configuration file known as ''crontab''. Each user on the system can maintain a separate crontab file to schedule commands individually. The root user's crontab is used to schedule system-wide tasks (though users may opt to use {{ic|/etc/crontab}} or the {{ic|/etc/cron.d}} directory, depending on which cron implementation they choose).
Line 193: Line 188:
 
See the crontab [[man page]] for further information and configuration examples.
 
See the crontab [[man page]] for further information and configuration examples.
  
==run-parts issue==
+
== run-parts issue ==
cronie use run-parts to carry out script in cron.daily/cron.week.y/cron.montly. Be carefull that the script name in these folders should not have any '.', like backup.sh. Since run-parts without options will ignore them. Detail information see man run-parts.
+
  
==Running X apps==
+
cronie use run-parts to carry out script in cron.daily/cron.week.y/cron.monthly. Be careful that the script name in these folders should not have any '.', like backup.sh. Since run-parts without options will ignore them. Detail information see man run-parts.
 +
 
 +
== Running Xorg server based applicatoins ==
  
 
If you find that you cannot run X apps from cron jobs then put this before the
 
If you find that you cannot run X apps from cron jobs then put this before the
Line 215: Line 211:
 
  bash -c "xhost +si:localuser:$(whoami)"
 
  bash -c "xhost +si:localuser:$(whoami)"
  
==Asynchronous job processing==
+
== Asynchronous job processing ==
  
 
If you regularly turn off your computer but do not want to miss jobs, there are some solutions available (easiest to hardest):
 
If you regularly turn off your computer but do not want to miss jobs, there are some solutions available (easiest to hardest):
Line 226: Line 222:
 
; Anacron ([https://aur.archlinux.org/packages.php?ID=5196 AUR]): Full replacement for dcron, processes jobs asynchronously.
 
; Anacron ([https://aur.archlinux.org/packages.php?ID=5196 AUR]): Full replacement for dcron, processes jobs asynchronously.
  
==See also==
+
== See also ==
 +
 
 
[http://wiki.gotux.net/config:crontab CronTab Config and Examples]
 
[http://wiki.gotux.net/config:crontab CronTab Config and Examples]

Revision as of 14:26, 17 June 2012

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary link Template:Article summary end

From Wikipedia:

cron is the time-based job scheduler in Unix-like computer operating systems. cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration...

Installation

cronie is installed by default as parted of the base group. Other cron implementations exist if preferred, Gentoo's Cron Guide offers comparisons. For example, bcronAUR or vixie-cronAUR offer a wider range of features and configuration options.

Note: Until May 2011, the default cron implementation for Arch Linux was dcron (Dillon's Cron), which is still supported and can be installed from the official repositories.

Configuration

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: The following sections of this article are still based on dcron: you are invited to check the validity of the contents below and fix what is out of date. (Discuss in Talk:Cron#)

Users & autostart

cron should be working upon login on a new system to run root scripts. This can be check by looking at the log in /var/log/. In order to use crontab application (editor for job entries), users must be members of a designated group users or root, of which all users should already be members. To ensure cron starts on boot, add the crond daemon to the daemons array of rc.conf.

Handling errors of jobs

Errors can occur during execution of jobs. When this happens, cron registers stderr output of job as e-mail and attempts to send it to the users mail spools via sendmail command.

To log this message use the -M option in the configuration file (crontd), then write a script or install rudimentary SMTP subsystem (esmtp in this example):

# pacman -S esmtp procmail

After installation the SMTP program, configure it's routing (/etc/esmtprc esmtp example):

identity myself@myisp.com
       hostname mail.myisp.com:25
       username "myself"
       password "secret"
       starttls enabled
       default
mda "/usr/bin/procmail -d %T"

Procmail needs root privileges to work in delivery mode but it is not an issue if you are running the cronjobs as root.

To test that everything works correctly, create a file message.txt with "test message" in it.

From the directory containing message.txt run:

$ sendmail user_name < message.txt 

then:

$ cat /var/spool/mail/user_name 

You should see in the terminal, the "test message", the time and date it was sent.

Thats all, all error output of jobs now will be redirected to /var/spool/mail/$user_name.

Due to the privileged issue, it is hard to create and send the email to root. So you could ask esmtp to forward all root's email to some ordinary users. Add the following lines in esmtprc

force_mda="user-name"

If the above test did not work, try creating a file ~/.esmtprc with the same content as /etc/esmtprc, (you can just create a copy as normal user).

Run the following command to make sure it has the correct 710 permission:

$ chmod 710 ~/.esmtprc

Now just repeat the test with message.txt exactly as before.

Crontab format

The basic format for a crontab is:

<minute> <hour> <day_of_month> <month> <day_of_week> <command>
  • minute values can be from 0 to 59.
  • hour values can be from 0 to 23.
  • day_of_month values can be from 1 to 31.
  • month values can be from 1 to 12.
  • day_of_week values can be from 0 to 6, with 0 denoting Sunday.

Multiple times may be specified with a comma, a range can be given with a hyphen, and the asterisk symbol is a wildcard character. Spaces are used to separate fields. For example, the line:

*0,*5 9-16 * 1-5,9-12 1-5 /home/user/bin/i_love_cron.sh

Will execute the script i_love_cron.sh at five minute intervals from 9 AM to 4:55 PM every weekday of the month except during the summer months (June, July, and August). More examples and advanced configuration techniques can be found below.

Basic commands

Crontabs should never be edited directly; instead, users should use the crontab program to work with their crontabs. To be granted access to this command, user must be a member of the users group (see gpasswd command).

To view their crontabs, users should issue the command:

$ crontab -l

To edit their crontabs, they may use:

$ crontab -e

To remove their crontabs, they should use:

$ crontab -r

If a user has a saved crontab and would like to completely overwrite their old crontab, he or she should use:

$ crontab saved_crontab_filename

To overwrite a crontab from the command line (Wikipedia:stdin), use

$ crontab - 

To edit somebody else's crontab, issue the following command as root:

# crontab -u username -e

This same format (appending "-u username" to a command) works for listing and deleting crontabs as well.

To use nano rather than vi as crontab editor, add the following lines to /etc/bash.bashrc:

export EDITOR="/usr/bin/nano"

And restart terminal/s

Examples

The entry:

01 * * * * /bin/echo Hello, world!

runs the command /bin/echo Hello, world! on the first minute of every hour of every day of every month (i.e. at 12:01, 1:01, 2:01, etc.)

Similarly,

*/5 * * jan mon-fri /bin/echo Hello, world!

runs the same job every five minutes on weekdays during the month of January (i.e. at 12:00, 12:05, 12:10, etc.)

As noted in the Crontab Format section, the line:

*0,*5 9-16 * 1-5,9-12 1-5 /home/user/bin/i_love_cron.sh

Will execute the script i_love_cron.sh at five minute intervals from 9 AM to 5 PM (excluding 5 PM itself) every weekday (Mon-Fri) of every month except during the summer (June, July, and August).

More information

The cron daemon parses a configuration file known as crontab. Each user on the system can maintain a separate crontab file to schedule commands individually. The root user's crontab is used to schedule system-wide tasks (though users may opt to use /etc/crontab or the /etc/cron.d directory, depending on which cron implementation they choose).

There are slight differences between the crontab formats of the different cron daemons. The default root crontab for dcron looks like this:

/var/spool/cron/root
# root crontab
# DO NOT EDIT THIS FILE MANUALLY! USE crontab -e INSTEAD

# man 1 crontab for acceptable formats:
#    <minute> <hour> <day> <month> <dow> <tags and command>
#    <@freq> <tags and command>

# SYSTEM DAILY/WEEKLY/... FOLDERS
@hourly         ID=sys-hourly   /usr/sbin/run-cron /etc/cron.hourly
@daily          ID=sys-daily    /usr/sbin/run-cron /etc/cron.daily
@weekly         ID=sys-weekly   /usr/sbin/run-cron /etc/cron.weekly
@monthly        ID=sys-monthly  /usr/sbin/run-cron /etc/cron.monthly

These lines exemplify one of the formats that crontab entries can have, namely whitespace-separated fields specifying:

  1. @period
  2. ID=jobname (this tag is specific to dcron)
  3. command

The other standard format for crontab entries is:

  1. minute
  2. hour
  3. day
  4. month
  5. day of week
  6. command

The crontab files themselves are usually stored as /var/spool/cron/username. For example, root's crontab is found at /var/spool/cron/root

See the crontab man page for further information and configuration examples.

run-parts issue

cronie use run-parts to carry out script in cron.daily/cron.week.y/cron.monthly. Be careful that the script name in these folders should not have any '.', like backup.sh. Since run-parts without options will ignore them. Detail information see man run-parts.

Running Xorg server based applicatoins

If you find that you cannot run X apps from cron jobs then put this before the command:

export DISPLAY=:0.0 ;

That sets the DISPLAY variable to the first display; which is usually right unless you like to run multiple xservers on your machine.

If it still does not work then you need to use xhost to give your user control over X11:

# xhost +si:localuser:$(whoami)

I put it in my gnome `Startup Applications' like this:

bash -c "xhost +si:localuser:$(whoami)"

Asynchronous job processing

If you regularly turn off your computer but do not want to miss jobs, there are some solutions available (easiest to hardest):

Dcron 
Vanilla dcron supports asynchronous job processing. Just put it with @hourly, @daily, @weekly or @monthly with a jobname, like this:
@hourly         ID=greatest_ever_job      echo This job is very useful.
Cronwhip (AUR, forum thread)
Script to automatically run missed cron jobs; works with the default cron implementation, dcron.
Anacron (AUR)
Full replacement for dcron, processes jobs asynchronously.

See also

CronTab Config and Examples