简体   繁体   English

如何在Manjaro上将sysvinit脚本转换为systemd

[英]How to convert sysvinit script to systemd on Manjaro

First, please don't consider this post as a systemd review or critic, but only and simply as a request for help. 首先,请不要将此帖子视为系统评论或批评者,而应仅是简单地寻求帮助。

Since I've not been able to find a solution to this problem with the systemd documentation, I've this question not solved for almost a year and a half that never ever received any answer. 由于我无法通过systemd文档找到解决此问题的方法,因此这个问题已经解决了将近一年半,但从未收到过任何答案。

So, here is the context: 因此,这是上下文:

I've a program (/opt/myprog) that can be sarted as a deamon service at boot time. 我有一个程序(/ opt / myprog),可以在启动时作为守护程序出售。

When using previous Debian, LMDE, Mint or Ubuntu OSes, I used SysVinit with the following script (myprog.sh within the /etc/init.d folder): 当使用以前的Debian,LMDE,Mint或Ubuntu操作系统时,我将SysVinit与以下脚本(/etc/init.d文件夹中的myprog.sh)一起使用:

MYPROG_PATH=/opt/myprog_64
NAME="myprog"
START="-d"
STOP="-k"
TEST=""
VERSION="-v"
SCRIPTNAME=/etc/init.d/$NAME
STARTMESG="\nStarting $NAME in deamon mode.\n"
UPMESG="\$NAME is running.\n"
DOWNMESG="\$NAME is not running!\n"
TESTMESG="\nStarting NAME in client mode.\nHit Ctrl+C (or close the terminal) to stop mprog.\n"
STATUS=`pidof $NAME` 

# Exit if myprog is not installed
[ -x "$MYPROG_PATH/$NAME" ] || exit 0

case "$1" in
    start)
        sleep 3
        echo $STARTMESG
        cd $MYPROG_PATH
        ./$NAME $START
    ;;
    stop)
        cd $MYPROG_PATH
        ./$NAME $STOP
    ;;
    status)
        if [ "$STATUS" > 0 ] ; then
            echo $UPMESG
        else
            echo $DOWNMESG
        fi
    ;;
    restart)
        cd $MYPROG_PATH
        ./$NAME $STOP
        echo $STARTMESG
        ./$NAME $START
    ;;
    version)
        cd $MYPROG_PATH
        ./$NAME $VERSION
    ;;
    test)
        cd $MYPROG_PATH
        echo $TESTMESG
        ./$NAME
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|status|restart|stop|version|test}" >&2
        exit 3
    ;;
esac
:

Now, since it's obvious that systemd will be widely adopted to replace SysVinit including with future Debian, Mint and Ubuntu distros as it's with CentOS, Fedroa or Ach and Manjaro, I've tried to adapt my sysVinit script to systemd with the following script that works but is too limited (myprog.service): 现在,由于很明显,systemd将被广泛采用,以取代SysVinit,包括将来的Debian,Mint和Ubuntu发行版,例如CentOS,Fedroa或Ach和Manjaro,因此,我尝试使用以下脚本将sysVinit脚本改编为systemd可以,但是太有限了(myprog.service):

Description=myprog
ConditionFileExecutable=/opt/myprog_64
After=NetworkManager.service
[Service]
Type=oneshot
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/opt/myprog -d
ExecStop=/opt/myprog -k
ExecRestart=/opt/myprog-k : /opt/myprog -d
TimeoutSec=0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

However, as systemd is advertised as compatible and more flexible than SysVinit, could anyone show me how to add the three following equivalent switches (status, test and version) that I have defined in the myprog.sh sysVinit script without responding with the classic and inelegant answer: "man is your friend" ? 但是,由于systemd被宣传为比SysVinit更兼容且更灵活,因此谁能向我展示如何添加我在myprog.sh sysVinit脚本中定义的以下三个等效开关(状态,测试和版本),而无需使用经典的和优雅的回答:“男人是你的朋友”?

/opt/myprog status to display the myprog status (i.e. running or not)    
/opt/myprog test to start myprog not as a deamon     
/opt/myprog version to display the release of myprog

Thank you in advance fo your time and help. 预先感谢您的时间和帮助。

systemd does not support custom implementation of arguments to systemctl . systemd不支持systemctl自定义参数实现。

So systemctl status myprog will show the results based the execution of Exec* settings. 因此, systemctl status myprog将根据执行Exec*设置显示结果。

systemctl show myprog uses the Description so you can use a version in your description if desired. systemctl show myprog使用Description因此如果需要,您可以在Description中使用版本。

If you wan't to run your program not as a daemon, then you can start it outside of systemd . 如果您不想以守护程序的身份运行程序,则可以在systemd之外启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM