Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What i want to do is pretty simple to understand.

I want the same launcher shortcut to act like a ON/OFF swith for eg.

Like suppose i have made a launcher to start LAMPP with the command gksudo /opt/lampp/lampp start

This works properly to start lampp. Now,what i want to do is,If LAMPP is not on,the launcher should execute the above command and start lampp.While if lampp is on,I want ti to execute the command gksudo /opt/lampp/lampp stop and stop lampp.

I want these 2 commands to be executed by the same launcher where the command to be executed is decided on the basis of a condition(LAMPP being ON or OFF in the above example)

Can i do this? and how?

I guess writing a bash script and then making the launcher execute the bash script would do it.but then how do i check weather lampp is on of off?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

I noticed that lampp starts proftpd so we can modify a scirpt that checks for a running service, if that service is running then it will stop lampp, I modified the script from here - http://www.anyexample.com/linux_bsd/bash/check_if_program_is_running_with_bash_shell_script.xml

So first make an empty text file, or open gedit, and paste this code-

#!/bin/sh
SERVICE='proftpd'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    gksudo /opt/lampp/lampp stop && notify-send -i /opt/lampp/htdocs/xampp/img/logo-small.gif "XAMPP stopped."


else
    gksudo /opt/lampp/lampp start && notify-send -i /opt/lampp/htdocs/xampp/img/logo-small.gif "XAMPP started."

fi

This should also send notification that it starts and stops.

Save the file as lampp.sh and make it executable - right click => Properties => Permissions => Allow executing file as program.


Now use your favorite method to make a launcher with the script, How can I create launchers on my desktop?

Or I made one that includes the icon-

Place lampp.sh in /usr/local/bin

Create a file named Lampp.desktop and paste this into it-

[Desktop Entry]
Version=1.0
Type=Application
Name=Lampp
Comment=
Exec=/usr/local/bin/lampp.sh
Icon=/opt/lampp/htdocs/xampp/img/logo-small.gif
Path=
Terminal=false
StartupNotify=true
GenericName=

Or if you want you can make a right click list for the different functions to achieve start and stop function without the script-

[Desktop Entry]
Version=1.0
Type=Application
Name=Lampp
Comment=
Exec=gksudo lampp
Icon=/opt/lampp/htdocs/xampp/img/logo-small.gif
Path=/opt/lampp
Terminal=false
StartupNotify=true
GenericName=

X-Ayatana-Desktop-Shortcuts=NewWindow;Start;Stop;Restart;Security
[Start Shortcut Group]
Name=Start
Exec=start
TargetEnvironment=Unity

[Stop Shortcut Group]
Name=Stop
Exec=stop
TargetEnvironment=Unity

[Restart Shortcut Group]
Name=Restart
Exec=restart
TargetEnvironment=Unity

[Security Shortcut Group]
Name=Security
Exec=security
TargetEnvironment=Unity

So use one or the other, have fun.


Also I found they wrote a control panel for the program to use-

gksudo /opt/lampp/share/xampp-control-panel/xampp-control-panel.py

I you want to try this with other programs you can make scripts that will check if the are running and will kill them (warning might loose work, but would be nice for frozen programs) Say for example conky or any other program, mostly.

#!/bin/sh
    SERVICE='conky'

    if ps ax | grep -v grep | grep $SERVICE > /dev/null

    then
        killall conky

    else
        conky

    fi

save as conky.sh put in /usr/local/bin

Next is the hard part, you need to find the conky.desktop file, so in terminal type locate conky.desktop then navigate to that folder and edit the desktop file in gedit (or your favorite editor, may need privileges depending on where it is) and replace the Exec= line with Exec=/usr/local/bin/conky.sh (note that this will break any of the launchers that have right click menus)

share|improve this answer
    
oh!!!!! the last line was the simplest soln! ;) –  Nirmik Jun 29 '12 at 18:06
    
Okay and suppose it is conky...how do i find out that conky is runnig and then make the launcher execute the command killall conky so that conky exits? –  Nirmik Jun 29 '12 at 21:44
    
no no i dont want that...this method i obviously knw...its nothing but creating a new .dektop file...i want the same .desktop file to stop conky too...by checking its state...just like what we did in the lampp thing –  Nirmik Jun 29 '12 at 22:01
    
I get it now, I'll post an edit, I think you can just adapt the script and change out or replace a line in the conky launcher –  Mateo Jun 29 '12 at 22:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.