Tuesday, February 2, 2016

HOWTO - Manage system services on Raspbian Jessie


List defined services

Print active services and their corresponding states in human readable form.
$ sudo systemctl list-units -t service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
acpid.service                      loaded active running ACPI event daemon
atd.service                        loaded active running Deferred execution scheduler
console-setup.service              loaded active exited  LSB: Set console font and keymap
cron.service                       loaded active running Regular background program processing daemon
dbus.service                       loaded active running D-Bus System Message Bus
exim4.service                      loaded active running LSB: exim Mail Transport Agent
[...]

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

31 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
Print active services and their corresponding states without legend/headers.
$ sudo systemctl list-units -t service --no-legend
acpid.service                      loaded active running ACPI event daemon
atd.service                        loaded active running Deferred execution scheduler
console-setup.service              loaded active exited  LSB: Set console font and keymap
cron.service                       loaded active running Regular background program processing daemon
dbus.service                       loaded active running D-Bus System Message Bus
exim4.service                      loaded active running LSB: exim Mail Transport Agent
[...]
Print all services and their corresponding states.
$ sudo systemctl list-units -t service --no-legend --all 
acpid.service                        loaded    active   running ACPI event daemon
atd.service                          loaded    active   running Deferred execution scheduler
auditd.service                       not-found inactive dead    auditd.service
clamav-daemon.service                not-found inactive dead    clamav-daemon.service
console-screen.service               not-found inactive dead    console-screen.service
console-setup.service                loaded    active   exited  LSB: Set console font and keymap
cron.service                         loaded    active   running Regular background program processing daemon
dbus.service                         loaded    active   running D-Bus System Message Bus
debian-fixup.service                 loaded    inactive dead    Various fixups to make systemd work better on Debian
display-manager.service              not-found inactive dead    display-manager.service
emergency.service                    loaded    inactive dead    Emergency Shell
exim4.service                        loaded    active   running LSB: exim Mail Transport Agent
[...]

Manage services on running system

Print status of single service.
$ sudo systemctl status cron
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled)
   Active: active (running) since sob 2015-03-28 12:41:18 CET; 5h 46min ago
     Docs: man:cron(8)
 Main PID: 373 (cron)
   CGroup: /system.slice/cron.service
           └─373 /usr/sbin/cron -f

mar 28 14:17:01 debian CRON[1187]: pam_unix(cron:session): session opened for user root by (uid=0)
mar 28 14:17:01 debian CRON[1188]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
mar 28 15:17:01 debian CRON[1318]: pam_unix(cron:session): session opened for user root by (uid=0)
mar 28 15:17:01 debian CRON[1319]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
mar 28 16:17:01 debian CRON[1336]: pam_unix(cron:session): session opened for user root by (uid=0)
mar 28 16:17:01 debian CRON[1337]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
mar 28 17:17:01 debian CRON[1542]: pam_unix(cron:session): session opened for user root by (uid=0)
mar 28 17:17:02 debian CRON[1543]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
mar 28 18:17:01 debian CRON[1624]: pam_unix(cron:session): session opened for user root by (uid=0)
mar 28 18:17:01 debian CRON[1625]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Check if service is active.
$ systemctl is-active --quiet atd; \
  if [ "$?" -eq "0" ]; then echo "service is active"; fi
service is active
Check if service failed.
$ systemctl is-failed --quiet atd; \
  if [ "$?" -eq "0" ]; then echo "service failed"; fi
Start service.
$ sudo systemctl start ssh
Stop service.
$ sudo systemctl stop ssh
Reload service's configuration.
$ sudo systemctl reload ssh
Restart service.
$ sudo systemctl restart ssh
Restart service if it is running.
$ sudo systemctl try-restart ssh
Reload service if it supports it, restart otherwise. Start if it is not running.
$ sudo systemctl  reload-or-restart ssh
Reload service if it supports it, restart otherwise.
$ sudo systemctl reload-or-try-restart ssh
Send SIGTERM signal to service.
$ sudo systemctl kill ssh
Send SIGHUP signal to main process of specified service.
$ sudo systemctl kill --signal=HUP --kill-who=main ssh

Manage startup services

Enable service.
$ sudo systemctl enable ssh
Disable service.
$ sudo systemctl disable ssh
Check if specified service is enabled.
$ systemctl is-enabled --quiet cron; \
  if [ "$?" -eq "0" ]; then echo "service is enabled"; fi
Mask service so it cannot be activated at boot time or by manual action.
$ sudo systemctl mask ssh
Unmask service.
$ sudo systemctl unmask ssh

No comments:

Post a Comment