System Services and Configuration
Modern Linux distributions use an "init system" to control system operations, primarily systemd. Systemd manages services, boot processes, networking, and more.
Controlling Services and Units
systemctl is the primary tool for managing services and units in systemd. Units can represent services, device drivers, network mounts, timers (similar to crontab), and more.
Common systemctl Commands:
systemctl status servicename- View the current status of a service.systemctl start servicename- Start a service.systemctl stop servicename- Stop a service.systemctl restart servicename- Restart a service (stop and start).systemctl reload servicename- Reload the configuration file without restarting the service.systemctl enable servicename- Enable a service to start at system boot.systemctl is-enabled servicename- Check if a service is enabled at startup.systemctl is-active servicename- Check if a service is running and active.systemctl list-units- List all running systemd units.systemctl list-units --all- List all units, both active and inactive.systemctl list-units --all --state=inactive- List all inactive units.systemctl list-units --all --type=service- List all units of type "service."
Unit File Locations
Systemd unit files are typically located in the following directories:
/usr/lib/systemd- The main location for system-created unit files./etc/systemd/system- Contains system-wide unit files, often symbolic links to/usr/lib/systemd. This directory has top priority when reading unit files.~/.config/systemd/user/- Contains user-specific unit files. This directory is not created by default and requires the--useroption for systemctl commands.
Example System Unit File
Below is an example of a system unit file:
[Unit]
Description=service_description
After=network.target
[Service]
ExecStart=path_to_executable
Type=forking
[Install]
WantedBy=default.target
For a full reference, Systemd Unit Documentation https://links.thelinuxbook.com/systemd.
User-Based Systemd Services and Unit Files
User-specific unit files are stored in the ~/.config/systemd/user/ directory. These files can be managed using the --user option with systemctl.
Example Command:
systemctl --user start usercreatedfile.service
This command starts a user-created service file located in the user's home directory.
Example User Unit File:
[Unit]
Description=Run service as user
DefaultDependencies=no
After=network.target
[Service]
Type=simple
User=titus
Group=users
ExecStart=/home/titus/scripts/startup_script.sh
TimeoutStartSec=0
RemainAfterExit=yes
[Install]
WantedBy=default.target