CodePark/ questions / Linux commands & permissions
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › Linux commands & permissions

Your Node API runs as a systemd service called api. How do you check whether it's running, read its logs, and restart it?

Use systemctl to control the service and journalctl to read its logs.

Control the service:

  • systemctl status api shows if the unit is active, its main PID, its memory use and its latest log lines.
  • sudo systemctl restart api restarts it. stop stops it and start starts it.
  • sudo systemctl enable api makes it start when the machine boots.
  • After you edit the unit file, run sudo systemctl daemon-reload first.

systemd saves the service's stdout and stderr in the journal (its log store). Read the logs:

  • journalctl -u api shows the service's logs.
  • -f follows new lines live, like tail -f.
  • -n 100 shows the last 100 lines.
  • --since "10 min ago" limits the time window.
  • -b shows only logs from the current boot.

If the service keeps dying, systemctl status shows the exit code or the signal that killed it. journalctl -k shows kernel messages, such as OOM kills (the kernel killing a process because memory ran out).

Useful unit settings for a Node app:

  • Restart=on-failure
  • User=
  • Environment=
  • LimitNOFILE=
  • systemctl has status, start, stop, restart and enable.
  • Run daemon-reload after you edit a unit file.
  • journalctl -u <unit> shows a service's logs, with -f, -n and --since to filter.
  • journalctl -k shows kernel messages, such as OOM kills.