Advertisement
kozec

/etc/rc.d/systemd-unit

Jun 28th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.33 KB | None | 0 0
  1. #!/bin/bash
  2. # rc.d script that launch service defined in systemd service unit
  3. # Symlink this to same name as systemd unit has(without .service extension)
  4. # and run as usual
  5. # (Example: ln -s systemd-unit NetworkManager)
  6.  
  7. # Version 0.1
  8. # TODO: ExecStartPre, ExecStartPost (AFAIK not used by any actual service in Arch)
  9.  
  10. . /etc/rc.conf
  11. . /etc/rc.d/functions
  12.  
  13. function read_ini { # filename, key, default value
  14.     x=$(cat $1 | egrep "^[ ]*""$2""[ ]*"= | cut -d = -f 2-)
  15.     [ x"$x" == x ] && x="$3"
  16.     echo "$x"
  17. }
  18.  
  19. function smallest_pidof { # program
  20.     # Gets smallest PID of specified program. Parameters are ignored
  21.     pidof -x $1 | tr " " "\n" | sort | head -1
  22. }
  23.  
  24. function nice_kill { # PID
  25.     # Sends SIGTERM, waits 5 secs (if necessary) and then kills
  26.     kill $PID
  27.     sleep 0.1
  28.     kill -0 $PID 2>/dev/null || return 0
  29.     for i in 1 2 3 4 5 ; do
  30.     sleep 1
  31.     kill -0 $PID 2>/dev/null || return 0
  32.     done
  33.     kill -9 $PID
  34.     echo "Killed." >/dev/stderr
  35. }
  36.  
  37. # Detect service name and systemd unit file
  38. SERVICE=$(basename $0)
  39. FILE=/usr/lib/systemd/system/$SERVICE.service
  40.  
  41. # Read configuration
  42. DESC=$(read_ini $FILE Description $SERVICE)
  43. ENVIRON=$(read_ini $FILE EnvironmentFile)
  44. ENVIRON=${ENVIRON#"-"}
  45. TYPE=$(read_ini $FILE Type simple)
  46.  
  47. [ x"$ENVIRON" != x ] && [ -e "$ENVIRON" ] && source "$ENVIRON"
  48.  
  49. case "$1" in
  50.   start)
  51.     EXEC=$(read_ini $FILE ExecStart /bin/false)
  52.     EXEC=$(eval echo $EXEC)
  53.     if [ x"$TYPE" == xforking ] ; then
  54.         # Forking service
  55.         stat_busy "Starting $DESC"
  56.         if $EXEC ; then
  57.             add_daemon $SERVICE
  58.             stat_done
  59.         else
  60.             stat_fail
  61.             exit 1
  62.         fi
  63.     elif [ x"$TYPE" == xoneshot ] ; then
  64.         # Oneshot
  65.         stat_busy "Executing $DESC"
  66.         if $EXEC ; then
  67.             stat_done
  68.         else
  69.             stat_fail
  70.             exit 1
  71.         fi
  72.     else
  73.         # Everything else (dbus, notify, idle, simple)
  74.         stat_busy "Starting $DESC"
  75.         if [[ "$EXEC" == -* ]] ; then
  76.             # There is no need to check if daemon is really running
  77.             EXEC=${EXEC#"-"}
  78.             $EXEC &>/dev/null &
  79.             stat_done
  80.         else
  81.             # Daemon is launched on background and it's excepted to stay alive
  82.             $EXEC &>/dev/null &
  83.             sleep 0.1
  84.             if [ x$(smallest_pidof $EXEC) != x ] ; then
  85.                 stat_done
  86.             else
  87.                 stat_fail
  88.                 exit 1
  89.             fi
  90.         fi
  91.     fi
  92.   ;;
  93.   stop)
  94.     if [ x"$TYPE" == xoneshot ] ; then
  95.         # stop command is not available for oneshots; displays help message
  96.         $0
  97.         exit 1
  98.     fi
  99.     PIDFILE=$(read_ini $FILE PIDFile)
  100.     EXEC=$(read_ini $FILE ExecStop)
  101.     PID=""
  102.     stat_busy "Stopping $DESC"
  103.     if [ x"$PIDFILE" != x ] ; then
  104.         # Read PID from defined PID file
  105.         PID=$(cat $PIDFILE 2>/dev/null)
  106.         if [ x"$PID" == x ] ; then
  107.             echo "Failed to read PID file $PIDFILE" >/dev/stderr
  108.             stat_fail
  109.             exit 1
  110.         fi
  111.     fi
  112.     if [ x"$EXEC" != x ] ; then
  113.         # Stop using defined command
  114.         MAINPID=$PID
  115.         EXEC=$(eval echo $EXEC)
  116.         if [[ "$EXEC" == -* ]] ; then
  117.             # There is no need to check if command was sucessfull
  118.             EXEC=${EXEC#"-"}
  119.             $EXEC
  120.             stat_done
  121.         else
  122.             if $EXEC ; then
  123.                 stat_done
  124.             else
  125.                 stat_fail
  126.                 exit 1
  127.             fi
  128.         fi
  129.     elif [ x"$PID" != x ] ; then
  130.         # Stop using PID from PID file
  131.         nice_kill $PID
  132.         rm_daemon $SERVICE
  133.         stat_done
  134.     else
  135.         # Stop using PID of program defined in ExecStart
  136.         EXEC=$(read_ini $FILE ExecStart /bin/false)
  137.         PID=$(smallest_pidof $EXEC)
  138.         if [ x"$PID" == x ] ; then
  139.             echo "Service is already stopped" >/dev/stderr
  140.             stat_fail
  141.             exit 1
  142.         fi
  143.         nice_kill $PID
  144.         rm_daemon $SERVICE
  145.         stat_done
  146.     fi
  147.   ;;
  148.   restart)
  149.     if [ x"$TYPE" == xoneshot ] ; then
  150.         # Oneshot cannot be restarted. Display help
  151.         $0
  152.         exit 1
  153.     else
  154.         RESTARTSEC=$(read_ini $FILE RestartSec 0.1)
  155.         $0 stop
  156.         sleep $RESTARTSEC
  157.         $0 start
  158.     fi
  159.   ;;
  160.   reload)
  161.     EXEC=$(read_ini $FILE ExecReload)
  162.     if [ x"$EXEC" != x -a x"$TYPE" != xoneshot ] ; then
  163.         stat_busy "Reloading $DESC"
  164.         PIDFILE=$(read_ini $FILE PIDFile /dev/null)
  165.         MAINPID=$(cat $PIDFILE 2>/dev/null)
  166.         [ x"$MAINPID" == x ] && MAINPID=$(smallest_pidof  $(read_ini $FILE ExecStart /bin/false) )
  167.         EXEC=$(eval echo $EXEC)
  168.         if $EXEC ; then
  169.             stat_done
  170.             exit 0
  171.         else
  172.             stat_fail
  173.             exit 1
  174.         fi
  175.     else
  176.         $0  # reload command is not available; displays help message
  177.         exit 1
  178.     fi
  179.   ;;
  180.   *)
  181.     RELOADEXEC=$(read_ini $FILE ExecReload)
  182.     USAGE="start"
  183.     [ x"$TYPE" != xoneshot ] && USAGE="$USAGE|stop|restart"
  184.     [ x"$RELOADEXEC" != x ]  && USAGE="$USAGE|reload"
  185.     echo "usage: $0 {$USAGE}" >&2
  186.     exit 1
  187.   ;;
  188. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement