6

I want the same launcher shortcut to act like a ON/OFF switch.

For example, 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.

  • If LAMPP is on, I want to to execute the command

    gksudo /opt/lampp/lampp stop
    

    and stop lampp.

I want these two 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 whether LAMPP is on of off?

0

1 Answer 1

6

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)

5
  • oh!!!!! the last line was the simplest soln! ;)
    – Nirmik
    Jun 29, 2012 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, 2012 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, 2012 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, 2012 at 22:04
  • Note that nowadays for the actions to work (at least in ubuntu 16.04) you should use the standard that is: Actions instead of X-Ayatana-Desktop-Shortcuts, [Desktop Action X] instead of [X Shortcut Group], and remove TargetEnvironment=Unity.
    – PhoneixS
    Oct 18, 2018 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .