Home Blog Certs Knowledge Base About

LPIC-1 107.2 β€” Automate System Administration Tasks by Scheduling Jobs

Exam weight: 4 β€” LPIC-1 v5, Exam 102

What You Need to Know

From the official LPIC-1 objectives:

  • Manage cron and at jobs.
  • Configure user access to job scheduling.
  • Use systemd timer units as an alternative to cron.

Key files and commands: /etc/cron.d/, /etc/cron.{daily,hourly,monthly,weekly}/, /etc/at.deny, /etc/at.allow, /etc/crontab, /etc/cron.allow, /etc/cron.deny, /var/spool/cron/, crontab, at, atq, atrm, systemctl, systemd-run.


Cron β€” Recurring Jobs

The cron daemon wakes every minute and checks crontab files for jobs to run. User crontabs are stored in /var/spool/cron/.

User Crontab Format

A user crontab has 5 time fields followed by the command:

min  hour  dom  month  dow  command
FieldRangeDescription
min0-59Minute
hour0-23Hour
dom1-31Day of month
month1-12Month
dow0-7Day of week (0 and 7 both = Sunday)
commandβ€”Shell command to run

Time Field Operators

OperatorMeaningExample
*Any value* * * * * β€” every minute
,List1,15,30 β€” at minutes 1, 15, 30
-Range1-5 β€” Monday through Friday
/Step*/20 β€” every 20 units

Crontab Examples

# Run daily at 10:00 am
0 10 * * * /home/frank/foo.sh

# Run every Tuesday at 08:00, 08:15, 08:30, 08:45
0,15,30,45 08 * * 2 /home/frank/bar.sh

# Run Mon-Fri Jan and June, days 1-15, at 20:30
30 20 1-15 1,6 1-5 /home/frank/foobar.sh

# Every 20 minutes
*/20 * * * * /path/to/script.sh

Crontab Shortcuts

ShortcutEquivalentMeaning
@rebootβ€”Once at startup
@hourly0 * * * *Every hour at :00
@daily / @midnight0 0 * * *Daily at midnight
@weekly0 0 * * 0Weekly, Sunday midnight
@monthly0 0 1 * *Monthly, 1st at midnight
@yearly / @annually0 0 1 1 *Yearly, Jan 1 at midnight

Crontab Variables

Set at the top of a crontab file:

VariableDefaultEffect
HOMEUser homeWorking directory for cron jobs
MAILTOUsernameEmail recipient for output; set to "" to suppress
PATHSystem pathSearch path for commands
SHELL/bin/shShell used to run commands

crontab Command

OptionDescription
-eEdit the current user’s crontab (opens $EDITOR)
-lList (display) the current crontab
-rRemove the current crontab
-u userOperate on another user’s crontab (root only)

System Crontabs

System crontabs have a 6th field (username) before the command, since they run as root:

# /etc/crontab or files in /etc/cron.d/
30 01 * * * root /root/barfoo.sh >>/root/output.log 2>>/root/error.log

Drop-in directories for scripts (no crontab format needed β€” just executable scripts):

  • /etc/cron.hourly/
  • /etc/cron.daily/
  • /etc/cron.weekly/
  • /etc/cron.monthly/

Cron Access Control

/etc/cron.allow and /etc/cron.deny control which non-root users may use crontab:

  • If /etc/cron.allow exists: only listed users may schedule jobs.
  • If only /etc/cron.deny exists: listed users may not schedule jobs; an empty file means all users are allowed.
  • If neither exists: access depends on the distribution.

Both files contain one username per line.


systemd Timers β€” Cron Alternative

Systemd timers are unit files with the .timer suffix. Each timer activates a matching .service unit (same name, different suffix).

Timer File Structure

[Unit]
Description=Run the foobar service

[Timer]
OnCalendar=Mon *-*-1..7 05:30:00
Persistent=true

[Install]
WantedBy=timers.target

OnCalendar Syntax

