Loading blogs…
Linux4 min read
Run your app as a systemd service that restarts on crash, replace cron with systemd timers, and read logs with journalctl. Copy-paste unit files included.
Full-stack engineer, New Delhi
If you deploy anything to a Linux server — a Node API, a Python worker, a bot — you'll eventually want it to start on boot, restart when it crashes and keep its logs somewhere sensible. That's exactly what systemd does, and you only need three pieces of it: services, timers and journalctl.
| Command | What it does |
|---|---|
systemctl status app | Is it running? Last few log lines |
sudo systemctl start app / stop / restart | Control it now |
sudo systemctl enable --now app | Start now and on every boot |
sudo systemctl daemon-reload | Reload after editing a unit file |
systemctl list-units --failed | What's broken? |
journalctl -u app -f | Follow the service's logs |
Say you have a Node API in /opt/myapi. Create a dedicated user so it doesn't run as root:
sudo useradd --system --home /opt/myapi --shell /usr/sbin/nologin myapi
sudo chown -R myapi: /opt/myapiThen write /etc/systemd/system/myapi.service:
[Unit]
Description=My API
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=myapi
WorkingDirectory=/opt/myapi
ExecStart=/usr/bin/node server.js
EnvironmentFile=/etc/myapi.env
Restart=on-failure
RestartSec=3
# basic hardening — cheap and effective
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ReadWritePaths=/opt/myapi/data
ProtectHome=true
[Install]
WantedBy=multi-user.targetSecrets go in /etc/myapi.env (chmod 600), one KEY=value per line — not in the unit file and not in git.
sudo systemctl daemon-reload
sudo systemctl enable --now myapi
systemctl status myapiKill the process and watch systemd bring it back three seconds later. That's Restart=on-failure doing its job.
Timers need two files, but you get logging, missed-run catch-up and easy status for free.
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly database backup
[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/backup-db.sh# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup every night
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=5min
[Install]
WantedBy=timers.targetsudo systemctl enable --now backup.timer
systemctl list-timers # when does everything run next?
sudo systemctl start backup # run it right now to testPersistent=true means if the server was off at 02:30, the job runs as soon as it boots. OnCalendar accepts friendly values too: daily, weekly, Mon..Fri 09:00. Test an expression with systemd-analyze calendar "Mon..Fri 09:00".
Everything your service prints to stdout/stderr lands in the journal.
journalctl -u myapi -f # follow live
journalctl -u myapi --since "1 hour ago"
journalctl -u myapi -p err # errors only
journalctl -b -1 -p warning # warnings from the previous boot
journalctl -u myapi -o json-pretty -n 5
journalctl --disk-usage
sudo journalctl --vacuum-time=14d # trim old logsOn your own machine, you can run services as yourself — great for dev servers, syncing scripts or a local bot. Put units in ~/.config/systemd/user/ and add --user:
systemctl --user enable --now syncthing
journalctl --user -u syncthing -fsystemctl status app — read the last lines, they usually say whyjournalctl -u app -n 50 --no-pager — more contextExecStart command by hand as the service user: sudo -u myapi /usr/bin/node server.jssystemd-analyze verify /etc/systemd/system/app.service catches typos in the unit fileThat's 90% of systemd a developer needs. Services keep things running, timers replace cron, and the journal answers "what happened?".
Linux
Use ed25519 keys, ~/.ssh/config shortcuts, jump hosts and port forwarding like a pro — then harden your server's SSH daemon.
Arch Linux
Install Arch Linux on a UEFI machine step by step — partitioning, pacstrap, bootloader, networking and your first user — with every command explained.
Arch Linux
Make Arch Linux dependable: Btrfs snapshots with Snapper and snap-pac, safe update habits, an LTS kernel fallback and arch-chroot recovery.