cron

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, dates or intervals. It is commonly used to automate system maintenance or administration.

Installation

There are many cron implementations, but none of them are installed by default as the base system uses systemd/Timers instead. See the Gentoo's cron guide, which offers comparisons.

Packages available:

Configuration

Activation and autostart

After installation, the daemon will not be enabled by default. The installed package likely provides a service, which can be controlled by systemctl. For example, cronie uses .

Check and similar directories to see which jobs are present. Activating cron service will trigger all of them.

Handling errors of jobs

cron registers the output from stdout and stderr and attempts to send it as email to the user's spools via the command. Cronie disables mail output if /usr/bin/sendmail is not found. In order for mail to be written to a user's spool, there must be an smtp daemon running on the system, e.g. opensmtpd. Otherwise, you can install a package that provides the sendmail command, and configure it to send mail to a remote mail exchanger. You can also log the messages by using the option and writing a custom script.

  1. Edit the unit.
  2. Install , msmtp, opensmtpd, sSMTP, or write a custom script.

Example with sSMTP

sSMTP is a send-only sendmail emulator which delivers email from a local computer to an smtp server. While there are currently no active maintainers, it is still by far the simplest way to transfer mail to a configured mailhub. There are no daemons to run, and configuration can be as simple as editing 3 lines in a single configuration file (if your host is trusted to relay unauthenticated email through your mailhub). sSMTP does not receive mail, expand aliases, or manage a queue.

Install , which creates a symbolic link from /usr/bin/sendmail to . You must then edit . See sSMTP for details. Creating a symbolic link to /usr/bin/sendmail insures that programs like S-nail (or any package which provides ) will just work without modification.

Restart to insure that it detects that you now have a /usr/bin/sendmail installed.

Example with msmtp

Install msmtp-mta, which creates a symbolic link from /usr/bin/sendmail to /usr/bin/msmtp. Restart to make sure it detects the new command. You must then provide a way for to convert your username into an email address.

Then either add line to your crontab, like so:

MAILTO=your@email.com

or create and append this line:

aliases /etc/aliases

and create :

your_username: your@email.com
# Optional:
default: your@email.com

Then modify the configuration of cronie daemon by replacing the command with:

ExecStart=/usr/bin/crond -n -m '/usr/bin/msmtp -t'

Example with esmtp

Install and .

After installation configure the routing:

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

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

From the same directory run:

$ sendmail user_name < message.txt 

then:

$ cat /var/spool/mail/user_name

You should now see the test message and the time and date it was sent.

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

Due to the privileged issue, it is hard to create and send emails to root (e.g. ). You can ask esmtp to forward all root's email to an ordinary user with:

Example with opensmtpd

Install opensmtpd.

Edit . The following configuration allows for local delivery:

listen on localhost
action "local" mbox alias <aliases>
match for local action "local"

You can proceed to test it. First start . Then do:

$ echo test | sendmail user

user can check their mail in with any reader able to handle mbox format, or just have a look at the file . If everything goes as expected, you can enable opensmtpd for future boots.

This approach has the advantage of not sending local cron notifications to a remote server. On the downside, you need a new daemon running.

Long cron job

Suppose this program is invoked by cron :

#!/bin/sh
echo "I had a recoverable error!"
sleep 1h

What happens is this:

  1. cron runs the script
  2. as soon as cron sees some output, it runs your MTA, and provides it with the headers. It leaves the pipe open, because the job has not finished and there might be more output.
  3. the MTA opens the connection to postfix and leaves that connection open while it waits for the rest of the body.
  4. postfix closes the idle connection after less than an hour and you get an error like this :

To solve this problem you can use the command chronic or sponge from moreutils. From their respective man page:

chronic
chronic runs a command, and arranges for its standard out and standard error to only be displayed if the command fails (exits nonzero or crashes). If the command succeeds, any extraneous output will be hidden.
sponge
sponge reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before opening the output fileā€¦ If no output file is specified, sponge outputs to stdout.

Chronic too buffers the command output before opening its standard output.

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.

Spaces are used to separate fields. To fine-tune your schedule you may also use one of the following symbols:

SymbolDescription
*Wildcard, specifies every possible time interval
,List multiple values separated by a comma.
-Specify a range between two numbers, separated by a hyphen
/Specify a periodicity/frequency using a slash

For example, the line:

*/5 9-16 * 1-5,9-12 1-5 ~/bin/i_love_cron.sh

will execute the script at five minute intervals from 9 AM to 4:55 PM on weekdays except during the months of June, July, and August.

In addition, crontab has some special keywords:

KeywordDescription
@rebootat startup
@yearlyonce a year
@annuallyidentical to @yearly
@monthlyonce a month
@weeklyonce a week
@dailyonce a day
@midnightidentical to @daily
@hourlyonce an hour

For example:

@reboot ~/bin/i_love_cron.sh

will execute the script at startup.

See more at: https://www.adminschoice.com/crontab-quick-reference

