0

我已经编写了一个“systemd 的服务单元”来运行我多年来一直在运行的 bash 脚本,没有任何问题,使用“/etc/rc.local”多年。除了以下问题外,'systemd 的服务运行良好:我的 bash 脚本每 5 分钟运行一次,运行以下几行:

nice --adjustment=$CommonNicePriority su root --command="HOME=$COMMON_DIR $0 $CyclesOption" # System common tasks.
for User in $Users # Run logged in users' tasks.
  do
    nice --adjustment=$UsersNicePriority su $User --command="$0 $CyclesOption"
  done

如您所见,它生成一个进程,以“root”(第一行)身份运行,然后每个登录的普通用户(循环)作为(普通)用户运行一个进程。上述每个进程都可以产生一些其他(任务)进程。以“root”身份运行的进程可以正常工作,但为普通用户运行的进程会被“systemd”杀死。这些是来自“/var/log/syslog”的行:

Feb  2 10:35:00 Linux-1 systemd[1]: Started User Manager for UID 0.
Feb  2 10:35:00 Linux-1 systemd[1]: Started Session c4 of user manolo.
Feb  2 10:35:00 Linux-1 systemd[1]: session-c4.scope: Killing process 31163 (CommonCron) with signal SIGTERM.
Feb  2 10:35:00 Linux-1 systemd[1]: session-c4.scope: Killing process 31164 (CommonCron) with signal SIGTERM.
Feb  2 10:35:00 Linux-1 systemd[1]: session-c4.scope: Killing process 31165 (CommonCron) with signal SIGTERM.
Feb  2 10:35:00 Linux-1 systemd[1]: Stopping Session c4 of user manolo.
Feb  2 10:35:00 Linux-1 systemd[1]: Stopped Session c4 of user manolo.
Feb  2 10:35:13 Linux-1 systemd[1]: Stopping User Manager for UID 0...

这是我的“systemd”服务:

[Unit]
Description=CommonStartUpShutdown
Requires=local-fs.target
Wants=network.target

[Service]
Type=forking
ExecStart=/etc/after_boot.local
RemainAfterExit=yes
TimeoutSec=infinity
KillMode=none
ExecStop=/etc/before_halt.local

[Install]
WantedBy=local-fs.target

# How I think it works:
#   Service starts when target 'local-fs.target' is reached, preferably, when target 'network.target'
#     is also reached. This last target is reached even if the router is powered off (tested).
#   Service start sequence runs script: 'ExecStart=/etc/after_boot.local' which is expected
#     to spawn child processes and exit: 'Type=forking'. Child processes: 'CommonSystemStartUp's
#     children: 'CommonDaemon', 'CommonCron'... This script must exit with 0, otherwise systemd
#     will kill all child processes and wont run 'ExecStop' script. Service start can run as long
#     as it needs: 'TimeoutSec=infinity'.
#   Service is kept alive, after running 'ExecStart=...', by 'RemainAfterExit=true'.
#   When the service is stopped, at system shutdown, script 'ExecStop=/etc/before_halt.local'
#     will run for as long as necessary, 'TimeoutSec=infinity', before target
#     'local-fs.target' is lost: 'Requires=local-fs.target'.
#     'ExecStart=/etc/after_boot.local's children processes ('CommonDaemon', 'CommonCron'...) won't be
#     killed when 'ExecStop=/etc/before_halt.local' runs: 'KillMode=none'.

我尝试将 'KillMode=none' 移动到 [Unit] 块:'systemd' 抱怨。

还在 [Install] 块中尝试了“WantedBy=multi-user.target.wants”:没有任何区别。

4

0 回答 0