Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.07 KB | None | 0 0
  1. #!/bin/sh -e
  2. ### BEGIN INIT INFO
  3. # Provides:          minidlna
  4. # Required-Start:    $network $local_fs $remote_fs
  5. # Required-Stop::    $network $local_fs $remote_fs
  6. # Should-Start:      $all
  7. # Should-Stop:       $all
  8. # Default-Start:     2 3 4 5
  9. # Default-Stop:      0 1 6
  10. # Short-Description: Start minidlna at boot time
  11. # Description:       Manage the minidlna daemon, a DLNA/UPnP-AV media server.
  12. ### END INIT INFO
  13.  
  14. unset USER
  15.  
  16. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  17. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  18. DESC="DLNA/UPnP-AV media server"
  19. NAME=minidlna
  20. DAEMON=/usr/bin/minidlna
  21. PIDDIR=/run/$NAME
  22. PIDFILE=$PIDDIR/$NAME.pid
  23. SCRIPTNAME=/etc/init.d/$NAME
  24. DEFAULT=/etc/default/$NAME
  25.  
  26. # Exit if the package is not installed
  27. [ -x $DAEMON ] || exit 0
  28.  
  29. # Read configuration variable file if it is present
  30. [ -r $DEFAULT ] && . $DEFAULT
  31.  
  32. # Load the VERBOSE setting and other rcS variables
  33. . /lib/init/vars.sh
  34.  
  35. # Define LSB log_* functions.
  36. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  37. . /lib/lsb/init-functions
  38.  
  39. # Do not start the daemon if NO_START is enabled in DEFAULT
  40. if [ "$START_DAEMON" != "yes" ] && [ "$1" != "stop" ]; then
  41.     log_warning_msg "$NAME: Not starting $DESC."
  42.     log_warning_msg "$NAME: Disabled in $DEFAULT."
  43.     exit 0
  44. fi
  45.  
  46. # Set the default configuration file
  47. if [ -z $CONFIGFILE ]; then
  48.     CONFIGFILE=/etc/minidlna.conf
  49. fi
  50.  
  51. # Set the default log file
  52. if [ -z $LOGFILE ]; then
  53.     LOGFILE=/var/log/minidlna.log
  54. fi
  55.  
  56. # Run as `minidlna' if USER is not specified or is `root'
  57. if [ -z $USER ]; then
  58.     USER=minidlna
  59. fi
  60.  
  61. # If no group is specified, use USER
  62. if [ -z $GROUP ]; then
  63.     GROUP=$USER
  64. fi
  65.  
  66. DAEMON_ARGS="-f $CONFIGFILE -P $PIDFILE $DAEMON_OPTS"
  67.  
  68. #
  69. # Function that starts the daemon/service
  70. #
  71. do_start()
  72. {
  73.     # Return
  74.     #   0 if daemon has been started
  75.     #   1 if daemon was already running
  76.     #   2 if daemon could not be started
  77.     touch $LOGFILE && chown $USER:$GROUP $LOGFILE || return 2
  78.     if [ ! -d $PIDDIR ]; then
  79.         mkdir $PIDDIR || return 2
  80.     fi
  81.     chown $USER:$GROUP $PIDDIR || return 2
  82.  
  83.     start-stop-daemon --start --quiet --pidfile $PIDFILE \
  84.         --chuid $USER:$GROUP --exec $DAEMON --test > /dev/null \
  85.         || return 1
  86.     start-stop-daemon --start --quiet --pidfile $PIDFILE \
  87.         --chuid $USER:$GROUP --exec $DAEMON -- \
  88.         $DAEMON_ARGS \
  89.         || return 2
  90. }
  91.  
  92. #
  93. # Function that stops the daemon/service
  94. #
  95. do_stop()
  96. {
  97.     # Return
  98.     #   0 if daemon has been stopped
  99.     #   1 if daemon was already stopped
  100.     #   2 if daemon could not be stopped
  101.     #   other if a failure occurred
  102.     start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  103.     RETVAL="$?"
  104.     [ "$RETVAL" = 2 ] && return 2
  105.     # Wait for children to finish too if this is a daemon that forks
  106.     # and if the daemon is only ever run from this initscript.
  107.     start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
  108.     [ "$?" = 2 ] && return 2
  109.     # Many daemons don't delete their pidfiles when they exit.
  110.     rm -rf $PIDDIR
  111.     return "$RETVAL"
  112. }
  113.  
  114. case "$1" in
  115.   start)
  116.     [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
  117.     do_start
  118.     case "$?" in
  119.         0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  120.         2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  121.     esac
  122.   ;;
  123.   stop)
  124.     [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  125.     do_stop
  126.     case "$?" in
  127.         0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  128.         2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  129.     esac
  130.     ;;
  131.   status)
  132.        status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
  133.        ;;
  134.   restart|force-reload)
  135.     log_daemon_msg "Restarting $DESC" "$NAME"
  136.     do_stop
  137.     case "$?" in
  138.       0|1)
  139.         if [ "$1" = "force-reload" ]; then
  140.             # Rescan the collection
  141.             DAEMON_ARGS="$DAEMON_ARGS -R"
  142.         fi
  143.         do_start
  144.         case "$?" in
  145.             0) log_end_msg 0 ;;
  146.             1) log_end_msg 1 ;; # Old process is still running
  147.             *) log_end_msg 1 ;; # Failed to start
  148.         esac
  149.         ;;
  150.       *)
  151.         # Failed to stop
  152.         log_end_msg 1
  153.         ;;
  154.     esac
  155.     ;;
  156.   *)
  157.     echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  158.     exit 3
  159.     ;;
  160. esac
  161.  
  162. :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement