Running things later: cron and at

Intermediate · 19 min

Schedule commands with crontab's five fields and run one-off jobs with at. Why a cron job fails when the same command works at your prompt.

Before thisSurviving logout: nohup, disown and tmuxRedirectionThe PATH and how commands are found

Learn Linux TV · 19 min

Running things later: cron and at

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • cron runs the command in a crontab line whenever the clock matches its five fields: minute, hour, day of month, month, day of week. at runs a command once, at a time you give it.
  • A cron job does not run in your interactive shell. It gets a tiny environment (a short PATH, no dotfiles, your home directory as the working directory), so use absolute paths and set what the job needs.
  • cron has no terminal, so a job's output goes to mail or nowhere. Redirect stdout and stderr to a log file in the line itself.
  • A job that exits nonzero fails silently unless something reads the log. Check that the log contains what you expect after the first scheduled run.
  • In a crontab line, % is special and must be escaped as \%, which catches people who put date +%F in a job.

The problem: nobody is logged in

Surviving logout kept work running after you left. A backup at 2 a.m. or a report every Monday is different: nobody is there to start it. You need a daemon that watches the clock and launches commands for you. That is cron. A related tool, at, handles the one-off case, “run this once at 17:00”.

The five fields

You tell cron what to run in a crontab, a table with one job per line: five time fields, then the command.

# minute  hour  day-of-month  month  day-of-week  command
  30      2     *             *      1-5          /home/me/bin/backup.sh

Each field accepts * (every value), a number, a list (1,15), a range (1-5) or a step (*/15). Day of week is 0-7 with both 0 and 7 meaning Sunday. So:

  • */15 9-17 * * 1-5 is every 15 minutes between 09:00 and 17:59 on weekdays.
  • 0 0 1 * * is midnight on the first of each month.
  • 30 2 * * 1-5 is 02:30 Monday to Friday.

You edit your own table with crontab -e and view it with crontab -l. I did not run either. They would modify or reveal the real crontab on the machine I used, and I will not install one for a lesson. I also did not run a scheduled job through cron itself, so everything below about the environment comes from running the same command under a stripped-down shell, not from cron.

Two syntax traps, from the macOS man 5 crontab, which I read but did not exercise: when both day-of-month and day-of-week are restricted, the job runs when either matches, and an unescaped % ends the command, with everything after it fed to the command as standard input. Write a literal one as \%.

The cron environment

The main reason a cron job fails is that your interactive shell is not there. cron starts the command with a short environment, typically SHELL=/bin/sh, a small PATH, a HOME, and your home directory as the working directory. Your .bashrc is never read.

I ran this script, which uses shellcheck, a Homebrew-installed tool, under env -i with a cron-like environment, on macOS:

#!/usr/bin/env bash
cd "$(dirname "$0")" || exit 1
echo "500s: $(grep -c ' 500 ' fx/logs/access.log)"
shellcheck --version | sed -n 2p
echo "PATH=$PATH HOME=$HOME"
$ env -i SHELL=/bin/sh PATH=/usr/bin:/bin HOME=/private/tmp/x /bin/sh -c "$PWD/report.sh"
500s: 24
.../report.sh: line 4: shellcheck: command not found
PATH=/usr/bin:/bin HOME=/private/tmp/x
$ echo $?
0

Two lessons. shellcheck lives in /opt/homebrew/bin, which is not on that PATH, so the PATH lookup fails. And the exit status is still 0, because the last command in the script (echo) succeeded. Nothing marked the job as failed. Put PATH at the top of the script, or at the top of the crontab (PATH=/opt/homebrew/bin:/usr/bin:/bin), and test the same way:

$ env -i SHELL=/bin/sh PATH=/usr/bin:/bin HOME=/private/tmp/x /bin/sh -c "PATH=/opt/homebrew/bin:\$PATH $PWD/report.sh"
500s: 24
version: 0.11.0
PATH=/opt/homebrew/bin:/usr/bin:/bin HOME=/private/tmp/x

The working directory is also not your project. Started from /private/tmp, ls report.sh found nothing, so cd "$(dirname "$0")" as the first line is cheap insurance. env -i is a reasonable rehearsal, but it is not cron, which on your system may set other variables.

Where the output goes

cron has no terminal. By default it tries to mail whatever the job prints to you, and on a laptop with no mail setup that means it vanishes. Put the redirection in the line itself (this is the redirection you already know):

30 2 * * 1-5 /home/me/bin/report.sh >> /home/me/logs/report.log 2>&1

The first scheduled run is the real test, so read the log after it.

at: once, later

at 17:00
at> /home/me/bin/remind.sh
at> <Ctrl-D>

at reads the commands, queues them, and runs them once. atq lists the queue and atrm removes an entry. On this Mac at and atq exist, and at -l ran and listed nothing. The at man page here says atrun is disabled by default, so I did not submit a job, and I did not enable it, which means I cannot show a job running. On Linux at generally works once the atd service is running.

About the video

“Linux Crash Course - Scheduling Tasks with Cron” is from Learn Linux TV (Jay LaCroix), published September 2021, 19 minutes 25 seconds. The description says it covers scheduling a one-off command and repeating jobs with a crontab. I could not read a transcript and it has no chapters. It is an English-language channel, and it targets Linux servers, so details like the file locations may differ on macOS. Akamai Developers’ shorter cron video is linked as a second view.

Where this fits

That finishes processes and jobs: how to see them, signal them, background them, keep them alive and start them on a schedule. The next group is about working in the shell day to day, starting with history and readline shortcuts.

Check yourself

  1. 01What does 30 2 * * 1-5 mean?

    At 02:30 on every weekday, Monday to Friday. The fields are minute 30, hour 2, any day of month, any month, days of week 1 through 5.

  2. 02A script works when you run it by hand but fails under cron with 'command not found'. Why?

    cron starts jobs with a minimal PATH, and your dotfiles are not read. Use absolute paths to commands, or set PATH at the top of the script or crontab.

  3. 03How do you find out what a cron job printed?

    Redirect it yourself, for example >> /path/job.log 2>&1 at the end of the line. Otherwise cron tries to mail the output, and many systems have no mail set up.

  4. 04When would you use at instead of cron?

    For a command that should run once at a specific time, such as a reminder or a deferred restart. Cron is for repeating schedules.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark