Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the gnome-run application in my home folder. I have now added the application to run when I press Meta+R (i added it in in ccsm). The problem is that if I have the gnome-run program open and I press the key combination I want the application to close. Is there some way to create a bash file that checks if the applications is running, if it is then close it, else launch it.

I run the application like ./gnome-run in my home folder.
I can't find the application process in ps -A or anything.

Thanks

share|improve this question
    
have you tried pgrep? eg pgrep xterm returns the pids of my running xterm's. You could check if your app is already running using this (run pgrep yourapp and check if it's return code is 0 or 1 using echo $?). –  romeovs Apr 1 '12 at 18:16
add comment

1 Answer 1

up vote 5 down vote accepted

This shell script should handle the starting and stopping of any program:

#!/bin/bash

BASECMD=${1%%\ *}
PID=$(pgrep "$BASECMD")
if [ "$?" -eq "0" ]; then
    echo "at least one instance of "$BASECMD" found, killing all instances"
    kill $PID
else
    echo "no running instances of "$BASECMD" found, starting one"
    $1
 fi

let's say you saved it under ~/mystarter, you can run any command with it using ~/mystarter <name>, eg in your case, bind Meta+R to:

~/mystarter gnome-run

and make sure the script is executable: chmod u+x ~/mystarter. Also it's probably best to put it somewhere in your PATH, so you don't have to type it's full location every time.

As for the fact that gnome-run doesn't show up in ps -A, make sure that gnome run itself isn't a script that launches the actual process. Check if there is a difference between ps -A | wc -l before and after launching it (this counts all running processes).

Edit:

Since you've accepted the answer, I thought I'd add support for running commands that have commandline arguments, so that this might become a place of reference. Run a command like so:

 ./mystarter 'cmd args'

eg:

./mystarter 'ncmpcpp -c ~/.ncmpcpp'

The command just looks up ncmpcpp to see if it's running already, but executes the full command (with arguments) when ncmpcpp wasn't running.

share|improve this answer
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.