1. 以apport为例,将/etc/init.d/apport拷贝并生成新的myapport, 内容如下:
#! /bin/sh
### BEGIN INIT INFO
# Provides: myapport
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: myapport init.d with systemd test"
### END INIT INFO
DESC="myapport init.d with systemd test"
NAME=myapport
SCRIPTNAME=/etc/init.d/$NAME
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
echo "myapport started"
return 0
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
echo "myapport stopped"
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC:" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC:" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
$0 stop || true
$0 start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
2. 在/etc/rc?.d任意一个中(?取值2 3 4 5),建立符号链接,指向/etc/init.d/myapport
lrwxrwxrwx 1 root root 18 9月 29 16:43 S01myapport -> ../init.d/myapport
注:如果不建立相关的符号链接,则表示该SysV服务没有启用,则在系统启动时不会执行。
3. 重启机器后查看myapport服务相关的信息
$ systemctl status myapport
● myapport.service - LSB: myapport init.d with systemd test"
Loaded: loaded (/etc/init.d/myapport; generated)
Active: inactive (dead) since Wed 2021-09-29 16:56:23 CST; 6s ago
Docs: man:systemd-sysv-generator(8)
Process: 2771 ExecStop=/etc/init.d/myapport stop (code=exited, status=0/SUCCESS)
Process: 979 ExecStart=/etc/init.d/myapport start (code=exited, status=0/SUCCESS)
9月 29 16:54:25 eric-vm-dev systemd[1]: Starting LSB: myapport init.d with systemd test"...
9月 29 16:54:29 eric-vm-dev myapport[979]: * Starting myapport init.d with systemd test: myapport
9月 29 16:54:29 eric-vm-dev myapport[979]: myapport started
9月 29 16:54:29 eric-vm-dev myapport[979]: ...done.
9月 29 16:54:29 eric-vm-dev systemd[1]: Started LSB: myapport init.d with systemd test".
$ systemctl list-unit-files | grep myapport
myapport.service generated
注:generated表示该服务单元是由systemd-sysv-generator应用程序生成。
$ systemctl cat myapport
# /run/systemd/generator.late/myapport.service
# Automatically generated by systemd-sysv-generator
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/myapport
Description=LSB: myapport init.d with systemd test"
Before=graphical.target
After=remote-fs.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/myapport start
ExecStop=/etc/init.d/myapport stop
参考:
本文介绍了如何创建一个名为myapport的自定义SysV服务,并通过Systemd进行管理。首先,创建了/etc/init.d/myapport脚本,然后在/etc/rc?.d目录下建立符号链接,确保服务在启动时运行。接着,通过systemctl命令查看myapport服务的状态和详细信息,展示了服务的加载、启动和停止过程。
https://unix.stackexchange.com/questions/233468/how-does-systemd-use-etc-init-d-scripts

725

被折叠的 条评论
为什么被折叠?



