Guest User

Untitled

a guest
Apr 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. CMD="$(basename $0)"
  4.  
  5. usage() {
  6. echo "usage: $CMD [-f file | -n name ] [ -i | -u | -s | -r ] [-v] | [-h]"
  7. echo ''
  8. echo 'systemd service installer'
  9. echo 'optional arguments:
  10. -h, --help show this help message and exit
  11. -f, --file service definition file (systemd unit)
  12. -n, --name service name (can be used with uninstall and status options)
  13. -i, --install install service
  14. -u, --uninstall uninstall service
  15. -r, --restart restart service
  16. -s, --status check status
  17. --status
  18. -v, --verbose print verbose output'
  19.  
  20. exit 1
  21. }
  22.  
  23. #!/bin/sh
  24. if [ ! $# -gt 0 ] ; then
  25. usage
  26. fi
  27.  
  28. verbose=
  29. install=
  30. uninstall=
  31. status=
  32. restart=
  33. SERVICE_DEFINITION=
  34. NAME=
  35.  
  36. while [ "$1" != "" ]; do
  37. case $1 in
  38. -f | --file ) shift
  39. SERVICE_DEFINITION=$1
  40. ;;
  41. -n | --name ) shift
  42. SERVICE_NAME=$1
  43. ;;
  44. -v | --verbose ) verbose=1
  45. ;;
  46. -i | --install ) install=1
  47. ;;
  48. -u | --uninstall ) uninstall=1
  49. ;;
  50. -r | --restart ) restart=1
  51. ;;
  52. -s | --status ) status=1
  53. ;;
  54. -h | --help ) usage
  55. exit
  56. ;;
  57. * ) usage
  58. exit 1
  59. esac
  60. shift
  61. done
  62.  
  63. if [[ -z $uninstall && -z $install && -z $status && -z $restart ]]
  64. then
  65. echo "No action specified. Please include -i (--install), -u (--uninstall), -s (--status)"
  66. echo ""
  67. usage
  68. fi
  69.  
  70. if [[ ! -z $install && -z $SERVICE_DEFINITION ]]
  71. then
  72. echo "Please include SERVICE_DEFINITION file with -f (--file) option to install service"
  73. echo ""
  74. usage
  75. fi
  76.  
  77. # Remove "<extra_identifier>": <service_name><extra_identifier>.service -> <service_name>.service
  78. if [[ ! -z $SERVICE_DEFINITION ]]
  79. then
  80. SERVICE_FILE="$(basename $SERVICE_DEFINITION)"
  81. DIRNAME="$(dirname $SERVICE_DEFINITION)"
  82. SERVICE_NAME="$(echo $SERVICE_FILE | /bin/sed 's/\.[A-Za-z0-9]*\.service//')"
  83. fi
  84.  
  85. #Attempt to stop/disable service before installing new
  86. if [[ ! -z $uninstall || ! -z $install ]]
  87. then
  88. if [[ -z $SERVICE_NAME ]]
  89. then
  90. echo "Missing SERVICE_NAME. Please include file or name of service to uninstall with -f (--file) or -n (--name) options."
  91. echo ""
  92. usage
  93. fi
  94. set -x
  95. service $SERVICE_NAME stop
  96. systemctl disable $SERVICE_NAME
  97. rm -f /etc/systemd/system/$SERVICE_NAME.service
  98. { set +x; } 2>/dev/null
  99. fi
  100.  
  101. if [[ ! -z $install ]]
  102. then
  103. set -x
  104. # Copy service definition file to systemd dir
  105. cp $SERVICE_DEFINITION /etc/systemd/system/$SERVICE_NAME.service
  106. # reload services to pick up changes
  107. systemctl daemon-reload
  108. # enable new service
  109. systemctl enable $SERVICE_NAME --now
  110. { set +x; } 2>/dev/null
  111. fi
  112.  
  113. if [[ ! -z $restart && ! -z $SERVICE_NAME ]]
  114. then
  115. echo "Restarting service:"
  116. set -x
  117. service $SERVICE_NAME restart
  118. { set +x; } 2>/dev/null
  119. fi
  120.  
  121. if [[ ! -z $status || ! -z $install ]]
  122. then
  123. echo "Service Status:"
  124. set -x
  125. service $SERVICE_NAME status
  126. { set +x; } 2>/dev/null
  127. fi
Add Comment
Please, Sign In to add comment