InitV and systemd parctical notes

Nicely written reference

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html

Short cheat shit

List services:
# chkconfig --list
# systemctl list-units

This will show any unit that systemd loaded or attempted to load, regardless of its current state on the system.

# systemctl list-units –all

systemctl list-units --all --state=inactive

# systemctl list-units --type=service

Since systemd will only read units that it thinks it needs, this will not necessarily include all of the available units on the system. To see every available unit file within the systemd paths, including those that systemd has not attempted to load

# systemctl list-unit-files

Enable/disable a service:
# chkconfig <servicename> on/off
# systemctl enable/disable <servicename>.service

Start/stop/status a service:
# service <servicename> start/stop/status
# systemctl start/stop/status <servicename>.service

Display dependencies:
# systemctl list-dependencies sshd.service

Show unit properties
# systemctl show sshd.service
Get default target for the system (multi-user.target, graphical.target)
# systemctl get-default

List all available targets:
# systemctl list-unit-files --type=target
List active targets:
# systemctl list-unit-files --type=target

Put system into a single user mode:
# sudo systemctl rescue

Creating new service
Let service name be iperf3

https://scottlinux.com/2014/12/08/how-to-create-a-systemd-service-in-linux-centos-7/

1. Create service description file
/etc/systemd/system/iperf3.service

[Unit]
Description=iperf3 Service
After=network.target

[Service]
Type=simple
User=iperf
ExecStart=/usr/bin/iperf3 -s
Restart=on-abort

[Install]
WantedBy=multi-user.target

Display contents of the unit file:

# systemctl cat iperf3.service

2. Reload systemd
$ sudo systemctl daemon-reload

3. Start new service and check it’s status
$ sudo systemctl start iperf3
[stmiller@ny ~]$ sudo systemctl status iperf3
iperf3.service - iperf3 Service
Loaded: loaded (/etc/systemd/system/iperf3.service; disabled)
Active: active (running) since Mon 2014-12-08 13:43:49 EST; 18s ago
Main PID: 32657 (iperf3)
CGroup: /system.slice/iperf3.service
└─32657 /usr/bin/iperf3 -s

4. Configure startup at boot
$ sudo systemctl enable iperf3

This will create a symbolic link from the system’s copy of the service file (usually in /lib/systemd/system or /etc/systemd/system) into the location on disk where systemd looks for autostart files (usually /etc/systemd/system/some_target.target.wants.

Keep in mind that enabling a service does not start it in the current session. If you wish to start the service and enable it at boot, you will have to issue both the start and enable commands.

Some theory

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

In systemd, the target of most actions are “units”, which are resources that systemd knows how to manage. Units are categorized by the type of resource they represent and they are defined with files known as unit files. The type of each unit can be inferred from the suffix on the end of the file.

For service management tasks, the target unit will be service units, which have unit files with a suffix of .service. However, for most service management commands, you can actually leave off the .service suffix, as systemd is smart enough to know that you probably want to operate on a service when using service management commands.