简体   繁体   中英

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.

So, here is the context:

I've a program (/opt/myprog) that can be sarted as a deamon service at boot time.

When using previous Debian, LMDE, Mint or Ubuntu OSes, I used SysVinit with the following script (myprog.sh within the /etc/init.d folder):

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):

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" ?

/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 .

So systemctl status myprog will show the results based the execution of Exec* settings.

systemctl show myprog uses the Description so you can use a version in your description if desired.

If you wan't to run your program not as a daemon, then you can start it outside of systemd .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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