Stop 02 · Linux & Command LineTopics · 11/11

systemd & Service Management

Part of the DevOps Roadmap

In short

Run your app with node app.js and it dies the moment you log out or it crashes. systemd keeps it running, starts it on boot, and restarts it when it falls over.

The modern init system managing services (starting, stopping, restarting, enabling on boot) on most Linux distributions — understanding unit files and systemctl is essential for running any long-lived process reliably.

The mascot opens a school gate early in the morning with a big key ring, a water pump nearby.

Think of it like this

The school peon's morning round

Every morning the school peon opens the gate, switches on the lights, then starts the water pump, always in that order. If the pump trips at noon, he restarts it. systemd is that peon: it starts services in order at boot and restarts them when they fail.

Watch it happen

Turning app.js into a service

Unit file
reload
enable
start
auto-restart

1/5You write /etc/systemd/system/myapp.service describing how to run the app.

The big ideas

What you need to know

Unit files describe services

A .service file says what command to run, as which user, and what to do if it stops.

enable vs start

start runs it now. enable makes it start on boot. You usually want both: systemctl enable --now.

Don't run as root

Set User=myapp so a bug in your app can't take over the whole machine.

Logs come for free

Anything your app prints goes to the journal. Read it with journalctl -u myapp.

See it in code

Walk through the code

ini
1# /etc/systemd/system/myapp.service2[Unit]3Description=My Node API4After=network.target5 6[Service]7User=myapp8WorkingDirectory=/opt/myapp9ExecStart=/usr/bin/node server.js10Restart=on-failure11Environment=PORT=300012 13[Install]14WantedBy=multi-user.target

1/4[Unit] describes the service and says to start it after networking is up.

Then run sudo systemctl daemon-reload and sudo systemctl enable --now myapp. Check it with systemctl status myapp.

Save yourself the pain

Common mistakes

Oops

Editing a unit file and restarting the service, but nothing changes.

Do this instead

Run sudo systemctl daemon-reload after every unit file edit, then restart the service.

Oops

Starting your app with nohup in an SSH session and calling it production.

Do this instead

Use a systemd service. It survives logouts and reboots, restarts on crashes and logs to the journal.

Your mission

Make a crash-proof service

~30 min

Quick check

You ran systemctl start myapp. After a reboot, the app isn't running. Why?

Go deeper

  1. 1Practice starting, stopping, and checking the status of a service with systemctl.
  2. 2Write a simple custom systemd unit file for a script you want to run as a service.
  3. 3Learn to enable a service to start automatically on boot.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge