Scheduling & Automation

0%
Schedulingcronsystemd-timeratbatchanacron

Scheduling & Automation

6 interactive questions

SchedulingQuestion
cronsystemd-timeratbatchanacron

Cron vs systemd timers — when to use which?

Click to reveal answer
SchedulingAnswer

cron — simple, user-level, ubiquitous, crontab -e. systemd timers — system-level, logging/journal, dependencies, accuracy, RandomizedDelaySec=, Persistent= (run if missed). Use timers for services; cron for user scripts.

SchedulingQuestion
cronsystemd-timeratbatchanacron

Crontab syntax and common pitfalls?

Click to reveal answer
SchedulingAnswer

* * * * * command (min hour dom mon dow). Pitfalls: % needs escaping, environment differs from shell (set PATH, HOME, SHELL), stdout/stderr go to mail (redirect: > /dev/null 2>&1), overlapping runs (use flock or systemd).

SchedulingQuestion
cronsystemd-timeratbatchanacron

How do you run a job only once at a specific time?

Click to reveal answer
SchedulingAnswer

at 14:30 (interactive) or echo "cmd" | at 14:30. atq lists, atrm N removes. batch runs when load < 1.5.

SchedulingQuestion
cronsystemd-timeratbatchanacron

What is anacron and when is it useful?

Click to reveal answer
SchedulingAnswer

Runs jobs periodically even if the system was off at the scheduled time. Reads /etc/anacrontab (period in days, delay in minutes, identifier, command). Good for laptops/desktops that aren't always on.

SchedulingQuestion
cronsystemd-timeratbatchanacron

How do you prevent overlapping cron jobs?

Click to reveal answer
SchedulingAnswer

Use flock -n /tmp/mylock.lock -c "command" (exits if locked). Or systemd timer with LockPersonality=/LockNamespace=. For scripts: implement PID file locking manually.

SchedulingQuestion
cronsystemd-timeratbatchanacron

How do you schedule a job to run on the first Monday of the month?

Click to reveal answer
SchedulingAnswer

Cron can't do "first Monday" directly. Two approaches: 1) 0 0 1-7 * 1 cmd (runs 1st-7th, then filter in script: [[ $(date +\%d) -le 7 ]]). 2) Use systemd timer with OnCalendar=Mon *-*-1..7.