Systemd

https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

https://www.freedesktop.org/software/systemd/man/systemd-analyze.html

#how to remove systemd service

systemctl stop [servicename]
systemctl disable [servicename]
rm /etc/systemd/system/[servicename]
rm /etc/systemd/system/[servicename] # and symlinks that might be related
rm /usr/lib/systemd/system/[servicename] 
rm /usr/lib/systemd/system/[servicename] # and symlinks that might be related
systemctl daemon-reload
systemctl reset-failed

#inside of enable/disable a service
  • sudo systemctl enable application.service
 

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. We will go over what a target is later in this guide).

To disable the service from starting automatically, you can type:

  • sudo systemctl disable application.service
 
 

Create a service

To launch a script at startup a proper service file have to be created and added to the systemd.

Let's create a service file for our blik.py program. The simplest way to create a new service is to use the following command

sudo systemctl --force edit blink.service

that will create a file called blink.service in /etc/systemd/system directory. In any case the exact location of the file will be shown when you edit again the file.

Then save inside it this content:

[Unit]
Description=Blinking led
After=network.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/blink.py
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Next systemd has to be configured, enabling the service so at the next reboot it will be started automatically. First off, though, the systemd itself has to be reloaded in order to detect the new service definition:

sudo systemctl daemon-reload

Next, enable the service

sudo systemctl enable blink

Check if it is enabled:

sudo systemctl list-unit-files | grep enabled

Of course, it can be started manually just now:

sudo systemctl start blink

The PID (process id) can be now checked:

ps -ef | grep blink
pi         817     1  0 09:19 ?        00:00:00 /usr/bin/python blink.py

it is 817 (the number will be different in your case). Please note that it has been started from systemd process, in fact the PPID (parent process id) is 1.

Test what happens if the program crashes and/or terminates.

In order to simulate a crash, we kill it manually:

sudo kill 817

Now we can check it was restarted looking again to the PID (process id):

ps -ef | grep blink
pi        1037     1  0 09:19 ?        00:00:00 /usr/bin/python blink.py

it is now 1037 (PPID always 1), so, as it is different, that means has been restarted automatically after the kill.

How to edit again the service file

sudo systemctl edit blink.service --full

 

 

Create a timer for your service

 

It is possible to use the systemd timers to launch a service instead of using cron

 

Example 1: Run a service every hour

Create a systemd service file

sudo systemctl --force edit blink.service   

with this contents:

[Unit]
Description=Blinking led

[Service]
WorkingDirectory=/home/pi
ExecStart=python blink.py
User=pi

Create a systemd timer file for this service

sudo systemctl --force edit blink.timer

with this contents::

[Unit]
Description=Runs blink.py every hour

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=blink.service

[Install]
WantedBy=multi-user.target

Reload the service definitions:

sudo systemctl daemon-reload

Then is your are using the same file of previous example disable the blink.service and enable just the blink.timer to start it after the bootstrap:

sudo systemctl disable blink.service
sudo systemctl enable blink.timer

Let's try it now:

sudo systemctl start blink.timer

Example 2: Run a service daily

[Unit]
Description=Runs blink.py daily at midnight

[Timer]
OnCalendar=*-*-* 00:00:00
Persistent=True 
Unit=blink.service

[Install]
WantedBy=multi-user.target

Changing the OnCalendar parameter is possible to start the blink.service in a lot of different way:

