On 11/10/05, Roy Souther <[EMAIL PROTECTED]> wrote:
Off the top of my head this would be a simple bash script that would call the app. Could look like this.
This is my quick idea. I have not tested it. Could use some work.

===== START BASH SCRIPT =====
#!/bin/bash

OUR_PID=$$

# Get the basename of the program to run
APP_NAME=$0
NANE_LENGTH=${#APP_NAME}
SHORT_NAME_LENGTH=`expr $NANE_LENGTH - 4`
CALLED_APP_NAME=`echo "$0" | cut -b -$SHORT_NAME_LENGTH`

# Use a PID lock file to see if it is already running
LOCK_FILE="$HOME/tmp/${CALLED_APP_NAME}.lock
LOCK_PID=0
if [ -f $LOCK_FILE ]; then
    LOCK_PID=`< $LOCK_FILE`
fi

if [ $LOCK_PID -gt 0 ]; then
    # It was run before, we have a PID but it could be an old PID
    # See if that program is still running by this user, match the PID, the program name and the user
    STILL_RUNNING=`ps -p $LOCK_PID -u | grep ^$USER | grep $CALLED_APP_NAME`

    # Something returned means it is still running
    if [ ! "$STILL_RUNNING" = "" ]; then
        exit 0
    fi

    # Nothing returned means it is an old PID
fi

# Put our PID into the file to stop this script from running twice
# This is fast because the program we call could be slow to start up
echo -n "$OUR_PID" > $LOCK_FILE

# Call the app and give it any passed argument, get the PID and put it into the lock file
$CALLED_APP_NAME $@ &
CALLED_PID=$!
echo -n "$CALLED_PID" > $LOCK_FILE
exit 0
===== START BASH SCRIPT =====

Then use this script as a wrapper for the program. If your program is mozilla in /usr/bin/mozilla make the bash script /usr/bin/mozilla.bsh

If you have a lot of programs you want to wrap use sym links. Save the script as /usr/bin/RunOnlyOnce.bsh
ln -s /usr/bin/RunOnlyOnce.bsh /usr/bin/mozilla.bsh
ln -s /usr/bin/RunOnlyOnce.bsh /usr/bin/gedit.bsh
ln -s /usr/bin/RunOnlyOnce.bsh /usr/bin/kmail.bsh
ln -s /usr/bin/RunOnlyOnce.bsh /usr/bin/someprogram.bsh


Great Roy,
Can you also incorporate the aspect of allowing some users more than one instance? See the reply to OP I have sent. Some thing which ignores single PID for anyone whose name is not there in control file. This way control file does not have to have all users defined.
The two combined will make for great start.


--
Sudev Barar
Learning Linux

Reply via email to