The systemctl command
Control the systemd system and service manager 28 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.
Options in the examples
--now- When used with enable, disable, mask, or reenable, also start/stop/try-restart the units after the specified unit file operations succeed.
Subcommands in the examples
systemctl status- Show runtime status information about the whole system or about one or more units followed by most recent log data from the journal.
systemctl restart- Stop and then start one or more units specified on the command line.
systemctl enable- Enable one or more units or unit instances.
systemctl reload- Asks all units listed on the command line to reload their configuration.
systemctl daemon-reload- Reload the systemd manager configuration.
systemctl disable- Disables one or more units.
systemctl is-active- Check whether any of the specified units are active (i.e. running).
systemctl is-enabled- Checks whether any of the specified unit files are enabled (as with enable).
systemctl list-units- List units that systemd currently has in memory.
systemctl start- Start (activate) one or more units specified on the command line.
systemctl stop- Stop (deactivate) one or more units specified on the command line.
systemctl cat- Show backing files of one or more units.
systemctl list-unit-files- List unit files installed on the system, in combination with their enablement state (as reported by is-enabled).
systemctl mask- Mask one or more units, as specified on the command line.
Examples
systemctl status nginx
systemctl status sshd
systemctl list-units --type=service
systemctl list-units --type=service --state=running
systemctl list-unit-files --type=service
systemctl is-enabled nginx
systemctl is-active nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginxStop then start
sudo systemctl reload nginxReload config without dropping connections
sudo systemctl enable nginxStart automatically on boot
sudo systemctl disable nginxDon't start on boot (doesn't stop it now)
sudo systemctl enable --now nginxEnable AND start immediately
sudo systemctl mask nginxPrevents it from being started at all, even manually
sudo systemctl unmask nginxAllows it again
sudo systemctl daemon-reload
sudo systemctl enable --now myappEnable + start
systemctl status myappVerify
systemctl status worker"activating (auto-restart)" = a crash loop
systemctl cat workerThe unit file: what runs, and as which user?
sudo systemctl restart worker
systemctl status PIDWhich service started that process?
sudo systemctl stop SERVICEFree port 80
sudo systemctl disable SERVICE…and keep it free after a reboot
systemctl status shop-apiIs the app running at all?
sudo nginx -t && sudo systemctl reload nginxafter a change to nginx's configuration
sudo systemctl restart shop-apiafter a change to the app's configuration
Lessons that use systemctl
- systemctl — Checking Services
- Starting, Stopping & Enabling Services
- Writing a Service Unit File
- nginx — Your First Web Server
- Configuration & Server Blocks
- Mission: Service Down
- Mission: The Upgrade Window
- Mission: One Missing Character
- Mission: The Service That Won't Stay Up
- Mission: Port 80 Is Taken
- Mission: 502 Bad Gateway