CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Packages & Services / systemctl

systemctl

Start, stop, enable and inspect systemd services

Beginneressentialservicessystemd

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.

Syntaxsystemctl [OPTIONS] COMMAND [UNIT]...

Watch it run

Someone says 'nginx is down'. Before touching anything, read its status.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
systemctl status nginx

Status combines the unit's state with its last log lines. Lines wider than the terminal are cut off with >; add -l (or --no-pager -l) to see them in full.

Anatomy of a command

$
commandsudo

Changing system services needs root (or a polkit rule). status and is-active don't.

How it works

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.

sudoStartUnitloaded configcreatefork + execlogsSIGCHLDenableYousystemctl startsystemctlclientsystemdPID 1Unit fileorders-api.svccgroupsystem.sliceMain processnode server.jsjournaldstdout/stderrwants/ symlinkenable = boot
1/7
systemctl asks PID 1

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.

← → to step

Options you'll actually use

status UNITState, main PID, cgroup, memory and the last log lines
start / stop / restart UNITRun, stop, or stop-then-start now
reload 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 state
list-units --type=serviceLoaded services and their states
list-timersScheduled timers: next and last run
cat UNITPrint the unit file and every drop-in in effect
edit UNITCreate a drop-in override (edit --full for a full copy)
daemon-reloadRe-read unit files after you changed them
show 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 counter

Common mistakes

Editing a unit file and only running systemctl restart

systemd still uses the copy it parsed earlier; your change isn't applied (it prints a warning).

sudo systemctl daemon-reload && sudo systemctl restart app
Editing /usr/lib/systemd/system/nginx.service directly

The 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 running

enable only affects boot. The service stays inactive until started.

sudo systemctl enable --now nginx
ExecStart=/opt/app/start.sh & or a script that daemonizes

With 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.js
Using restart for every config change

A restart drops in-flight connections; many daemons can reload gracefully.

nginx -t && sudo systemctl reload nginx

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    The sandbox is a container without systemd. Write the command that starts nginx now and at every boot.

  2. 2

    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.

  3. 3

    In a health-check script, print 'down' and exit 1 if nginx isn't running. Write the line.

  4. 4

    Read this: 'Active: activating (auto-restart) (Result: exit-code)'. What is the service doing?

Related commands

journalctlQuery systemd's journal: service, boot and kernel logspsSnapshot the running processeskillSend a signal to a processcrontabSchedule recurring jobs with cron
PreviousdpkgNextjournalctl
Linux / Packages & Services / systemctl

systemctl

Start, stop, enable and inspect systemd services

Beginneressentialservicessystemd

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.

Syntaxsystemctl [OPTIONS] COMMAND [UNIT]...

Watch it run

Someone says 'nginx is down'. Before touching anything, read its status.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
systemctl status nginx

Status combines the unit's state with its last log lines. Lines wider than the terminal are cut off with >; add -l (or --no-pager -l) to see them in full.

Anatomy of a command

$
commandsudo

Changing system services needs root (or a polkit rule). status and is-active don't.

How it works

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.

sudoStartUnitloaded configcreatefork + execlogsSIGCHLDenableYousystemctl startsystemctlclientsystemdPID 1Unit fileorders-api.svccgroupsystem.sliceMain processnode server.jsjournaldstdout/stderrwants/ symlinkenable = boot
1/7
systemctl asks PID 1

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.

← → to step

Options you'll actually use

status UNITState, main PID, cgroup, memory and the last log lines
start / stop / restart UNITRun, stop, or stop-then-start now
reload 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 state
list-units --type=serviceLoaded services and their states
list-timersScheduled timers: next and last run
cat UNITPrint the unit file and every drop-in in effect
edit UNITCreate a drop-in override (edit --full for a full copy)
daemon-reloadRe-read unit files after you changed them
show 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 counter

Common mistakes

Editing a unit file and only running systemctl restart

systemd still uses the copy it parsed earlier; your change isn't applied (it prints a warning).

sudo systemctl daemon-reload && sudo systemctl restart app
Editing /usr/lib/systemd/system/nginx.service directly

The 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 running

enable only affects boot. The service stays inactive until started.

sudo systemctl enable --now nginx
ExecStart=/opt/app/start.sh & or a script that daemonizes

With 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.js
Using restart for every config change

A restart drops in-flight connections; many daemons can reload gracefully.

nginx -t && sudo systemctl reload nginx

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    The sandbox is a container without systemd. Write the command that starts nginx now and at every boot.

  2. 2

    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.

  3. 3

    In a health-check script, print 'down' and exit 1 if nginx isn't running. Write the line.

  4. 4

    Read this: 'Active: activating (auto-restart) (Result: exit-code)'. What is the service doing?

Related commands

journalctlQuery systemd's journal: service, boot and kernel logspsSnapshot the running processeskillSend a signal to a processcrontabSchedule recurring jobs with cron
PreviousdpkgNextjournalctl