More examples and advanced configuration techniques can be found below.

Basic commands

Crontabs should never be edited directly; instead, users should use the program to work with their crontabs.

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, they 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.

Examples

The entry:

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

runs the command 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.).

The line (as noted in ):

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

will execute the script 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).

Periodical settings can also be entered as in this crontab template:

Here are some self-explanatory crontab syntax examples:

Default editor

To use an alternate default editor, define the environment variable in a shell initialization script as described in Environment variables.

As a regular user, will need to be used instead of sudo for the environment variable to be pulled correctly:

$ su -c "crontab -e"

To have an alias to this is required to carry the arbitrary string because launches in a new shell:

alias scron="su -c $(printf "%q " "crontab -e")"

Running X.org server-based applications

Cron does not run under the X.org server therefore it cannot know the environmental variable necessary to be able to start an X.org server application so they will have to be defined. One can use a program like to do it:

17 02 * ... /usr/bin/xuserrun /usr/bin/xclock

Or they can be defined manually ( will give the current DISPLAY value):

17 02 * ... env DISPLAY=:0 /usr/bin/xclock

If running notify-send for desktop notifications in cron, notify-send is sending values to dbus. So it needs to tell dbus to connect to the right bus. The address can be found by examining DBUS_SESSION_BUS_ADDRESS environment variable and setting it to the same value. Therefore:

17 02 * ... env DBUS_SESSION_BUS_ADDRESS=your-address notify-send 'Foo bar'

If done through say SSH, permission will need be given:

# 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):

Cronie

cronie comes with anacron included. The project homepage says:

Cronie contains the standard UNIX daemon crond that runs specified programs at scheduled times and related tools. It is based on the original cron and has security and configuration enhancements like the ability to use pam and SELinux.

Dcron

Vanilla dcronAUR 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

is a script to automatically run missed cron jobs; it works with the former default cron implementation, dcron.

See also the forum thread.

Anacron

Anacron is a full replacement for dcron which processes jobs asynchronously.

It is provided by cronie. The configuration file is . Information on the format can be found in the anacrontab(5). Running will test for validity.

Fcron

Like anacron, fcron assumes the computer is not always running and, unlike anacron, it can schedule events at intervals shorter than a single day which may be useful for systems which suspend/hibernate regularly (such as a laptop). Like cronwhip, fcron can run jobs that should have been run during the computer's downtime.

When replacing cronie with fcron be aware the spool directory is and the command is used instead of crontab to edit the user crontabs. These crontabs are stored in a binary format with the text version next to them as foo.orig in the spool directory. Any scripts which manually edit user crontabs may need to be adjusted due to this difference in behavior.

A quick scriptlet which may aide in converting traditional user crontabs to fcron format:

cd /var/spool/cron && (
 for ctab in *; do
  fcrontab ${ctab} -u ${ctab}
 done
)

See also the forum thread.

Ensuring exclusivity

If you run potentially long-running jobs (e.g., a backup might all of a sudden run for a long time, because of many changes or a particular slow network connection), then () can ensure that the cron job will not start a second time.

  5,35 * * * * /usr/bin/flock -n /tmp/lock.backup /root/make-backup.sh

Cronie

The relevant file hierarchy for cronie is the following:

   /etc/
     |----- cron.d/
              | ----- 0hourly
     |----- cron.minutely/
     |----- cron.hourly/
              | ----- 0anacron
     |----- anacrontab
     |----- cron.daily/
     |----- cron.monthly/
     |----- cron.weekly/
     |----- crontab
     |----- cron.deny

Cronie provides both cron and anacron functionalities: cron runs jobs at regular time intervals (down to a granularity of one minute) as long as the system is available at the specified time, while anacron executes commands at intervals specified in days. Unlike cron, it does not assume that the system is running continuously. Whenever the system is up, anacron checks if there are any jobs that should have been run and handles them accordingly.

A cron job can be defined in a crontab-like file in the directory or added within the file. Note the latter is not present by default but is used if it exists. As instructed by , any executable file in will be run every hour (by default at minute 1 of the hour). Files in are executed every minute if instructed accordingly in . The executables are typically shell scripts, symlinks to executable files can also be used. The file includes the list of users not allowed to use crontab, without this file, only users listed in /etc/cron.allow can use it.

Anacron works similarly, by executing the files in the , /etc/cron.weekly and directories, placed there depending on the desired job frequency. The cron job makes sure that anacron is run once daily to perform its pending tasks.

Dcron

The cron daemon parses a configuration file known as . 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 or the directory, depending on which cron implementation they choose).

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 . For example, root's crontab is found at

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

gollark: Wait, are they applying this to laptops too?
gollark: Phones and such.
gollark: Lots of devices only have one port though.
gollark: They're increasingly moving onto the same port. USB-C does video, peripherals and power nowadays.
gollark: Legislation inevitably takes ages. Stuff would be stuck on a worse standard.

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.