← Back to Linux

Scheduling tasks and managing software

Systems run jobs on schedules and need software installed and updated safely. cron, systemd timers, and package managers handle these routine but critical tasks.

cron

cron runs commands on a schedule defined in crontab files. Each line has five time fields, minute, hour, day of month, month, and day of week, followed by a command: 30 2 * * * /usr/local/bin/backup.sh runs at 02:30 every day; */15 * * * * runs every 15 minutes; 0 9 * * 1-5 runs at 09:00 on weekdays. crontab -e edits a user's crontab; system jobs live in /etc/crontab and /etc/cron.d/.

cron pitfalls

cron runs jobs with a minimal environment: PATH may be short, so use full paths or set PATH in the crontab, and the working directory is the user's home. Output is mailed to the user or lost, so redirect it to a log. A job that takes longer than its interval can overlap with the next run; tools such as flock prevent concurrent runs. Remember that times are in the system's time zone.

systemd timers

systemd timers are an alternative: a .timer unit activates a matching .service unit. OnCalendar=*-*-* 02:30:00 runs daily at 02:30; OnCalendar=Mon..Fri 09:00 runs on weekdays; OnBootSec=5min runs five minutes after boot; Persistent=true runs a missed job after the machine was off. Timers integrate with the journal, dependencies, and resource limits, and systemctl list-timers shows when each next fires.

One-off scheduling

at schedules a command to run once at a given time, such as echo 'rm /tmp/big.iso' | at 23:00, with atq listing and atrm removing jobs. systemd-run --on-active=30m command creates a transient timer for a single future run. sleep inside scripts is suitable only for short waits.

Package managers

Distributions install software from signed repositories using package managers that resolve dependencies. Debian and Ubuntu use apt: apt update refreshes package lists, apt install nginx installs, apt upgrade upgrades, apt remove removes, and apt search finds packages; dpkg handles individual .deb files. Fedora and RHEL use dnf: dnf install, dnf upgrade, dnf remove, and rpm for individual packages. Arch uses pacman; Alpine uses apk.

Repositories and trust

Repositories are signed with GPG keys, and package managers verify signatures so tampered packages are rejected. Adding third-party repositories means trusting their maintainers with root-level access to your system, so add only reputable sources. Distributions publish security updates quickly; tools such as unattended-upgrades or dnf-automatic can install them automatically.

Universal and container packages

Snap (Canonical) and Flatpak (common on Fedora and other desktops) package applications with their dependencies, running them in sandboxes across distributions; AppImage bundles an application in a single file. Containers, built with Docker or Podman, package entire application environments. Language-specific managers such as pip, npm, and cargo install libraries for development and should be used in virtual environments or project directories rather than system-wide.

Worked example

To compress old logs every Sunday at 03:00 with a systemd timer: create /etc/systemd/system/logpack.service with ExecStart=/usr/local/bin/logpack.sh and Type=oneshot, and logpack.timer with [Timer] OnCalendar=Sun 03:00 and Persistent=true and [Install] WantedBy=timers.target. Run systemctl daemon-reload and systemctl enable --now logpack.timer; systemctl list-timers shows the next run, and journalctl -u logpack shows each run's output.

Key takeaways

cron runs commands from crontabs with five fields: minute, hour, day of month, month, day of week; crontab -e edits them. cron jobs have a minimal environment, so use full paths, log output, and prevent overlaps with flock. systemd timers pair .timer and .service units, support OnCalendar, OnBootSec, and Persistent, and integrate with the journal; list-timers shows schedules. at and systemd-run handle one-off jobs. apt, dnf, pacman, and apk install signed packages with dependency resolution; dpkg and rpm handle single packages. Trust only reputable repositories and automate security updates. Snap, Flatpak, AppImage, and containers package applications; use language package managers in project environments.

Flashcard

cron

Runs commands on schedules

crontab files.

Check your understanding

What schedule does the crontab entry 0 6 * * 1 describe?

← Previous lesson Next lesson →