systemctlStart, stop, enable and inspect systemd services
systemctl controls systemd, the init system and service manager on almost every modern distribution. You use it to start, stop, restart and reload services, make them start at boot, see why one failed, and manage the unit files that describe them.
systemctl [OPTIONS] COMMAND [UNIT]...Someone says 'nginx is down'. Before touching anything, read its status.
sudoChanging system services needs root (or a polkit rule). status and is-active don't.
systemd runs as PID 1 and manages units: services, sockets, timers, mounts, targets. Unit files live in /usr/lib/systemd/system (from packages), /etc/systemd/system (your overrides, which win) and /run. systemctl doesn't start anything itself; it sends a D-Bus call to PID 1, which queues a job. For a service start, systemd creates a cgroup (/system.slice/nginx.service), forks, applies the sandboxing and limits in the unit (User=, LimitNOFILE=, ProtectSystem=), connects stdout and stderr to journald, runs ExecStartPre= and then ExecStart=. It tracks the main PID, and every child stays in the cgroup, so stop kills them all. Type= decides when the start counts as done: simple (right after fork), notify (the service signals READY=1) or forking. Restart= makes systemd restart crashed services, rate-limited by StartLimitBurst. enable only creates symlinks under *.wants/; daemon-reload makes PID 1 re-read changed unit files.
systemctl only sends a request; PID 1 does the work: cgroup, user, exec, log wiring, then it watches the process.
systemctl doesn't run the service. It sends a StartUnit call over D-Bus to systemd (PID 1), which checks you're allowed (root or polkit) and queues a start job.
status UNITState, main PID, cgroup, memory and the last log linesstart / stop / restart UNITRun, stop, or stop-then-start nowreload UNITAsk the service to reload config without a restart (ExecReload=)enable / disable UNITStart / don't start at boot (add --now to act immediately)is-active / is-enabled / is-failedOne-word answer plus exit code, for scripts--failedList units in the failed statelist-units --type=serviceLoaded services and their stateslist-timersScheduled timers: next and last runcat UNITPrint the unit file and every drop-in in effectedit UNITCreate a drop-in override (edit --full for a full copy)daemon-reloadRe-read unit files after you changed themshow UNIT -p PROPMachine-readable properties (NRestarts, MainPID...)mask / unmask UNITMake a unit impossible to start (links it to /dev/null)reset-failed UNITClear the failed state and the start-rate counterEditing a unit file and only running systemctl restartsystemd still uses the copy it parsed earlier; your change isn't applied (it prints a warning).
sudo systemctl daemon-reload && sudo systemctl restart appEditing /usr/lib/systemd/system/nginx.service directlyThe next package upgrade overwrites it without warning.
sudo systemctl edit nginx # drop-in under /etc/systemd/system/nginx.service.d/systemctl enable nginx and assuming it's runningenable only affects boot. The service stays inactive until started.
sudo systemctl enable --now nginxExecStart=/opt/app/start.sh & or a script that daemonizesWith Type=simple, systemd tracks the process it started. If it forks into the background and exits, systemd thinks the service died and kills the rest of the cgroup.
Run the app in the foreground: ExecStart=/usr/bin/node server.jsUsing restart for every config changeA restart drops in-flight connections; many daemons can reload gracefully.
nginx -t && sudo systemctl reload nginxTry these on a Linux machine of your own. Each task has a hint and a solution.
The sandbox is a container without systemd. Write the command that starts nginx now and at every boot.
Write a unit file for ~/app/worker/report.py that runs as user devops, restarts on failure after 10 seconds and starts at boot. Save it as ~/report.service.
In a health-check script, print 'down' and exit 1 if nginx isn't running. Write the line.
Read this: 'Active: activating (auto-restart) (Result: exit-code)'. What is the service doing?
systemctlStart, stop, enable and inspect systemd services
systemctl controls systemd, the init system and service manager on almost every modern distribution. You use it to start, stop, restart and reload services, make them start at boot, see why one failed, and manage the unit files that describe them.
systemctl [OPTIONS] COMMAND [UNIT]...Someone says 'nginx is down'. Before touching anything, read its status.
sudoChanging system services needs root (or a polkit rule). status and is-active don't.
systemd runs as PID 1 and manages units: services, sockets, timers, mounts, targets. Unit files live in /usr/lib/systemd/system (from packages), /etc/systemd/system (your overrides, which win) and /run. systemctl doesn't start anything itself; it sends a D-Bus call to PID 1, which queues a job. For a service start, systemd creates a cgroup (/system.slice/nginx.service), forks, applies the sandboxing and limits in the unit (User=, LimitNOFILE=, ProtectSystem=), connects stdout and stderr to journald, runs ExecStartPre= and then ExecStart=. It tracks the main PID, and every child stays in the cgroup, so stop kills them all. Type= decides when the start counts as done: simple (right after fork), notify (the service signals READY=1) or forking. Restart= makes systemd restart crashed services, rate-limited by StartLimitBurst. enable only creates symlinks under *.wants/; daemon-reload makes PID 1 re-read changed unit files.
systemctl only sends a request; PID 1 does the work: cgroup, user, exec, log wiring, then it watches the process.
systemctl doesn't run the service. It sends a StartUnit call over D-Bus to systemd (PID 1), which checks you're allowed (root or polkit) and queues a start job.
status UNITState, main PID, cgroup, memory and the last log linesstart / stop / restart UNITRun, stop, or stop-then-start nowreload UNITAsk the service to reload config without a restart (ExecReload=)enable / disable UNITStart / don't start at boot (add --now to act immediately)is-active / is-enabled / is-failedOne-word answer plus exit code, for scripts--failedList units in the failed statelist-units --type=serviceLoaded services and their stateslist-timersScheduled timers: next and last runcat UNITPrint the unit file and every drop-in in effectedit UNITCreate a drop-in override (edit --full for a full copy)daemon-reloadRe-read unit files after you changed themshow UNIT -p PROPMachine-readable properties (NRestarts, MainPID...)mask / unmask UNITMake a unit impossible to start (links it to /dev/null)reset-failed UNITClear the failed state and the start-rate counterEditing a unit file and only running systemctl restartsystemd still uses the copy it parsed earlier; your change isn't applied (it prints a warning).
sudo systemctl daemon-reload && sudo systemctl restart appEditing /usr/lib/systemd/system/nginx.service directlyThe next package upgrade overwrites it without warning.
sudo systemctl edit nginx # drop-in under /etc/systemd/system/nginx.service.d/systemctl enable nginx and assuming it's runningenable only affects boot. The service stays inactive until started.
sudo systemctl enable --now nginxExecStart=/opt/app/start.sh & or a script that daemonizesWith Type=simple, systemd tracks the process it started. If it forks into the background and exits, systemd thinks the service died and kills the rest of the cgroup.
Run the app in the foreground: ExecStart=/usr/bin/node server.jsUsing restart for every config changeA restart drops in-flight connections; many daemons can reload gracefully.
nginx -t && sudo systemctl reload nginxTry these on a Linux machine of your own. Each task has a hint and a solution.
The sandbox is a container without systemd. Write the command that starts nginx now and at every boot.
Write a unit file for ~/app/worker/report.py that runs as user devops, restarts on failure after 10 seconds and starts at boot. Save it as ~/report.service.
In a health-check script, print 'down' and exit 1 if nginx isn't running. Write the line.
Read this: 'Active: activating (auto-restart) (Result: exit-code)'. What is the service doing?