Advertisement
frakman1

Launch At Startup

May 29th, 2017
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.13 KB | None | 0 0
  1. pi@raspberrypi:~ $ cat /etc/init.d/launch-at-startup
  2. #!/bin/sh
  3. ### BEGIN INIT INFO
  4. # Provides: homebridge
  5. # Required-Start:    $network $remote_fs $syslog
  6. # Required-Stop:     $remote_fs $syslog
  7. # Default-Start:     2 3 4 5
  8. # Default-Stop:      0 1 6
  9. # Short-Description: Start daemon at boot time
  10. # Description:       Enable service provided by daemon.
  11. ### END INIT INFO
  12.  
  13. dir="/home/pi/"
  14. cmd="/home/pi/start.sh"
  15. user="pi"
  16.  
  17. name=`basename $0`
  18. pid_file="/var/run/$name.pid"
  19. stdout_log="/var/log/$name.log"
  20. stderr_log="/var/log/$name.err"
  21.  
  22. get_pid() {
  23.     cat "$pid_file"
  24. }
  25.  
  26. is_running() {
  27.     [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
  28. }
  29.  
  30. case "$1" in
  31.     start)
  32.     if is_running; then
  33.         echo "Already started"
  34.     else
  35.         echo "Starting $name"
  36.         cd "$dir"
  37.         if [ -z "$user" ]; then
  38.             sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
  39.         else
  40.             sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
  41.         fi
  42.         echo $! > "$pid_file"
  43.         if ! is_running; then
  44.             echo "Unable to start, see $stdout_log and $stderr_log"
  45.             exit 1
  46.         fi
  47.     fi
  48.     ;;
  49.     stop)
  50.     if is_running; then
  51.         echo -n "Stopping $name.."
  52.         kill `get_pid`
  53.         for i in {1..10}
  54.         do
  55.             if ! is_running; then
  56.                 break
  57.             fi
  58.  
  59.             echo -n "."
  60.             sleep 1
  61.         done
  62.         echo
  63.  
  64.         if is_running; then
  65.             echo "Not stopped; may still be shutting down or shutdown may have failed"
  66.             exit 1
  67.         else
  68.             echo "Stopped"
  69.             if [ -f "$pid_file" ]; then
  70.                 rm "$pid_file"
  71.             fi
  72.         fi
  73.     else
  74.         echo "Not running"
  75.     fi
  76.     ;;
  77.     restart)
  78.     $0 stop
  79.     if is_running; then
  80.         echo "Unable to stop, will not attempt to start"
  81.         exit 1
  82.     fi
  83.     $0 start
  84.     ;;
  85.     status)
  86.     if is_running; then
  87.         echo "Running"
  88.     else
  89.         echo "Stopped"
  90.         exit 1
  91.     fi
  92.     ;;
  93.     *)
  94.     echo "Usage: $0 {start|stop|restart|status}"
  95.     exit 1
  96.     ;;
  97. esac
  98.  
  99. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement