2

I am trying to write a script that will enable xampp start and xampp stop with the same script without having to write in the terminal everytime.

To start xampp, I write in console :

sudo /opt/lampp/lampp start sudo /opt/lampp/lampp stop

How to make a script executable which allows xampp to start and stop simultaneously when double clicked ? Any idea is always welcomed.

2 Answers 2

4

You'll need to do 2 things to get what you want :

1) Create a simple bash script to start lampp

I've written it for you! It starts lampp if it isn't started and stops it if it's already started :

#!/bin/bash                                           ##This is a bash script

                                                      ##PREREQUISITES
if [[ ! -f /tmp/lampp-startstop ]] ; then             # if temp file doesn't exist
 echo 0 > /tmp/lampp-startstop 2>&1                   # create it and write 0 in it
fi

                                                      ##IF NOT RUNNING
if [ "`cat /tmp/lampp-startstop`" == "0" ] ; then     # if temp file contains 0
 sudo /opt/lampp/lampp start                          # start lampp
 echo 1 > /tmp/lampp-startstop 2>&1                   # write 1 in the temp file
 notify-send "Lampp" "Program started." -i xampp      # send a notification
 exit 0                                               # and exit
fi

                                                      ##IF RUNNING
if [ "`cat /tmp/lampp-startstop`" == "1" ] ; then     # if temp file contains 1
 sudo /opt/lampp/lampp stop                           # stop lampp
 echo 0 > /tmp/lampp-startstop 2>&1                   # write 0 in the temp file
 notify-send "Lampp" "Program stopped." -i xampp      # send a notification
 exit 0                                               # and exit
fi

2) Create a shortcut that will launch that script (on the Desktop for example) :

  • A) Create a .desktop file on the Desktop :

    gedit ~/Desktop/Lampp.desktop 
    
  • B) Enter the following in it :

    [Desktop Entry]
    Name=Lampp
    Comment=Start/Stop Lampp
    Exec=gksu bash /PATH/TO/THE/SCRIPT
    Icon=PATH/TO/THE/ICON
    Terminal=false
    Type=Application
    

    => Remplace the two /PATH/TO/THE/... by something. Icons are stored in /usr/share/icons/ and a good place for the script would be in your HOME folder, maybe hidden (hide by adding a . at the beginning of his name).

  • C) Make it executable :

    sudo chmod +x ~/Desktop/Lampp.desktop
    

Note : the script isn't really checking if lampp is working, it is using a temporary file (disapears at reboot) containing 1 if you have used the script once (meaning it's started) and 0 if you haven't used the script (meaning it isn't started). What does it mean ? That you have to only use this script if you want things to work : do not start lampp without this script and you'll be ok.

Note : you'll have to type your password in order to launch the shortcut. You could bypass that, but that wasn't your question so I will not explain that here.

6
  • i have tried your peice of code and saved it in test.sh.whn i run that script its running successfully. but when lampp.desktop file is saved with ur code its asking for user password and its starting admin but thn no response. failing.i think problem is in .desktop file with EXEC=gksu and icon path.
    – black
    Mar 17, 2014 at 11:42
  • you have to replace "/PATH/TO/THE" with the real path. For example, if the script test.sh is in your HOME directory, the path is ~/test.sh and if it is in the directory ".config", the path is ~/.config/test.sh. Same goes for the icon.
    – MrVaykadji
    Mar 17, 2014 at 12:51
  • You can also sudo apt-get install gksu to install "gksu", but as it is asking for your password, it should be installed...
    – MrVaykadji
    Mar 17, 2014 at 12:52
  • lol. i have chnaged that path to absolute path of .sh file.let the icon path be removed as it will not effect execution.but still no success.its asking for xampp password after entering its exiting.
    – black
    Mar 17, 2014 at 12:53
  • try to remove to "notify-send" lines, it sometimes responds badly to a gksu execution ?
    – MrVaykadji
    Mar 17, 2014 at 13:03
3

You can use a script like this,

#!/bin/bash
#checks if the process is already running or not
ps ax | grep "/opt/lampp/lamp[p]" > /dev/null
#if the process is running exit status $?=0 
if [ $? -eq 0 ]; then
#interactively states the running status of the process and asks permission to
#proceed in a zenity pop-up box
    zenity --question --text="Process is running. select \"yes\" to stop"
    if [ $? -eq 0 ]; then
#stop the running process on approval
    sudo /opt/lampp/lampp stop
    else
        exit 0
    fi
else
#interactively states the running status of the process and asks permission to
#proceed in a zenity pop-up box
    zenity --question --text="Process is not running. select \"yes\" to start"
    if [ $? -eq 0 ]; then
#starts the process on approval
        sudo /opt/lampp/lampp start
    else
        exit 0
    fi
fi

Save the script. Give it execution permission,

chmod +x <script_name>

It should give you Run option on double-clicking on it. If you wish you can make a .desktop file to run it.

Note: See this answer to run sudo /opt/lampp/lampp without password.

2
  • unfortunately, your script doesn't work on my "lubuntu" : the grep command itself is captured and lampp seems always started. So I replace the ps command by : ps aux | grep /opt/lampp | grep -v grep But start or stop commands are not launch on my system. The message itself appears but no command is running. I must run the script from a terminal. And if I run the script directly from the file manager, it ask me if I want to run in a terminal. So I click on "terminal" and nothing happened. So I must open manually a terminal and start manually the command. Not fun. So the script is useless.
    – user328811
    Sep 20, 2014 at 14:30
  • 1. If you have any trouble with redirection of output of grep to /dev/null you can use -q option instead. 2. I just tested it successfully from nautilus using run option. (run in terminal is not needed). Finally, I can not comment on the result you obtained after modification of the script by yourself. P.S As I don't have lampp I tried it with /usr/bin/vlc and used pkill vlc to kill it.
    – sourav c.
    Sep 20, 2014 at 17:01

You must log in to answer this question.

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