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.
pgrep
? egpgrep xterm
returns the pids of my running xterm's. You could check if your app is already running using this (runpgrep yourapp
and check if it's return code is 0 or 1 usingecho $?
). – romeovs Apr 1 '12 at 18:16