Help - Search - Members - Calendar
Full Version: Run Only One Copy Of Program?
OESF Forums > Distros, Development, and Model Specific Forums > Distro Support and Discussion > pdaXrom
desertrat
Having mapped some often used programs onto the application keys the problem is that whenever the application key is pressed it just keeps starting a new copy of the mapped program.

What I would is that when an application key is pressed:

1) if mapped program is not running, start it
2) if already running bring it to front

Is this possible?

-- cheers
gromituk
I suppose you could map the key to a shell script which does a "ps -A|grep <name>" to see if a copy of the application is already running, and start it if not. Don't know how to bring it to the front though, but having the PID could be useful here! More rigorous would be for it to get the PID of the app when it starts it up and store it in a file somewhere.
desertrat
QUOTE(gromituk @ Mar 24 2006, 10:16 AM)
I suppose you could map the key to a shell script which does a "ps -A|grep <name>" to see if a copy of the application is already running, and start it if not.  Don't know how to bring it to the front though, but having the PID could be useful here!  More rigorous would be for it to get the PID of the app when it starts it up and store it in a file somewhere.


That was exactly what I had been thinking, and like you I don't know how to bring an app to front either smile.gif

But I was hoping there was a more elegant solution (ie one that doesn't require so much hard work). The WMs certainly knows what apps are running and are able to "bring it to front", for instance when you Alt-TAB to cycle through windows. If I can hook into that mechanism then I'll be all set. Any ideas?

-- cheers
pungipungi
On the agenda vr3 there was a utility called runone which does some parts of the job
(I think it uses a more stable mechanism via proc files and temp files to check if a program is already runing).
The source can be found on
http://agenda-snow.sourceforge.net/release/source/libs/

Maybe that's a possible base for a start ....
sad.gif but that's not the easy way

Andreas
grog
The 'Standard UNX Way' is for the program to save it's PID into a file under /var/run & remove it when it closes. When the program starts up, it first checks to see if the pidfile exits, and if so then uses the PID in the file to check for an active process. If one exists, then it exits.

Here's a simple bit of code that I use at the top of all of my scripts that I need to only have one instance. It of course has to be run as root, or you could change the permissions of the /var/run directory. All you'd have to do is to write it as a wrapper script for binary programs. The exec command is used to run a binay in the same process (i.e. with the original PID). Otherwise just include the rest of the script right after the echo. The script will clean iself up when it closes because of the trap.

HTH

CODE
#!/bin/sh

PIDFILE=/var/run/${0##*/}.pid

if [ -s $PIDFILE ]; then
   ps -ax | grep "^ *$(cat $PIDFILE).*${0##*/}" >/dev/null 2>&1 && exit
fi

trap "\rm -f $PIDFILE" 0 1 2 3 15
echo $$ >| $PIDFILE

exec myprog
Armagon
QUOTE(desertrat @ Mar 24 2006, 03:03 AM)
QUOTE(gromituk @ Mar 24 2006, 10:16 AM)
I suppose you could map the key to a shell script which does a "ps -A|grep <name>" to see if a copy of the application is already running, and start it if not.  Don't know how to bring it to the front though, but having the PID could be useful here!  More rigorous would be for it to get the PID of the app when it starts it up and store it in a file somewhere.


That was exactly what I had been thinking, and like you I don't know how to bring an app to front either smile.gif

But I was hoping there was a more elegant solution (ie one that doesn't require so much hard work). The WMs certainly knows what apps are running and are able to "bring it to front", for instance when you Alt-TAB to cycle through windows. If I can hook into that mechanism then I'll be all set. Any ideas?

-- cheers
*



I was thinking that the Aterm launcher (beside the start menu on the taskbar, in a default install of pdaXrom 1.1.0beta1) seems to be able to keep track of this. It even seems to be able to toggle the window on and off.

The file is /usr/share/applications/mb-launcher-term.desktop, and it contains this noteable line:
CODE
Exec=mb-applet-launcher -o -1 --desktop /usr/share/applications/aterm.desktop


I wonder if you can leverage off of that?

Armagon
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.