Systemd services to init.d helper script

Here you'll find a place for solutions and hints.

Please use one of the support subforums below for questions or if you have any issues and need support.

Systemd services to init.d helper script

Postby syschuck » Dec 2nd, '15, 18:25

This is a script for us old-timers that can't get the hang of the systemd forced religion. If your like me, when ever I need to start or stop a service, my first inclination is to check /etc/init.d. Typical for me is to do something like /etc/init.d/dhcpd restart. With the introduction of the much bloated and maligned systemd finding the services and their correct name and spelling means jumping through hoops by typing systemctl --help; finding list-unit-files and typing systemctl list-unit-files and then looking through the list of units for the service it want. It's either that or cd into /usr/lib/systemd/system and do an ls.

To save time and the frustrations with the systemd way, I thought, wouldn't it be nice to all of the systemd services installed in /etc/init.d and functioning like the old init.d scripts did. So the other day while fitzing around with systemd, I decided to write this script. Basically each entry in init.d will act like the old init.d scripts so /etc/init.d/service has options start stop restart reload condrestart status enable disable. The script simply invokes the systemd equivilant command. It not only makes finding the services easier; ie. 'ls /etc/init.d' it faster and easier to start and stop services.

Code: Select all
#!/bin/sh
#
SCRIPTNAME=initdsystemd.sh
BASENAME=`basename $0`

# initdsystemd.sh fixed-that-for-you-script.  Copy this script into /etc/init.d using the name in the $SCRIPTNAME
# variable. Running this script using the SCRIPTNAME (/etc/init.d/$SCRIPTNAME) above will list all systemd
# services names and create softlinks in init.d that point back to this script.  This script will then provide each
# service with a init.d like wrapper that calls systemd using the $0 argument and provide command to control
# that systemd services.  Each service in init.d will have the following options:
# {start|status|stop|restart|condrestart|reload|enable|disable}

# CBS: 12/2015

if [ "$BASENAME" == "$SCRIPTNAME" ]; then
    TMPFILE=`mktemp`
    systemctl list-unit-files --type=service | grep -v static | cut -f 1 -d ' ' | sed s/.service//g > $TMPFILE

    for i in `cat $TMPFILE`
    do
           if [ "$1" == "clean" ]; then
            A=`find $i -maxdepth 1  -type l -ls | grep $SCRIPTNAME`
            if [ -n A ]; then
                rm $i
            fi
        else
            if [ ! -f /etc/init.d/$i ]; then
                 ln -s /etc/init.d/$SCRIPTNAME /etc/init.d/$i
            fi
        fi
    done
    rm $TMPFILE
    exit 0
fi

# Now the systemd wrapper command start|status|stop|restart|condrestart|reload|enable|disable
# enable and disable would normally be done with chkconfig but I put them here for convience.
start () {
    echo -n "Starting $BASENAME..."

    systemctl start $BASENAME

    RETVAL=$?
    if [ $RETVAL = 0 ]
    then
        echo "done."
    else
        echo "failed. See error code for more information."
    fi
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n "Stopping $BASENAME..."

    systemctl stop $BASENAME
    RETVAL=$?

    if [ $RETVAL = 0 ]
    then
        echo "done."
    else
        echo "failed. See error code for more information."
    fi
    return $RETVAL
}


restart () {
    systemctl restart $BASENAME
}

condrestart () {
    systemctl condrestart $BASENAME
}

reload () {
    systemctl reload $BASENAME
}

enable () {
    systemctl enable $BASENAME
}

disable () {
    systemctl disable $BASENAME
}


status () {
    systemctl status $BASENAME
    return $?
}


case "$1" in
    start)
        start
    ;;
    status)
        status
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    condrestart)
        restart
    ;;
    reload)
        reload
    ;;
    enable)
        enable
        start
    ;;
    disable)
        stop
        disable
    ;;
    *)
        echo $"Usage: $BASENAME {start|status|stop|restart|condrestart|reload|enable|disable}"
        exit 3
    ;;
esac

exit $RETVAL



Enjoy. Hopefully this will help the old guys like me born and raised on the old init.d scripts. :D
Last edited by syschuck on Dec 2nd, '15, 21:53, edited 3 times in total.
syschuck
 
Posts: 15
Joined: Jul 4th, '13, 04:06

Re: Systemd services to init.d helper script

Postby doktor5000 » Dec 2nd, '15, 19:03

syschuck wrote:With the introduction of the much bloated and maligned systemd finding the services and their correct name and spelling means jumping through hoops by typing systemctl --help; finding list-unit-files and typing systemctl list-unit-files and then looking through the list of units for the service it want. It's either that or cd into /usr/lib/systemd/system and do an ls.


FWIW, systemctl supports tab completion, so you could simply do systemctl status <tab> <tab> to get a list of all units, or e.g. for your example with dhcpd you could use systemctl status dhcp <tab>
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 17630
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: Systemd services to init.d helper script

Postby alf » Dec 2nd, '15, 20:30

and a
Code: Select all
service <service> <cmd>
redirects to systemctl.
example
Code: Select all
[root@kre010 alfred]# service network restart
Restarting network (via systemctl):                                                                                                  [  OK  ]
[root@kre010 alfred]# service cups stop
Redirecting to /bin/systemctl stop cups.service
Warning: Stopping cups.service, but it can still be activated by:
  cups.socket
  cups.path
for windows problems reboot; for linux problems be root
alf
 
Posts: 326
Joined: Apr 1st, '11, 23:07
Location: DE Paderborn

Re: Systemd services to init.d helper script

Postby syschuck » Dec 2nd, '15, 21:44

All good to note. I added a 'clean' option to the script so it will clean the symlinks if someone wants to give it a test drive but doesn't like it. The usage is the same: /etc/init.d/initdsystemd.sh would create the sysmlinks for the services and /etc/init.d/initdsystemd.sh clean will remove them.
syschuck
 
Posts: 15
Joined: Jul 4th, '13, 04:06


Return to The magician suggests...

Who is online

Users browsing this forum: No registered users and 1 guest

cron