Author Topic: Run Only One Copy Of Program?  (Read 5280 times)

desertrat

  • Hero Member
  • *****
  • Posts: 743
    • View Profile
    • http://
Run Only One Copy Of Program?
« on: March 24, 2006, 03:41:24 am »
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
SL-C3100 / Ambicon WL1100C-CF / pdaXrom 1.1.0beta3 / IceWM

gromituk

  • Full Member
  • ***
  • Posts: 207
    • View Profile
Run Only One Copy Of Program?
« Reply #1 on: March 24, 2006, 05:16:47 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.

desertrat

  • Hero Member
  • *****
  • Posts: 743
    • View Profile
    • http://
Run Only One Copy Of Program?
« Reply #2 on: March 24, 2006, 06:03:53 am »
Quote
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

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
SL-C3100 / Ambicon WL1100C-CF / pdaXrom 1.1.0beta3 / IceWM

pungipungi

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • http://members.cheese.at/woody/zaurus
Run Only One Copy Of Program?
« Reply #3 on: March 25, 2006, 02:16:21 pm »
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 ....
  but that's not the easy way

Andreas
SL5500G OZ3.5.3 (GPE) , SD 1G,   D-Link air 660 wlan
C1000 pdaXrom, SD 256, D-Link air 660 wlan
Suse 9.3
My Zaurus Page

grog

  • Hero Member
  • *****
  • Posts: 692
    • View Profile
    • http://
Run Only One Copy Of Program?
« Reply #4 on: March 25, 2006, 02:49:55 pm »
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: [Select]
#!/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
GROG!

Armagon

  • Full Member
  • ***
  • Posts: 106
    • View Profile
    • http://
Run Only One Copy Of Program?
« Reply #5 on: March 26, 2006, 12:24:01 am »
Quote
Quote
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

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
[div align=\"right\"][a href=\"index.php?act=findpost&pid=120067\"][{POST_SNAPBACK}][/a][/div]

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: [Select]
Exec=mb-applet-launcher -o -1 --desktop /usr/share/applications/aterm.desktop
I wonder if you can leverage off of that?

Armagon
Sharp Zaurus SL-C1000 -- 2 GB SD Card -- Zippy Slim Mini Keyboard -- Loving pdaXii13!