Linux administration

Automate and Schedule System Administration Tasks

Nguyen Hai Chau
Vietnam National University

Running Commands in the Future

  • There is sometimes a need for commands not to be run immediately, but scheduled to run later
  • One-off commands:
    • "At 10:00 tomorrow, e-mail me this reminder message."
    • These are known as at commands
  • Regularly repeating commands:
    • "Every night, rebuild the database used by the locate command."
    • These are known as cron jobs

at Commands

  • at commands are defined using at:
$ at 16:30
at> pstree > processes
at> <EOT>
  • The time the command should run is given as a parameter to at
  • at then prompts for the command itself
    • Command(s) exactly as they would be typed in the shell
    • Press Ctrl+D to finish
  • The at daemon will run the command at the specified time. In this example, the output of running pstree at 16:30 will be saved in the file processes

Commands Run by the At Daemon

  • A command executed by the at daemon:
    • Has the permissions of its owner
    • Runs in the directory it was set up
    • Has the environment in which it was set up
    • Does not run in a terminal
  • Output from the command:
    • Cannot be included in a terminal window
    • Will be mailed to its owner

At Command Specification

  • A command may be specified on standard input instead of interactively
  • From a file:
$ at 16:30 < monitor_processes.sh
  • The commands contained in the file monitor_processes.sh are run at 16:30

Opening Windows from at Commands

  • The $DISPLAY environment variable is not provided in at commands' environments
  • This needs to be set for an at command to be able to open windows
  • Discover the current value and type it in again:
$ echo $DISPLAY
beehive:0
$ at 11:00
at> DISPLAY=beehive:0 xclock &
at> <EOT>
  • Use interpolation to embed it in the command:
$ echo "DISPLAY=$DISPLAY clock &" | at 11:00

at Command Date & Time Specification

  • Unadorned times are in the next 24 hours:
$ at 09:30
  • Tomorrow can be specified explictly: $ at 17:00 tomorrow
  • A specific date can be used:
$ at 11:00 Nov 11
$ at 00:30 16.04.06
- Relative times can be specified in minutes, hours, days, or weeks:
$ at now + 45 minutes
$ at 16:00 + 3 days

Managing at Commands

  • atq lists any pending at commands:
$ atq
38  2002-01-16 11:00    a
  • The number at the start of each line identifies that at command
  • A particular at command can be displayed with at -c:
$ at -c 38
#!/bin/sh
umask 2
cd /home/simon || { echo 'Bad directory' >&2; exit 1 }
echo 'Check the download has completed.'
  • Real at commands include the environment too. Remove an at command with atrm:
$ atrm 38

Simple Cron Job Specification

  • The simplest method for specifying cron jobs is to save each job as a separate file in an appropriate directory:
    • /etc/cron.daily/ is for jobs to be run daily
    • Once a day, each file in that directory is run
    • The files are typically shell scripts
    • There are equivalent directories for monthly, weekly, and possibly hourly jobs
    • Each job is run with root permissions
  • Normally only root can set up cron jobs this way
  • Any required environment variables must be set explicitly

More Complex Cron Job Specification

  • Sometimes more control is needed:
    • To run jobs at a non-standard time
    • To run jobs as a user other than root
  • The directory /etc/cron.d/ is for this purpose
  • Each file in that directory must contain lines in a specific format:
    • When the command should run
    • For which user the command should be run
    • The command to be run
  • Such a file is known as a cron table or crontab
    • Details are in crontab(5)
    • Easier to have one file per job

