Eric_Sebasta

Talking Linux Toggle LAMP Webserver

Oct 24th, 2021
950
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ########################################################################################
  3. # PURPOSE  : Toggle Lamp Server status with confirmation.
  4. # AUTHOR   : Eric Sebasta
  5. # REQUIRES : gksu and yad.
  6. # OPTIONAL : notify-osd for notifications
  7. # TESTED   : Ubuntu 18.04+, ElementaryOS Hera, Debian 9+, MX 18.3
  8. # NOTE     : Can be modified to use zenity and work with pkexec if desired
  9. ########################################################################################
  10.  
  11. function say() {
  12. su $1 -c "spd-say -w '$1 $2 $3 $4 $5 $6 $7 $8 $9'"
  13. }
  14.  
  15. # check for apache and mysql/mariaDB
  16. if [[ ! -f /usr/sbin/apache2 ]] ; then
  17.     notify-send -i error 'Apache is not installed' 'You do not have Apache installed'
  18.     exit 1
  19. fi
  20. if [[ ! -f /usr/bin/mysql ]] ; then
  21.     notify-send -i error 'No Database' 'MySQL/MariaDB is not installed'
  22.     exit 2
  23. fi
  24. # Find out if server is running or not.
  25. STATUS=$(ps aux | grep 'apache'| wc -l)
  26. if [[ $STATUS -gt 1 ]] ; then
  27.     STATUS='On'
  28. else
  29.     STATUS='Off'
  30. fi
  31. if [[ $UID -gt 0 ]] ; then
  32.     yad --posx 320 --posy 240 --text "Server is $STATUS. Toggle Webserver?"
  33.     if [[ $? -eq 1 ]] ; then
  34.         notify-send -i info 'Canceled' "Server is still $STATUS"
  35.         exit 1 #canceled
  36.     fi
  37.     gksu "$0" $USER
  38.     exit
  39. fi
  40. # Here we are. confirmed toggle and running as root
  41. if [[ $STATUS == 'On' ]] ; then
  42.     # shut down services
  43.     service apache2 stop
  44.     if [[ $? -eq 0 ]] ; then
  45.         notify-send -i info 'Apache2' 'Webserver shut down'
  46.     else
  47.         notify-send -i error 'Apache2' 'Error shutting down Webserver!'
  48.         exit 111
  49.     fi
  50.     sleep 3
  51.     service mysql stop
  52.     if [[ $? -eq 0 ]] ; then
  53.         notify-send -i info 'Database' 'Database shut down'
  54.     else
  55.         notify-send -i error 'Database' 'Error shutting down Database!'
  56.         exit 222
  57.     fi
  58.     say 'Server is now Off'
  59. else
  60.     # start services
  61.     service apache2 start
  62.     if [[ $? -eq 0 ]] ; then
  63.         notify-send -i info 'Apache2' 'Webserver started'
  64.     else
  65.         notify-send -i error 'Apache2' 'Error starting Webserver!'
  66.         exit 100
  67.     fi
  68.     sleep 3
  69.     service mysql start
  70.     if [[ $? -eq 0 ]] ; then
  71.         notify-send -i info 'Database' 'Database started'
  72.     else
  73.         notify-send -i error 'Database' 'Error starting Database!'
  74.         exit 200
  75.     fi
  76.     say 'Server is running'
  77. fi
  78. sleep 2
  79. exit
  80.  
RAW Paste Data