Scheduled tasks: cron and timers

Reading and writing a crontab line, understanding cron's reduced environment, using at and scheduling with a systemd timer, including the missed task.

  • 35 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Crontab line: what is it?

    Show the answer

    A line that pairs five date fields (minute, hour, day of month, month, day of week) with a command, run when all the fields match.

    A field accepts * (every value), a list 1,4, a range 1-5 or a step */15. crontab -l lists the user's lines and crontab -e edits them.

  • When does the line “30 2 * * 1,4” run?

    • On Mondays and Thursdays, at 2:30
    • On the 1st and the 4th of each month, at 2:30 in the morning
    • Every day at 2:30, except on Mondays and Thursdays
    • On Tuesdays and Fridays, at 2:30, with Sunday as the first day
    Show the answer

    On Mondays and Thursdays, at 2:30

    The third field is the day of month, the fifth the day of week, where 1 is Monday and 0 or 7 is Sunday. The first two fields give the minute (30) first, then the hour (2).

  • The five fields of a crontab line are, in order: …, …, …, … and ….

    Show the answer

    The five fields of a crontab line are, in order: minute, hour, day of month, month and day of week.

    The smallest unit comes first: 30 7 * * 1-5 reads minute 30, hour 7, every day of the month, every month, Monday to Friday.

  • Step in a cron expression: what is it?

    Show the answer

    The notation */n or a-b/n that keeps one value out of n in a field: in minutes, */15 gives 0, 15, 30 and 45.

    */5 * * * * runs every 5 minutes, but 5 * * * * only runs at minute 5 of each hour: the star and the slash make all the difference.

  • The script succeeds in the terminal but fails every night under cron with “pg_dump: command not found”. What is the likely cause, and the fix?

    • pg_dump is not installed on the machine: install the PostgreSQL tools package
    • The user's account is not allowed to run scripts: give it sudo
    • Cron's reduced PATH does not contain pg_dump's directory: write its absolute path or set PATH= at the top of the crontab
    • Cron ignores scripts located outside /etc/cron.d: move them into that directory
    Show the answer

    Cron's reduced PATH does not contain pg_dump's directory: write its absolute path or set PATH= at the top of the crontab

    Cron reads no profile or .bashrc, and its PATH is limited to the basic system directories. Since the command works by hand, it is installed: it is its path that cron does not know.