将 init.d 脚本移植到 systemd

将 init.d 脚本移植到 systemd
#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/local/bin/redis-server"
PIDFILE="/var/run/redis/redis_7128.pid"
RUNDIR="/var/run/redis"
REDIS_USER="redis"
DAEMON_ARGS="/etc/redis/redis_7128.conf"
REDISPORT="7128"

case "$1" in
  start)
        echo -n "Starting $DAEMON: "
        touch $PIDFILE
        chown redis:redis $PIDFILE
        chmod 755 $RUNDIR

        if [ -n "$ULIMIT" ]
        then
                ulimit -n $ULIMIT
        fi

        if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid $REDIS_USER:$REDIS_USER --exec $DAEMON -- $DAEMON_ARGS
        then
                echo "$NAME."
        else
                echo "failed"
        fi
        ;;
  stop)
        echo -n "Stopping $DESC: "
        if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
        then
                echo "$NAME."
        else
                echo "failed"
        fi
        rm -f $PIDFILE
        sleep 1
        ;;

  restart|force-reload)
        ${0} stop
        ${0} start
        ;;

  status)
        echo -n "$DESC is "
        if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
        then
                echo "running"
        else
                echo "not running"
                exit 1
        fi
        ;;

  *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0

我已经为 编写了上述脚本init.d,我想将其移植到systemd.

我尝试编写自己的,并将其保存在/etc/systemd/system/redis.service但变量没有扩展

[Unit]
Description=My Redis Service

[Service]
Type=forking
#User=redis
Restart=on-failure
RemainAfterExit=yes
EnvironmentFile=/etc/systemd/system/redisenv
ExecStart=start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid $REDIS_USER:$REDIS_USER --exec $DAEMON -- $DAEMON_ARGS
ExecStart=echo -n "Starting $DAEMON: "
ExecStart=touch $PIDFILE
ExecStart=chown redis:redis $PIDFILE
ExecStart=chmod 755 $RUNDIR
ExecStart=if [ -n "$ULIMIT" ];then ulimit -n $ULIMIT ;fi  
ExecStart=if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid $REDIS_USER:$REDIS_USER --exec $DAEMON -- $DAEMON_ARGS ;then echo "$NAME." ; else echo "failed"; fi  

ExecStop=start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON

[Install]
WantedBy=multi-user.target

EnvironmentFile我在移植时为其编写的内容,我将其保存在/etc/systemd/system/redisenv

SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN
ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/redis-server
PIDFILE=/var/run/redis/redis_6382.pid
RUNDIR=/var/run/redis
REDIS_USER=redis
DAEMON_ARGS=/etc/redis/redis_6382.conf
REDISPORT=6382

之后我做了

sudo chmod 777 /etc/systemd/system/redis.service
sudo chmod 777 /etc/systemd/system/redisenv
sudo systemctl daemon-reload
sudo systemctl start redis-service

但它返回

Failed to start redis.service: Unit redis.service is not loaded properly: Invalid argument. 
See system logs and 'systemctl status redis.service' for details.

sudo systemctl status redis.service返回

● redis.service - My Redis Service                                                                                                                                                                                                                                            
   Loaded: error (Reason: Invalid argument)                                                                                                                                                                                                                                   
   Active: inactive (dead)

Aug 14 19:11:31 squid2 systemd[1]: [/etc/systemd/system/redis.service:16] Executable path is not absolute, ignoring: if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid $REDIS_USER:$REDIS_USER --exec $DAEMON -- $DAEMON_ARGS ;then echo "$NAME." ; 
Aug 14 19:11:31 squid2 systemd[1]: redis.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:10] Executable path is not absolute, ignoring: start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid $REDIS_USER:$REDIS_USER --exec $DAEMON -- $DAEMON_ARGS
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:11] Executable path is not absolute, ignoring: echo -n "Starting $DAEMON: "
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:12] Executable path is not absolute, ignoring: touch $PIDFILE
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:13] Executable path is not absolute, ignoring: chown redis:redis $PIDFILE
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:14] Executable path is not absolute, ignoring: chmod 755 $RUNDIR
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:15] Executable path is not absolute, ignoring: if [ -n "$ULIMIT" ];then ulimit -n $ULIMIT ;fi
Aug 14 19:11:37 squid2 systemd[1]: [/etc/systemd/system/redis.service:16] Executable path is not absolute, ignoring: if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid $REDIS_USER:$REDIS_USER --exec $DAEMON -- $DAEMON_ARGS ;then echo "$NAME." ; 
Aug 14 19:11:37 squid2 systemd[1]: redis.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

init.d脚本移植到任何帮助systemd

我看到的第一个问题是变量不是变量扩展的脚本没有发生

答案1

欢迎来到 Unix StackExchange。

我建议从已经存在的 Redis 的 systemd 单元文件之一开始创建并共享

直接翻译的尝试变得比实际需要的更加困难,因为 systemd 内置了对许多之前在 bash 中编码的功能的支持:

  • User=指令设置其运行的用户
  • start-stop-daemon不需要
  • 不需要显式的 Pidfile 管理。
  • systemd已经设置了一个合理的 PATH 变量,所以你不需要。
  • systemd有很多控制资源的选项,所以不需要调用ulimit。查看man systemd.resource-control您的所有选项。

由于所有这些原因,您在网上找到的示例redis.service文件相当短。

Systemd 仅在有限的情况下支持环境变量替换。man systemd.service涵盖了在文档中搜索“环境变量替换”时的详细信息。

相关内容