systemd service unit notes

In this tutorial, The describe systemd service unit notes.

  • systemd is a system and service manager for Linux.
  • init is no longer an old Linux.
  • Only the systemctl command can be used for starting and stopping.

systemd service unit file

  • It is arranged under “/etc/systemd/system” with “unitname.service
  • Others include directories like “/usr/lib/systemd” and “/lib/systemd“, but rather for systems.

Service section

Describe the operation parameters of the unit.

Type

The relationship between the execution command and the main process is represented by Type.

  • simple: ExecStart command remains as it is main process
  • forking: Child process of ExecStart is main process
  • oneshot: Even if ExecStart ends, the main process will remain.

Restart

Restart conditions can be specified with parameters equivalent to respawn in the old inittab.

  • always: always rerun
  • on-abort: re-execute when terminating with a signal that can not be caught
  • on-watchdog: Rerun with monitor timeout
  • on-abnormal: Re-execution when terminating with a signal other than SIGHUP, SIGINT, SIGTERM or SIGPIPE
  • on-failure: re-execution when the main process ends with a code other than the normal termination code
  • on-success: Rerun when the main process ends with a normal exit code

Install section

Specify which target (old runlevel) should be executed when the unit is activated (sytemctl enable).

For example, when enabling in multi-user mode (old run level 3), a symbolic link to a unit file is established under “/etc/systemd/system/multi-user.target“.

[Unit]
Description=Description
After=Execute after starting the specified unit list
Before=Execute before the specified unit list
Requires=Execute after the specified unit list has been successfully started
Wants=Even if the specified unit list fails to start up

[Service]
Environment=environment variable list
EnvironmentFile=environment variable file
Type=simple|forking|oneshot
ExecStart=start command
ExecStop=stop command
ExecReload=reload command
Restart=On-abort|on-watchdog|on-abnormal|on-failure|on-success|no
RemainAfterExit=yes|no
PIDFile=PID file path of main process
User=ExecXX execution user
SuccessExitStatus=(other than 0) EXIT code list to be regarded as a normal completion of the main process

[Install]
Alias=service alias list
WantedBy=target list
Also=unit list installed together

used commands

Unit file installation (loading)

$ sudo systemctl daemon-reload

Enable/disable

$ systemctl enable|disable unitname

Dependency display

$ systemctl list-dependencies unitname

For example, List Dependency of chronyd package.

systemd service unit notes

Confirm startup sequence

Summary of time spent on startup (kernel, initrd, user)

$ systemd-analyze time

Time is taken to activate unit (currently active unit)

$ systemd-analyze blame

SVG output startup sequence

$ systemd-analyze plot > systemd.sequence.svg 

Confirm log

$ journalctl -xe 
$ journalctl -xe -S "2019-01-22 23:00:00"  
$ journalctl -xe -S "2019-01-22 23:00:00" -U "2019-01-22 23:10:00" 
$ journalctl -xe -u unitname
$ journalctl -f -u servicename 
$ journalctl --disk-usage

$ lsof -p $(pidof systemd-journald)

Reference information

  • man systemd.service
  • man systemd-analyze

unit file example

Apache system (fork daemon)

myhttpd.service unit file

[Unit]
Description=HTTP Server
After=web.service

[Service]
Type=forking
ExecStart=/opt/bin/apachectl start
ExecStop=/opt/bin/apachectl graceful-stop
PIDFile=/opt/logs/httpd.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target

web.service unit file

[Unit]
Description=WebSphere Application Server apserver
After=network.target network.service

[Service]
Type=forking
ExecStart=/opt/AppServer/profiles/AppSrv01/bin/startServer.sh apserver
ExecStop=/opt/AppServer/profiles/AppSrv01/bin/stopServer.sh apserver
PIDFile=/opt/AppServer/profiles/AppSrv01/logs/apserver/apserver.pid
Restart=on-failure
User=webuser
SuccessExitStatus=143 0
TimeoutStopSec=0
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

systemd service unit notes. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.