DayOfWeek Year-Month-Day Hour:Minute:Second

DayOfWeek is optional. Use * for any, , for list, .. for range (not -), / for step.

OnCalendar Examples

ExpressionMeaning
*-*-* 08:30:00Every day at 08:30
Sat,Sun *-*-* 05:00:00Saturday and Sunday at 05:00
*-*-01 13:15,30,45:001st of every month at 13:15, 13:30, 13:45
Fri *-09..12-* 16:20:00Every Friday in Sep, Oct, Nov, Dec at 16:20
*-*-* *:00/05:00Every 5 minutes
Mon *-*-* 00:00:00Every Monday at midnight

OnCalendar Shortcuts

ShortcutEquivalent
hourly*-*-* *:00:00
daily*-*-* 00:00:00
weeklyMon *-*-* 00:00:00
monthly*-*-01 00:00:00
yearly*-01-01 00:00:00

Managing Timers

systemctl enable foobar.timer
systemctl start foobar.timer
systemctl daemon-reload          # after editing timer files
systemctl list-timers            # active timers, sorted by next trigger
systemctl list-timers --all      # include inactive timers

Timer logs go to the systemd journal; review with journalctl. Ordinary users need --user with systemctl and journalctl.


at β€” One-Time Jobs

at schedules a command to run once at a specified future time. The atd daemon must be running.

$ at now +5 minutes
warning: commands will be executed using /bin/sh
at> date
at> Ctrl+D
job 12 at Sat Sep 14 09:15:00 2019

at Options

OptionDescription
-c jobidPrint commands of a job to stdout
-d jobidDelete a job (alias for atrm)
-f fileRead job commands from a file
-lList pending jobs (alias for atq)
-mSend mail even if there was no output
-q queueSpecify queue (a-z, A-Z); capital letters behave like batch
-vShow execution time before reading the job

Time Specifications for at

  • HH:MM β€” 24h or 12h with AM/PM; if time has passed, next day is assumed
  • midnight, noon, teatime (4 PM)
  • now +N minutes/hours/days/weeks
  • today, tomorrow
  • Date formats: month-name day, MMDDYY, MM/DD/YY, DD.MM.YY, YYYY-MM-DD

atq and atrm

atq                    # list pending at jobs (as root: all users)
atrm 14                # delete job 14
atrm 50 51 52          # delete multiple jobs
at -l                  # alias for atq
at -d 14               # alias for atrm 14

at Access Control

Same logic as cron:

  • If /etc/at.allow exists: only listed users may schedule at jobs.
  • If only /etc/at.deny exists: listed users may not; empty file = all allowed.
  • If neither exists: access depends on the distribution.

systemd-run β€” Alternative to at

# Run date at a specific calendar time
systemd-run --on-calendar='2019-10-06 11:30' date

# Run a script after 2 minutes
systemd-run --on-active="2m" ./foo.sh

Ordinary users need --user. Review logs with journalctl --user.


Quick Reference

User crontab: 5 time fields + command (min hour dom month dow cmd)
System crontab: 6 fields (adds username before command)
  /etc/crontab, /etc/cron.d/

Drop-in script dirs (no crontab format):
  /etc/cron.hourly/  /etc/cron.daily/  /etc/cron.weekly/  /etc/cron.monthly/

User crontabs stored in: /var/spool/cron/

crontab flags:
  -e  edit    -l  list    -r  remove    -u user  (root only)

Cron shortcuts (in crontab):
  @reboot  @hourly  @daily/@midnight  @weekly  @monthly  @yearly/@annually

Cron output: mailed by default; MAILTO="" suppresses; MAILTO="user" redirects

Cron access:
  /etc/cron.allow   whitelist (only listed users allowed)
  /etc/cron.deny    blacklist (listed users denied; empty = all allowed)

systemd timer:
  OnCalendar=DayOfWeek Year-Month-Day Hour:Minute:Second
  Persistent=true   catch up missed runs
  systemctl enable/start NAME.timer
  systemctl list-timers [--all]

