Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ########################################################################################
- # PURPOSE : Toggle Lamp Server status with confirmation.
- # AUTHOR : Eric Sebasta
- # REQUIRES : gksu and yad.
- # OPTIONAL : notify-osd for notifications
- # TESTED : Ubuntu 18.04+, ElementaryOS Hera, Debian 9+, MX 18.3
- # NOTE : Can be modified to use zenity and work with pkexec if desired
- ########################################################################################
- function say() {
- su $1 -c "spd-say -w '$1 $2 $3 $4 $5 $6 $7 $8 $9'"
- }
- # check for apache and mysql/mariaDB
- if [[ ! -f /usr/sbin/apache2 ]] ; then
- notify-send -i error 'Apache is not installed' 'You do not have Apache installed'
- exit 1
- fi
- if [[ ! -f /usr/bin/mysql ]] ; then
- notify-send -i error 'No Database' 'MySQL/MariaDB is not installed'
- exit 2
- fi
- # Find out if server is running or not.
- STATUS=$(ps aux | grep 'apache'| wc -l)
- if [[ $STATUS -gt 1 ]] ; then
- STATUS='On'
- else
- STATUS='Off'
- fi
- if [[ $UID -gt 0 ]] ; then
- yad --posx 320 --posy 240 --text "Server is $STATUS. Toggle Webserver?"
- if [[ $? -eq 1 ]] ; then
- notify-send -i info 'Canceled' "Server is still $STATUS"
- exit 1 #canceled
- fi
- gksu "$0" $USER
- exit
- fi
- # Here we are. confirmed toggle and running as root
- if [[ $STATUS == 'On' ]] ; then
- # shut down services
- service apache2 stop
- if [[ $? -eq 0 ]] ; then
- notify-send -i info 'Apache2' 'Webserver shut down'
- else
- notify-send -i error 'Apache2' 'Error shutting down Webserver!'
- exit 111
- fi
- sleep 3
- service mysql stop
- if [[ $? -eq 0 ]] ; then
- notify-send -i info 'Database' 'Database shut down'
- else
- notify-send -i error 'Database' 'Error shutting down Database!'
- exit 222
- fi
- say 'Server is now Off'
- else
- # start services
- service apache2 start
- if [[ $? -eq 0 ]] ; then
- notify-send -i info 'Apache2' 'Webserver started'
- else
- notify-send -i error 'Apache2' 'Error starting Webserver!'
- exit 100
- fi
- sleep 3
- service mysql start
- if [[ $? -eq 0 ]] ; then
- notify-send -i info 'Database' 'Database started'
- else
- notify-send -i error 'Database' 'Error starting Database!'
- exit 200
- fi
- say 'Server is running'
- fi
- sleep 2
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement