The Plain Picture#
An app store for command-line software.
A building's caretaker who starts the lifts every morning, restarts the boiler if it breaks, and keeps a logbook.
An alarm clock that runs a task at set times.
The Real Names#
Package managers#
| System | Tool | Install | Search | Update everything |
|---|---|---|---|---|
| Ubuntu / Debian | apt | sudo apt install nginx | apt search nginx | sudo apt update && sudo apt upgrade |
| Red Hat / Amazon Linux | dnf (yum on older ones) | sudo dnf install nginx | dnf search nginx | sudo dnf upgrade |
| macOS | brew (Homebrew) | brew install jq | brew search jq | brew upgrade |
| Alpine (tiny Docker images) | apk | apk add curl |
systemd — the service manager#
On almost every modern Linux server, systemd is PID 1 (Operating Systems Ch 3). It starts long-running programs — services (also called daemons) — at boot, restarts them if they crash, and collects their logs.
StudyBuddy's service is described by a unit file, /etc/systemd/system/studybuddy.service:
[Unit]
Description=StudyBuddy API
After=network.target
[Service]
User=studybuddy
WorkingDirectory=/opt/studybuddy
EnvironmentFile=/opt/studybuddy/.env
ExecStart=/opt/studybuddy/venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000
Restart=on-failure
[Install]
WantedBy=multi-user.targetNotice: it runs as a non-root user (Ch 4), loads its settings from a file of environment variables (Ch 8), binds to
0.0.0.0 (Networking 2: TCP, UDP and Ports Ch 2), and restarts itself if it crashes.
sudo systemctl status studybuddy # running? since when? last few log lines
sudo systemctl restart studybuddy # stop (SIGTERM) + start
sudo systemctl enable studybuddy # start automatically at boot
sudo systemctl daemon-reload # after editing a unit file
journalctl -u studybuddy -f # follow this service's logs live
journalctl -u studybuddy --since "10 min ago"cron — scheduled jobs#
crontab -e opens your schedule. Each line is five time fields and a command:
┌───────── minute (0–59) │ ┌─────── hour (0–23) │ │ ┌───── day of month (1–31) │ │ │ ┌─── month (1–12) │ │ │ │ ┌─ day of week (0–6, Sunday = 0) │ │ │ │ │ 0 2 * * * /opt/studybuddy/scripts/backup.sh >> /var/log/backup.log 2>&1 # every day at 02:00 */5 * * * * /opt/studybuddy/scripts/check_health.sh # every 5 minutes
* means "every". The same five-field syntax is used by Kubernetes CronJobs, GitHub Actions schedules and cloud
schedulers (DevOps (coming soon)) — learn it once.
Gotchas
cron runs with a minimal environment — a short
PATHand none of your.zshrcvariables. Scripts that work in your terminal fail in cron. Use full paths and set variables inside the script.cron uses the server's time zone, which on cloud servers is usually UTC — not IST. 02:00 UTC is 07:30 IST.
Check yourself
5 questionsInstall htop on Ubuntu. On your Mac.
Show answerHide answer
sudo apt install htop; brew install htop.
How do you see whether the studybuddy service is running, and follow its logs?
Show answerHide answer
sudo systemctl status studybuddy; journalctl -u studybuddy -f.
What does Restart=on-failure do?
Show answerHide answer
systemd restarts the service automatically if it exits with an error.
Write a cron line for "every Monday at 09:30".
Show answerHide answer
30 9 * * 1 /path/to/command.
A script works when you run it but fails in cron. Two likely reasons?
Show answerHide answer
cron's minimal PATH/environment (missing variables or commands), and relative paths — cron starts in a different
directory. (Also: time zone differences.)