Processes, Services & systemd

See what's running with ps and top, stop stuck programs, and turn your app into a systemd service that starts on boot, restarts after a crash, and logs to journald.

Beginner⏱ 6 min readLesson 4 of 17#linux#systemd#processes#journalctl#services

The big idea

If you start your app with npm start in an SSH window, it dies the moment you close that window. If it crashes at 3 a.m., it stays dead until you wake up. If the server reboots, nothing starts again.

systemd is Linux's building manager. You tell it once "keep this app running", and it:

  • starts the app when the server boots,
  • restarts it if it crashes,
  • collects its logs,
  • lets you start, stop and check it with one command.

Processes: what's running?

A process is a running program. Each one has a PID (process ID).

ps aux                        # every process
ps aux | grep node            # find Node processes
pgrep -a node                 # same, shorter
top                           # live view, sorted by CPU (q to quit)
htop                          # nicer live view (sudo apt install htop)
Column in top / psMeaning
PIDProcess ID
USERWho runs it (should not be root for your app)
%CPU / %MEMResource use
RESReal memory used
COMMANDWhat it is

Load average (uptime, top right of top): average number of processes waiting for CPU over 1, 5 and 15 minutes. On a 4-core server, a load above 4 means work is queuing.

Stopping processes: signals

kill 12345          # politely ask PID 12345 to stop (SIGTERM, signal 15)
kill -9 12345       # force kill (SIGKILL): last resort, no cleanup
pkill -f "node server.js"
SignalNumberWhat it does
SIGTERM15"Please stop": the app can finish requests and close connections
SIGINT2Same as pressing Ctrl+C
SIGKILL9Instant death: no cleanup, possible data loss
SIGHUP1Often means "reload your config" (Nginx)

💡 Your app should handle SIGTERM gracefully: stop accepting requests, finish the ones in progress, close database connections, then exit. systemd, Docker and Kubernetes all send SIGTERM first.

Which process is using a port?

sudo ss -ltnp                 # every listening TCP port and its process ⭐
sudo ss -ltnp | grep ':3000'  # who is on port 3000?
sudo lsof -i :3000            # alternative

Error: listen EADDRINUSE :::3000 means another process already has that port. Find it with ss, then decide whether to stop it or use another port.

systemd: turning your app into a service

systemd starts the app on boot, restarts it after a crash, sends SIGTERM on stop, and sends its output to journald

Step 1: write a unit file

Create /etc/systemd/system/myapp.service:

[Unit]
Description=My Next.js app
After=network.target
# Wait for the database if it runs on the same server:
# After=network.target postgresql.service

[Service]
Type=simple
User=deploy
Group=deploy
WorkingDirectory=/var/www/myapp
Environment=NODE_ENV=production
Environment=PORT=3000
EnvironmentFile=/var/www/myapp/.env
ExecStart=/usr/bin/npm start
Restart=always
RestartSec=5
# Give the app time to finish requests on stop
TimeoutStopSec=20
# Basic hardening
NoNewPrivileges=true
PrivateTmp=true

[Install]
WantedBy=multi-user.target
LineMeaning
After=network.targetStart once networking is up
User=deployRun as a normal user, not root
WorkingDirectoryLike cd before starting
EnvironmentFileLoad KEY=value lines (secrets) from a file with chmod 600
ExecStartThe command to run. Use full paths (which npm shows them)
Restart=always + RestartSec=5Restart 5 s after any exit or crash
WantedBy=multi-user.targetStart automatically on boot (after enable)

⚠️ If you installed Node with nvm, /usr/bin/npm doesn't exist. Use the full path from which npm (for example /home/deploy/.nvm/versions/node/v22.11.0/bin/npm), or install Node system-wide from NodeSource so the path stays stable across upgrades.

Step 2: load, start and enable

sudo systemctl daemon-reload          # re-read unit files after any change ⭐
sudo systemctl start myapp            # start now
sudo systemctl enable myapp           # start on every boot
sudo systemctl enable --now myapp     # both at once
sudo systemctl status myapp           # is it running? last log lines

Everyday commands

CommandWhat it does
systemctl status myappRunning? Since when? Recent logs
systemctl restart myappStop + start (after a deploy)
systemctl reload nginxRe-read config without dropping connections (if supported)
systemctl stop myappStop it
systemctl disable myappDon't start on boot
systemctl is-active myappPrints active / failed, handy in scripts
systemctl list-units --type=service --state=failedEverything that's broken

Reading status:

● myapp.service - My Next.js app
     Loaded: loaded (/etc/systemd/system/myapp.service; enabled)
     Active: active (running) since Sat 2026-11-15 10:04:12 UTC; 2h ago
   Main PID: 48211 (npm start)
     Memory: 182.4M

enabled = starts on boot. active (running) = alive now. If you see failed or activating (auto-restart), check the logs.

Logs with journalctl

systemd captures everything your app prints to stdout and stderr (console.log, console.error).

journalctl -u myapp                 # all logs for the service
journalctl -u myapp -f              # follow live ⭐
journalctl -u myapp -n 100          # last 100 lines
journalctl -u myapp --since "1 hour ago"
journalctl -u myapp --since today -p err   # only errors
journalctl -b                       # everything since the last boot
journalctl --disk-usage             # how much space logs use

Scheduled jobs with systemd timers

Timers are systemd's modern alternative to cron (the next lesson covers cron too):

# /etc/systemd/system/backup.service
[Service]
Type=oneshot
User=deploy
ExecStart=/home/deploy/bin/backup.sh

# /etc/systemd/system/backup.timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl enable --now backup.timer
systemctl list-timers

Timers get logs in journalctl -u backup and run a missed job after downtime (Persistent=true).

What about PM2?

PM2 is a popular Node.js process manager with similar features (restart, logs, cluster mode). It's fine, but on a Linux server systemd is already there, is used for every other service, and integrates with the system logs. Pick one; don't run PM2 inside systemd and have systemd restart it too, unless you know why.

Common problems

SymptomLikely causeCheck
status=203/EXECExecStart path is wrongwhich npm, use the full path
status=200/CHDIRWorkingDirectory doesn't existls -ld /var/www/myapp
Keeps restartingApp crashes on start (missing env var, port in use)journalctl -u myapp -n 50
Changes to the unit ignoredForgot to reloadsudo systemctl daemon-reload
Works by hand, fails in systemdDifferent user, PATH or env varsCompare env with the unit's Environment=
EADDRINUSEPort already takensudo ss -ltnp | grep :3000

Key takeaways

  • ps, top/htop and ss -ltnp show what's running and on which port.
  • Stop with kill (SIGTERM) first; kill -9 only as a last resort.
  • A systemd unit keeps your app running, restarts it on crash, and starts it on boot.
  • After editing a unit: daemon-reload, then restart. Check with status.
  • Read logs with journalctl -u myapp -f.

Try it yourself

Write a tiny Node server, create a systemd unit for it with Restart=always, enable it, then run sudo kill -9 <PID> and watch systemctl status bring it back within 5 seconds.