osteth

Demonsaw Daemon

Mar 1st, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.78 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. #  Demonsaw_cli Daemon
  4. #
  5. #  This script creates a service out of demonsaw_cli
  6. #  
  7. #  This script relies on "daemon" please use "apt-get install daemon" to install daemon
  8. #  demonsaw_cli should be install at /opt/demonsaw/demonsaw_cli for this to work out of the box
  9. #
  10. #  remember to make this scrip and demonsaw_cli executable using chmod +x
  11. #
  12. #  Usage:
  13. #  service demonsaw start|stop|restart|reload|status
  14. #
  15. #  Notice:
  16. #  demonsaw.xml must be properly configured and permissioned prior to running the daemon
  17. #  please configure demonsaw and test it using ./demonsaw_cli before attempting to start the daemon
  18. #
  19. # The daemon's name (to ensure uniqueness and for stop, restart and status)
  20. name="demonsaw"  
  21. # The path of the client executable
  22. # This must be the full path as symbolic pathing will not work in this situation
  23. command="/opt/demonsaw/demonsaw_cli"  
  24. # Any command line arguments for the client executable
  25. command_args=""  
  26. # The path of the daemon executable
  27. # use "where daemon" to find the path where daemon was installed however this should work
  28. # for any debian based system
  29. daemon="/usr/bin/daemon"
  30.  
  31. [ -x "$daemon" ] || exit 0
  32. [ -x "$command" ] || exit 0
  33.  
  34. # Note: The following daemon option arguments could be in /etc/daemon.conf
  35. # instead. That would probably be better because if the command itself were
  36. # there as well then we could just use the name here to start the daemon.
  37. # Here's some code to do it here in case you prefer that.
  38.  
  39. # Any command line arguments for the daemon executable (when starting)
  40. daemon_start_args="" # e.g. --inherit --env="ENV=VAR" --unsafe  
  41. # The pidfile directory (need to force this so status works for normal users)
  42. pidfiles="/var/run"  
  43. # The user[:group] to run as (if not to be run as root)
  44. user=""  
  45. # The path to chroot to (otherwise /)
  46. chroot=""  
  47. # The path to chdir to (otherwise /)
  48. chdir=""  
  49. # The umask to adopt, if any
  50. umask=""  
  51. # The syslog facility or filename for the client's stdout (otherwise discarded)
  52. stdout="/opt/demosaw/daemon.info"  
  53. # The syslog facility or filename for the client's stderr (otherwise discarded)
  54. stderr="/opt/demonsaw/daemon.err"
  55.  
  56. case "$1" in  
  57.     start)
  58.         # This if statement isn't strictly necessary but it's user friendly
  59.         if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
  60.         then
  61.             echo "$name is already running."
  62.         else
  63.             echo -n "Starting $name..."
  64.             "$daemon" --respawn $daemon_start_args \
  65.                 --name "$name" --pidfiles "$pidfiles" \
  66.                 ${user:+--user $user} ${chroot:+--chroot $chroot} \
  67.                 ${chdir:+--chdir $chdir} ${umask:+--umask $umask} \
  68.                 ${stdout:+--stdout $stdout} ${stderr:+--stderr $stderr} \
  69.                 -- \
  70.                 "$command" $command_args
  71.             echo done.
  72.         fi
  73.         ;;
  74.  
  75.     stop)
  76.         # This if statement isn't strictly necessary but it's user friendly
  77.         if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
  78.         then
  79.             echo -n "Stopping $name..."
  80.             "$daemon" --stop --name "$name" --pidfiles "$pidfiles"
  81.             echo done.
  82.         else
  83.             echo "$name is not running."
  84.         fi
  85.         ;;
  86.  
  87.     restart|reload)
  88.         if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
  89.         then
  90.             echo -n "Restarting $name..."
  91.             "$daemon" --restart --name "$name" --pidfiles "$pidfiles"
  92.             echo done.
  93.         else
  94.             echo "$name is not running."
  95.             exit 1
  96.         fi
  97.         ;;
  98.  
  99.     status)
  100.         "$daemon" --running --name "$name" --pidfiles "$pidfiles" --verbose
  101.         ;;
  102.  
  103.     *)
  104.         echo "usage: $0 <start|stop|restart|reload|status>" >&2
  105.         exit 1
  106. esac
  107.  
  108. exit 0
Add Comment
Please, Sign In to add comment