systemd如何使用/etc/init.d脚本

本文介绍了如何创建一个名为myapport的自定义SysV服务,并通过Systemd进行管理。首先,创建了/etc/init.d/myapport脚本,然后在/etc/rc?.d目录下建立符号链接,确保服务在启动时运行。接着,通过systemctl命令查看myapport服务的状态和详细信息,展示了服务的加载、启动和停止过程。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

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

参考:

How does systemd use /etc/init.d scripts? - Unix & Linux Stack Exchangeicon-default.png?t=L892https://unix.stackexchange.com/questions/233468/how-does-systemd-use-etc-init-d-scripts

https://qastack.cn/unix/233468/how-does-systemd-use-etc-init-d-scriptsicon-default.png?t=L892https://qastack.cn/unix/233468/how-does-systemd-use-etc-init-d-scripts

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值