at β€” one-time scheduling (requires atd):
  at HH:MM / at now +N minutes / at midnight / at tomorrow
  atq                 list pending jobs
  atrm ID             delete job
  at -c ID            show job commands
  at -l = atq    at -d = atrm

at access:
  /etc/at.allow   /etc/at.deny   (same logic as cron)

systemd-run (alternative to at):
  systemd-run --on-calendar='YYYY-MM-DD HH:MM' cmd
  systemd-run --on-active="Nm" cmd

Exam Questions

  1. What is the format of a user crontab entry? β†’ Five time fields (min hour dom month dow) followed by the command.
  2. What does */20 * * * * mean in crontab? β†’ Run every 20 minutes.
  3. What shortcut replaces 0 0 * * * in crontab? β†’ @daily or @midnight
  4. What is the difference between /etc/crontab and user crontabs? β†’ System crontabs have a 6th field specifying the username to run the job as.
  5. Where are user crontabs stored? β†’ /var/spool/cron/
  6. What does crontab -e do? β†’ Opens the current user’s crontab for editing in $EDITOR.
  7. What does the MAILTO="" variable in a crontab do? β†’ Suppresses email output from cron jobs.
  8. What is /etc/cron.allow? β†’ Whitelist β€” only listed non-root users may use crontab; takes precedence over cron.deny.
  9. What command runs a job once in 30 minutes? β†’ at now +30 minutes
  10. What is atq? β†’ Lists pending at jobs; as root, shows jobs for all users.
  11. What is atrm? β†’ Deletes pending at jobs by job ID; at -d is an alias.
  12. What daemon must be running for at to work? β†’ atd
  13. What is the OnCalendar syntax for every Friday in September–December at 16:20? β†’ Fri *-09..12-* 16:20:00
  14. How do you view active systemd timers? β†’ systemctl list-timers
  15. What systemd-run option schedules a one-time job at a calendar time? β†’ --on-calendar=
  16. What does Persistent=true do in a systemd timer? β†’ Runs the timer immediately at next start if the last scheduled run was missed.
  17. How do you schedule a cron job for the 1st and 15th of each month at 08:30? β†’ 30 08 1,15 * * command
  18. What OnCalendar shortcut means “every Monday at midnight”? β†’ weekly (equivalent to Mon *-*-* 00:00:00)

Exercises

Exercise 1 β€” Crontab Shortcuts

What crontab time specification corresponds to @weekly?

Answer

0 0 * * 0 β€” run at midnight on Sunday (day of week 0).


Exercise 2 β€” Reading Cron Expressions

Explain what the following crontab entries do:

30 13 * * 1-5
00 09-18 * * *
0,20,40 11 * * Sun
*/20 * * * *
Answer
  • 30 13 * * 1-5 β€” at 13:30 (1:30 PM) every weekday (Mon-Fri)
  • 00 09-18 * * * β€” at the top of every hour from 09:00 to 18:00, every day
  • 0,20,40 11 * * Sun β€” every Sunday at 11:00, 11:20, and 11:40
  • */20 * * * * β€” every 20 minutes

Exercise 3 β€” Send Cron Output by Email

How do you redirect all cron job output to user emma via email? And how do you prevent any mail from being sent?

Answer

Set MAILTO at the top of the crontab:

MAILTO="emma"      # send output to emma
MAILTO=""          # suppress all mail

Exercise 4 β€” One-Time at Job

Schedule a job to run ./backup.sh at 10:30 AM on October 31st as an ordinary user.

Answer
at 10:30 AM October 31
at> ./backup.sh
at> Ctrl+D

Exercise 5 β€” OnCalendar Expressions

Write an OnCalendar expression that runs a timer every 5 minutes.

Answer

*-*-* *:00/05:00

This matches every day, every hour, at minutes 0, 5, 10, 15, …, 55.


LPIC-1 Study Notes | Topic 107: Administrative Tasks