Crontab Format

  • Blank lines are ignored
  • Comments are lines starting with a hash (#)
  • Environment variables can be set:
PATH=/usr/local/bin
  • Example cron job specification
30 9 * * * root /usr/local/bin/check_logins
  • Explanation:
    • At 09:30
    • On all days
    • For the root user
    • Run the command /usr/local/bin/check_logins

Crontab Date & Time Specification

  • Order of the date and time fields:
    • Minute (0–59)
    • Hour (0–23)
    • Day of the month (1–31)
    • Month (1–12)
    • Day of the week (0–7; 0 and 7 are Sunday)
  • Note: Fields almost in ascending order
  • The command is run when the fields match the current time
  • A field containing an asterisk (*) always matches
  • Three-letter abbreviations can be used for month and day names
# Run every Friday night at 17:30:
30 17 * * Fri root /usr/local/bin/weekly-backup

More Complex Crontab Dates & Times

  • A list of alternative values for a field are specified by commas:
# Run at :15 and :45 past each hour:
15,45 * * * * httpd /usr/local/bin/generate-stats-page
  • A range is specified with a hyphen:
# Run every half hour 09:15-17:45 Mon-Fri:
15,45 9-17 * * 1-5 root /usr/local/bin/check-faxes
  • Numbers rather than names must be used for months and days in lists and ranges
  • A step through a range is specified with a slash:
# Run every two hours 08:30-18:30 Mon-Fri:
30 8-18/2 * * 1-5 root /usr/local/bin/check-faxes

/etc/crontab

  • The /etc/crontab file is an older way of specifying cron jobs
  • Each job in that file is like a file from /etc/cron.d/
  • Having many unrelated cron jobs in a single file is much harder to manage
  • This file may be the mechanism by which your system runs the contents of /etc/cron.daily/ and friends
  • There is no need to use this file for anything else

User Crontabs

  • Sometimes non-root users need to set up cron jobs
  • Each user has a crontab file
    • This is not edited directly
    • The crontab command manipulates it
    • Use crontab -e option to edit the crontab
  • The editor in the $EDITOR variable is invoked for this
    • Use crontab -l to display the crontab
  • The format is very similar to that of /etc/rc.d/ crontabs
    • But there is no username field
    • All commands run as the owner of the crontab

Cron Job Output

  • Cron jobs do not run in a terminal window
  • Generally they are administrative tasks designed not to produce any output when run successfully
  • Any output that is generated by a cron job is mailed:
    • The recipient can be specified in the $MAILTO environment variable
    • Otherwise mail is sent to the job's owner
    • Jobs in /etc/cron.daily et al are owned by root

at Command and Cron Job Permissions

  • Non-root users can be prohibited from having crontabs
    • If /etc/cron.allow exists then only users listed in it may have a crontab
    • If it doesn't exist but /etc/cron.deny does, then users not listed in the latter may have a crontab
    • If neither exist, then all users may have crontabs
  • Permissions for running at commands are similar:
    • The files /etc/at.allow and /etc/at.deny are analogous
    • If neither file exists then no users may run at commands
    • If only /etc/at.deny exists but is empty then all users may run at command

Exercise 1

  • a. Set up a command which in a couple of minutes' time will write the disk usage of the partition containing /home/ to ˜/home.use.
  • b. Change to your home directory. Set up a command which in ten minutes' time will write the disk usage of the partition containing the current directory to a file. Repeat the above for two more directories in other partitions, writing to separate files. Before the time is up, display the list of pending commands. Examine each one. How do they differ?

Exercise 2

  • a. Set up a command which will mail you a reminder message in a few minutes. Remember that output from a job run by the at daemon will be mailed to its owner, so there is no need to invoke an explicit mail command. Check that the mail arrives. Mutt is a simple mail reader you can use to do this.
  • b. Make the xeyes command start in a minute's time, capable of opening window's on your screen. Remember that any error messages from a command failing to run will be mailed to you.
  • c. Make a clock appear on your screen after 5 minutes from now

Exercise 3

  • a. Create a file containing a command which will delete all files in ˜/tmp/ when it is run. Make that script be run every hour.
  • b. Set up a script that once a minute appends the following data to a file:
    • The current date and time
    • A count of the number of processes currently running Monitor that file, and start and stop programs to affect the number reported. When you've finished, deactivate the script.
  • c. As a non-privileged user, set up two scripts:
    • In even-numbered minutes, write the time the system has been running to a particular file.
    • In odd-numbered minutes, delete that file.

Exercise 4

Set up scripts to run as a non-privileged user at the following times; you won't be able to test most of them, but it gives you practice at getting used to the order of the fields. Add the jobs one at a time, so that your crontab will be parsed to check for errors after each one.

  • At 09:25 every Sunday in December
  • At 21:30 on the 1st and 15th of every Month
  • Every three hours at weekends
  • Half past every hour during weekdays
  • The first day of every quarter
  • The evening of the first and third Thursday of each month
  • At 17:30 every other day