Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    Packages, Services and Schedules

    cron runs with a minimal environment — a short PATH and none of your .zshrc variables. Scripts that work in your

    Chapter 11 of 125 min read

    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#

    SystemToolInstallSearchUpdate everything
    Ubuntu / Debianaptsudo apt install nginxapt search nginxsudo apt update && sudo apt upgrade
    Red Hat / Amazon Linuxdnf (yum on older ones)sudo dnf install nginxdnf search nginxsudo dnf upgrade
    macOSbrew (Homebrew)brew install jqbrew search jqbrew upgrade
    Alpine (tiny Docker images)apkapk 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:

    Config
    [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.target

    Notice: 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.

    Terminal
    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 PATH and none of your .zshrc variables. 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 questions
    1

    Install htop on Ubuntu. On your Mac.

    Show answer

    sudo apt install htop; brew install htop.

    2

    How do you see whether the studybuddy service is running, and follow its logs?

    Show answer

    sudo systemctl status studybuddy; journalctl -u studybuddy -f.

    3

    What does Restart=on-failure do?

    Show answer

    systemd restarts the service automatically if it exits with an error.

    4

    Write a cron line for "every Monday at 09:30".

    Show answer

    30 9 * * 1 /path/to/command.

    5

    A script works when you run it but fails in cron. Two likely reasons?

    Show answer

    cron's minimal PATH/environment (missing variables or commands), and relative paths — cron starts in a different directory. (Also: time zone differences.)