Minimal form                   Normalized form
Sat,Thu,Mon-Wed,Sat-Sun    ==> Mon-Thu,Sat,Sun *-*-* 00:00:00
Mon,Sun 12-*-* 2,1:23      ==> Mon,Sun 2012-*-* 01,02:23:00
Wed *-1                    ==> Wed *-*-01 00:00:00
Wed-Wed,Wed *-1            ==> Wed *-*-01 00:00:00
Wed, 17:48                 ==> Wed *-*-* 17:48:00
Wed-Sat,Tue 12-10-15 1:2:3 ==> Tue-Sat 2012-10-15 01:02:03
*-*-7 0:0:0                ==> *-*-07 00:00:00
10-15                      ==> *-10-15 00:00:00
monday *-12-* 17:00        ==> Mon *-12-* 17:00:00
Mon,Fri *-*-3,1,2 *:30:45  ==> Mon,Fri *-*-01,02,03 *:30:45
12,14,13,12:20,10,30       ==> *-*-* 12,13,14:10,20,30:00
mon,fri *-1/2-1,3 *:30:45  ==> Mon,Fri *-01/2-01,03 *:30:45
03-05 08:05:40             ==> *-03-05 08:05:40
08:05:40                   ==> *-*-* 08:05:40
05:40                      ==> *-*-* 05:40:00
Sat,Sun 12-05 08:05:40     ==> Sat,Sun *-12-05 08:05:40
Sat,Sun 08:05:40           ==> Sat,Sun *-*-* 08:05:40
2003-03-05 05:40           ==> 2003-03-05 05:40:00
2003-03-05                 ==> 2003-03-05 00:00:00
03-05                      ==> *-03-05 00:00:00
hourly                     ==> *-*-* *:00:00
daily                      ==> *-*-* 00:00:00
monthly                    ==> *-*-01 00:00:00
weekly                     ==> Mon *-*-* 00:00:00
*:20/15                    ==> *-*-* *:20/15:00

 

 

 

This will remove the symbolic link that indicated that the service should be started automatically.

(RT) 

systemd is a new init system and system manager that has become so popular that it has been widely transformed into the new standard init system by most Linux distributions.

 Systemctl is a systemd application that allows you to manage the systemd system.
 

 

 

systemd Features:

  • systemd provides aggressive parallelization capabilities
  • Uses socket and D-Bus activation for starting services
  • Offers on-demand starting of daemons
  • Keeps track of processes using Linux cgroups
  • Supports snapshotting and restoring of the system state
  • Maintains mount and automount points
  • Implements an elaborate transactional dependency-based service control logic

Service Commands:

These are the most commonly used service commands on a Linux system to manage services.

Short Description SysVinit Command systemd Command
To Start a Service service example start systemctl start example
To Stop a Service service example stop systemctl stop example
Stop and then Start a Service (Restart a Service) service example restart systemctl restart example
Reload a Service (Reload the config file) service example reload systemctl reload example
Restarts if the service is already running service example condrestart systemctl condrestart example
How to check if a service is currently running service example status systemctl status example
How to enable a service on boot/startup chkconfig example on systemctl enable example
How to disable a service on boot/startup chkconfig example off systemctl disable example
How to check if a service is configured to start on boot or not chkconfig example –list systemctl is-enabled example
How to display a list of enabled or disabled services on boot with runlevels information chkconfig systemctl list-unit-files –type=service
Create a new service file or modify any configuration chkconfig example –add systemctl daemon-reload

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Runlevels/Targets:

The systemd has a concept of targets, which serves a similar purpose as runlevels, but operates slightly differently. Each target is named instead of numbers and is intended to serve a specific purpose.

 
Short Description SysVinit Command systemd Command
To halt the system 0, halt runlevel0.target, poweroff.target, systemctl halt
Single user mode 1, S, single runlevel1.target, rescue.target
Multi User 2 runlevel2.target, multi-user.target
Multi User with Network 3 runlevel3.target, multi-user.target
Experimental (No User) 4 runlevel4.target, multi-user.target
Multi-user with Graphical & Network 5 runlevel5.target, graphical.target
To reboot a system 6, reboot runlevel6.target, reboot.target, systemctl reboot
Emergency shell emergency emergency.target

 

 

 

 

 

 

 

 

 

 

Other systemd Commands:

There are other commands in systemd that are very useful.

 

Short Description systemd Command
Control the system hostname hostnamectl
Team daemon control tool teamdctl
Control the system time and date timedatectl
Query the systemd journal journalctl
How to check system boot time systemd-analyze or systemd-analyze time
How to check each service time consumption at startup systemd-analyze blame
Kill all processes associated with a service systemctl kill example
List all running services systemctl
Show system status systemctl status
How to run the systemd command on a remote machine systemctl status example -H user@remotehost

posted on 2020-03-30 14:45  荷树栋  阅读(172)  评论(0编辑  收藏  举报

导航