Last Updated: February 25, 2016
·
1.067K
· syxanash

Build an Apache Switch with Zenity

According to me Zenity is a really wonderful CLI tool, because you can easily write GUIs with just one command. So recently I decided to build a script which allows me to start and stop the Apache web service on my local server, with just 2 clicks.

Here's the code:

#!/bin/bash

process_started[0]='FALSE'
process_started[1]='TRUE'

if [ -n "$(pidof apache2)" ] ; then
    process_started[0]='TRUE'
    process_started[1]='FALSE'
fi

output=$(zenity --title='Apache switcher' --text='Would you like to activate Apache?' --list --radiolist --column='Select' --column='Choice' --column='Option' ${process_started[0]} 'start' 'Start' FALSE 'restart' 'Restart' ${process_started[1]} 'stop' 'Stop' --hide-column=2)

if [ -z $output ] ; then
    zenity --error --text="No value selected!"
    exit 1
fi

zenity --info --text="`/etc/init.d/apache2 $output`"

save this file and chmod it in order to execute the file as program. Then, once you click on it, you should be able to see a window like this:

Picture

Now you can, for example, make an icon launcher on your DE:

Picture

Remember to run the script as root, so if you decide to create a launcher, execute the command as:

gksu ./apache-switch

or

sudo ./apache-switch