Advertisement
Justman10000

Basic SysV-Init Script

Aug 11th, 2023 (edited)
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.24 KB | None | 0 0
  1. # Replace `Description` with the description of the application, `/path/to/my/service`, with the command, that should be executed
  2. # Then save the file under `/etc/init.d/myservice`, where you replace `myservice`, with the name of the application
  3.  
  4. # To activate
  5. service myservice enable
  6. # To start
  7. service myservice start
  8.  
  9. #!/bin/sh
  10. ### BEGIN INIT INFO
  11. # Provides:          myservice
  12. # Required-Start:    $remote_fs $syslog
  13. # Required-Stop:     $remote_fs $syslog
  14. # Default-Start:     2 3 4 5
  15. # Default-Stop:      0 1 6
  16. # Short-Description: Start/stop my custom service
  17. # Description:       Start/stop my custom service
  18. ### END INIT INFO
  19.  
  20. NAME="myservice"
  21. DAEMON="/path/to/my/service" # Parameters can be specified with --
  22. PIDFILE="/var/run/$NAME.pid"
  23.  
  24. start() {
  25.     echo "Starting $NAME..."
  26.     start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON
  27. }
  28.  
  29. stop() {
  30.     echo "Stopping $NAME..."
  31.     start-stop-daemon --stop --pidfile $PIDFILE --retry 10
  32.     rm -f $PIDFILE
  33. }
  34.  
  35. case "$1" in
  36.     start)
  37.         start
  38.     ;;
  39.  
  40.     stop)
  41.         stop
  42.     ;;
  43.  
  44.     restart)
  45.         stop
  46.         start
  47.     ;;
  48.  
  49.     *)
  50.         echo "Usage: $0 {start|stop|restart}"
  51.         exit 1
  52.     ;;
  53. esac
  54.  
  55. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement