在 Ubuntu 16.04 中停止虚假服务/守护进程

在 Ubuntu 16.04 中停止虚假服务/守护进程

我有以下问题坚果(网络UPS工具)包。所以我开始深入研究并做一些实验。

我怎样才能创建一个假服务,它只是stop在某个运行级别上用参数调用,就像在一个美好的旧时代一样init.d

正如我所见,许多软件包仍然依赖于此,但我无法获得所需的行为。

例如,我有一个简单的脚本文件/etc/init.d/halt和指向它的链接/etc/rc0.d/K07halt。它应该在停止运行级别结束时调用。

但如果我这样做service halt stop/etc/rc0.f/K07halt收到错误:

Stopping K07halt (via systemctl): K07halt.serviceFailed to stop
K07halt.service: Unit K07halt.service not loaded.
failed!

即使没有这样的服务/守护进程,我怎样才能执行这样的虚拟脚本?

更改标题中的某些内容?声明一些标志/lib/lsb/init-functions?如何欺骗Systemd并使其无条件调用脚本?

请不要告诉我不要使用,init.d因为我不是在编写新的守护进程,而是试图修复已经存在的东西。

答案1

我为 Ubuntu 16.04 编写了 LSB 服务,没有任何问题:

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial

fake-service添加具有 755 权限的新服务/etc/init.d

#!/bin/sh
# fake-service   Bring up/down fake-service
#
### BEGIN INIT INFO
# Provides:     fake-service
# Default-Start:    2 3 4 5
# Default-Stop:
#Required-Start:
#Required-Stop:
# Should-Start:
# Should-Start:
# Short-Description: Bring up/down fake-service
# Description: Bring up/down fake service
### END INIT INFO

DAEMON=
NAME=fake-service
DESC="fake-service"
PIDFILE=/var/run/fake-service.pid
SCRIPTNAME=/etc/init.d/fake-service

. /lib/lsb/init-functions

case "$1" in
start)
    log_daemon_msg "Starting fake-service ..." "$NAME"
    touch $PIDFILE
    log_end_msg $?
;;
stop)
    log_daemon_msg "Stoping fake-service ..." "$NAME"
    rm -f $PIDFILE
    log_end_msg $?
;;
status)
    status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
;;
*)
    log_action_msg "Usage: $0 {start|stop|status}"
    exit 2
    ;;
esac

exit 0

将其添加到运行级别:

# update-rc.d fake-service defaults

man 8 update-rc.d

当使用默认选项运行时,update-rc.d 使用来自 init.d 的运行级别和依赖项信息,创建名为 /etc/rcrunlevel.d/[SK]NNname 的链接,该链接指向脚本 /etc/init.d/name脚本 LSB 注释头。

测试服务:

开始:

# service fake-service start
# service fake-service status
● fake-service.service - LSB: Bring up/down fake-service
   Loaded: loaded (/etc/init.d/fake-service; bad; vendor preset: enabled)
   Active: active (exited) since Mon 2018-04-09 19:14:18 MSK; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3643 ExecStop=/etc/init.d/fake-service stop (code=exited, status=0/SUCCESS)
  Process: 3713 ExecStart=/etc/init.d/fake-service start (code=exited, status=0/SUCCESS)

Apr 09 19:14:18 ubuntu16-04 systemd[1]: Starting LSB: Bring up/down fake-service...
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:  * Starting fake-service ... fake-service
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:    ...done.
Apr 09 19:14:18 ubuntu16-04 systemd[1]: Started LSB: Bring up/down fake-service.

停止:

# service fake-service stop
# service fake-service status
● fake-service.service - LSB: Bring up/down fake-service
   Loaded: loaded (/etc/init.d/fake-service; bad; vendor preset: enabled)
   Active: inactive (dead) since Mon 2018-04-09 19:15:03 MSK; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3757 ExecStop=/etc/init.d/fake-service stop (code=exited, status=0/SUCCESS)
  Process: 3713 ExecStart=/etc/init.d/fake-service start (code=exited, status=0/SUCCESS)

Apr 09 19:14:18 ubuntu16-04 systemd[1]: Starting LSB: Bring up/down fake-service...
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:  * Starting fake-service ... fake-service
Apr 09 19:14:18 ubuntu16-04 fake-service[3713]:    ...done.
Apr 09 19:14:18 ubuntu16-04 systemd[1]: Started LSB: Bring up/down fake-service.
Apr 09 19:15:03 ubuntu16-04 systemd[1]: Stopping LSB: Bring up/down fake-service...
Apr 09 19:15:03 ubuntu16-04 fake-service[3757]:  * Stoping fake-service ... fake-service
Apr 09 19:15:03 ubuntu16-04 fake-service[3757]:    ...done.
Apr 09 19:15:03 ubuntu16-04 systemd[1]: Stopped LSB: Bring up/down fake-service.

相关内容