Apache, Aliases and Zenity on Ubuntu – how to control your ego

It’s the weekend. My girlfriend is staring at the screen in ferocious concentration as she does battle with her latest essay plan for the Masters she’s studying for.
Evicted from the desk and consigned to a dark corner, I’m trying to find some diverting, productive and, above all, quiet, way to amuse myself. As any parent will know, when the kids are quiet, it usually means they’re up to something…

I recently read a really good article by Coskan on the use of aliases in Linux.
Following on from my recent adventures with OPAL, I’ve found a perfect excuse to use them.
The apache init script has several options ( stop, start, status etc) so it seems like a perfect candidate for the use of aliases to save me a bit of typing.
The idea is simple enough. Simply add the aliases to my .bashrc and away I go …

alias apachestart="sudo /etc/init.d/apache2 start" 
alias apachestop="sudo /etc/init.d/apache2 stop" 
alias apachestatus="sudo /etc/init.d/apache2 status"

Now, if I want to check the status of Apache, I just need to type :

$ apachestatus 
[sudo] password for mikes: 
 * Apache is running (pid 1421). 

So simple. So handy. So cool. Sorry, getting a bit carried away there.
Speaking of which…
as I’m running this on a laptop and have Gnome available, I don’t need to bother with the command line at all. All I need is a bit of shell scripting and Zenity.

First of all though, I need to re-visit the vexed question of …

sudo in a shell script

Now, having played around with this a bit, I know that the -S switch should cause sudo to read the password from stdout …

$ echo 'pwd' | sudo -S /etc/init.d/apache2 status 
[sudo] password for mikes: 
 * Apache is running (pid 3104). 

In this example pwd is your user password.

By piping stdout from echo into the sudo command, we don’t get left hanging about if we invoke it from a shell script.

Now what we need is a script that will :

  • Give the user a choice of actions to perform on the Apache server
  • Prompt them for their password for the sudo command to be executed
  • Execute the required command

The GUI Apache Management Script

It was at this point my ego got all Jeremy Clarkson and insisted on calling the script “tomahawk”.
After all, it said, testosterone oozing from every pore, it’s related to Apache…and it does sound rather butch.
It was at this point that Google came to the rescue in pointing out that tomahawk is actually the name of a tool for testing network-based intrusion prevention systems.
Instead, we settled on the language of which the Apache spoke a dialect – Athabaskan. OK, we looked that up on Wikipedia.
Having relinquished the keyboard whilst looking appropriately smug, my ego wandered off to see if it could find any Top Gear repeats on Dave.
Meanwhile, I was left in peace to write the source :

# athabaskan.sh - gui based manager for Apache
action=$(zenity --list --height=240 --text "Select Action" \
    --radiolist \
    --column "Pick" \
    --column "Action" \
    TRUE "Status" \
    FALSE "Start" \
    FALSE "Stop" \
    FALSE "Restart" \
    FALSE "Reload");
# Check to see if the user has hit Cancel - if so $action will
# be null    
if [ -z "$action" ]; then
    exit 0
# convert action to lowercase for insertion into the command we'll eventually
# execute
action=`echo $action |tr '[A-Z]' '[a-z]'`
# Now build the command string
command="sudo -S /etc/init.d/apache2 $action"
# setup a temp file to hold the command output
# Now prompt for the sudo password
zenity --entry --width=250 --title "Sudo Password" \
    --text="Enter Password" --hide-text \
    | $command >$TFILE
# Get the command output for populating the dialog box
# if it's null and the status was asked for, it's cos Apache isn't running
msg=`cat $TFILE`
if [ -z "$msg" ] && [ "$action" = "status" ]; then
    msg="Apache is not running."
zenity --info --text "$msg"
# cleanup the temp file
exit 0

That should do, now to set the appropriate permissions :

chmod 755 athobaskan.sh

Next step is to add the script as a menu option.

Adding the script to the Applications Menu

I’ve chosen to put it under the System Tools Menu :

Right click the Applications menu and select Edit Menus from the pop-up list.

The Edit Menu Dialog

Click on System Tools in the left-hand pane.

Click on the New Item button.

Enter the details...

If you want to change the icon that will appear on the menu, just click on the one that appears in the dialog box and select something more to your liking.

...and select a suitable icon

Finally, hit OK and the new menu item will appear. Hit Close.

And we’re ready to test…

Initially, when I tried to run the script from the menu, I got a message telling me “file or directory does not exist”.
After extensive investigation, I called my ego away from whatever macho nonsense was on the TV to point out that I had missed the leading “/” in the “#!/bin/sh” line at the start of the script.
My ego is now firmly back in it’s box and won’t be coming out to play until I next need to write my CV.

Anyway, the whole thing now looks something like this :

Select our new menu item

What do we want to do with Apache ?

Need to supply a password for sudo

All working as expected

Well, that kept me amused for a while. Now, I’m sure that ITV4 were showing re-runs of The Professionals…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.