This page is about service management with the systemd init process.
This page is about the Service unit type of SystemD .service (ie about systemd.service)
It is designed to replace and be backwards compatible with SysV init scripts. Traditional init scripts continue to function on a systemd system. An init script /etc/rc.d/init.d/foobar is implicitly mapped into a service unit foobar.service during system initialization.
In Red Hat Enterprise Linux 7, systemd replaces Upstart as the default init system.
Units are representations of resources that systemd knows about.
A Systemd output may show the following columns
systemctl cat foobar.service
[Unit]
Description=foobar daemon
After=network.target
[Service]
Type=forking
User=infa
; sleep for 5 minutes to ensure headnode is up and running
ExecStartPre=/bin/sleep 300
ExecStart=/usr/bin/daemon startup
ExecStop=/usr/bin/daemon shutdown
TimeoutSec=infinity
TimeoutStartSec=10min 10sec
[Install]
WantedBy=multi-user.target
Example for java with redirections
ExecStart=/bin/sh -c 'exec /bin/java -jar xxx.jar -Xmx512M -Xms32M >> /data/logs/xxx.log 2>&1'
https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
Environment variable possibilities can be seen here: SystemD - Environment Variable for a service
Redirection of the standard stream of the application.
To query it, see SystemD - Journalctl
StandardOutput=file:/home/user/log1.log
StandardError=file:/home/user/log2.log
StandardOutput=append:/home/user/log1.log
StandardError=append:/home/user/log2.log
The command line tool that manage SystemD is called systemctl.
systemctl
systemctl list-units --all --type=service
#-all because list-units command shows only active units by default
# State
systemctl list-units --all --state=inactive
systemctl list-unit-files
systemctl cat foobar.service
systemctl show sshd.service
You cannot used infinity for the timeout everywhere
# all timeout property
systemctl show node.service | grep -i Timeout*
# By property
systemctl show node.service -p TimeoutStartSec
systemctl show node.service -p TimeoutStopSec
systemctl show node.service -p TimeoutSec # A shorthand for configuring both TimeoutStartSec= and TimeoutStopSec= to the specified value
# ''-p --property=NAME'' Show only properties by this name
Example
; 15 min
TimeoutSec=900
systemctl list-dependencies sshd.service
sshd.service
├─system.slice
└─basic.target
├─microcode.service
├─rhel-autorelabel-mark.service
├─rhel-autorelabel.service
├─rhel-configure.service
├─rhel-dmesg.service
├─rhel-loadmodules.service
├─paths.target
├─slices.target
. . .
sudo systemctl start foobar.service
# as systemd knows to look for *.service files for service management commands
sudo systemctl start foobar
Fail If the start fails, systemctl will give you a generic error message:
systemctl start foo.service
Job failed. See system journal and 'systemctl status' for details.
By default:
Next step:
systemctl status foo.service
journalctl --unit foo
systemctl stop foobar.service
sudo systemctl restart foobar.service
# if you don't know
sudo systemctl reload-or-restart application.service
sudo systemctl reload foobar.service
systemctl is-active application.service
systemctl is-failed application.service
Whether the service should start on boot.
sudo systemctl enable foobar.service
# isEnabled
systemctl is-enabled application.service
sudo systemctl disable foobar.service
Ansible - Disable if exists
- name: Populate service facts
service_facts:
- name: Disable firewalld
when: "'firewalld.service' in services"
systemd:
name: firewalld
enabled: no
state: stopped
systemctl status application.service
Example with cron
systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-02-20 14:56:00 UTC; 1 weeks 6 days ago
Main PID: 6339 (crond)
CGroup: /system.slice/crond.service
└─6339 /usr/sbin/crond -n
cp foobar.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable infaservice
systemctl [start|stop|status] foobar