Re: being able to get results of shell command

On Thu, May 27, 2004 at 01:33:04PM +0200, Rohan Nicholls wrote:
> I am trying to figure out how to find out if a program is already
> running before starting it up.  I start a number of programs from my
> .sawfishrc and on a restart of sawfish I don't want to start them up
> again.

On Linux I wrote myself the following script, called maybe_restart:

#! /bin/sh
# run a program only if we do not have a copy already
# Michal Jaegermann, michal harddata com, 2002/Feb/10

who="$(id -u)"
iown () {
    [ "$who" = $3 ]
for p in $(/sbin/pidof $1) ; do
    iown $(\ls -ldn /proc/$p) && exit
exec $@

If you will do 'maybe_restart some_program <options>' then
some_program will start only if _your_ copy of some_program is not
running already.  Function 'iown()' can be obviously implemented in
some other way if do not have /proc to peek into directly.  All of
the above can done by trawling through an output from 'ps', with
appropriate options, but I wanted that quick and simple. :-)

The above will clearly fail if the program you are trying to start
is really a "wrapper" which sets up some enviroment and after that
runs "the real thing" (see, for example, galeon or mozilla).  If you
have a list of such programs then you can add it to that script and
"translate" before a search.  Some 'case .... esac' construct will
do.  For my purposes this simple thing was good enough.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]