How to find other copies of same shell script running inside shell?

How to find other copies of same shell script running inside shell?

Post by Robert Yode » Mon, 25 Jan 1999 04:00:00




> Q: How to find other copies of same shell script running inside shell?
> The easy answer does not always work:
>     i=${0##*/}
>     j=$(echo $i | cut -c1-8)
>     if [[ $(ps -e | grep -c "[0-9] $j") -gt 1 ]]
>     then
>       echo "another instance is running."
>     fi
> Because the shell script may fork a child process. Sometimes it does
> sometimes it does not. What is the best answer? Thanks.

If you are running Solaris 7, the new commands "pgrep" and "pkill"
may be useful to you.

ry
--

"Unix:  The Solution to the W2K Problem."

.

 
 
 

How to find other copies of same shell script running inside shell?

Post by Michael Wa » Tue, 26 Jan 1999 04:00:00


Q: How to find other copies of same shell script running inside shell?
The easy answer does not always work:
    i=${0##*/}
    j=$(echo $i | cut -c1-8)
    if [[ $(ps -e | grep -c "[0-9] $j") -gt 1 ]]
    then
      echo "another instance is running."
    fi
Because the shell script may fork a child process. Sometimes it does
sometimes it does not. What is the best answer? Thanks.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog


 
 
 

How to find other copies of same shell script running inside shell?

Post by Michael Wa » Tue, 26 Jan 1999 04:00:00



Quote:>> Q: How to find other copies of same shell script running inside shell?

>If you are running Solaris 7, the new commands "pgrep" and "pkill"
>may be useful to you.

How can the new commands "pgrep" and "pkill" be the answer for my question?
Thanks.

--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

How to find other copies of same shell script running inside shell?

Post by Bernd Finge » Tue, 26 Jan 1999 04:00:00



> Q: How to find other copies of same shell script running inside shell?
> The easy answer does not always work:
>     i=${0##*/}
>     j=$(echo $i | cut -c1-8)
>     if [[ $(ps -e | grep -c "[0-9] $j") -gt 1 ]]
>     then
>       echo "another instance is running."
>     fi
> Because the shell script may fork a child process. Sometimes it does sometimes it
> does not. What is the best answer? Thanks.

I think you could use:

NUM_PROCESSES=$(ps -ef | \
awk '/SCRIPT_NAME/ && !/awk/{a=a+1}END{print a}')

to count running processes... (replace SCRIPT_NAME by the name of your script)

> --
> unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
> submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog


Bernd Finger, Walldorf, Germany
 
 
 

How to find other copies of same shell script running inside shell?

Post by Ralf Draege » Tue, 26 Jan 1999 04:00:00



> Q: How to find other copies of same shell script running inside shell?
> The easy answer does not always work:
>     i=${0##*/}
>     j=$(echo $i | cut -c1-8)
>     if [[ $(ps -e | grep -c "[0-9] $j") -gt 1 ]]
>     then
>       echo "another instance is running."
>     fi
> Because the shell script may fork a child process. Sometimes it does
> sometimes it does not. What is the best answer? Thanks.
> --
> unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
> submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog


Another way is to create lockdirs.

If you don't want two instances of the script include a

mkdir /tmp/locks/yourscript || ( echo "Oops, I'm running"; exit 1)
echo $$ > /tmp/locks/yourscript/pid

(or a while statement if you want to wait for the other process)

If you want multible instances you can do something like

PID=$$
touch /tmp/locks/yourscript/$PID

Here's an example what I'm dooing in my Backupscript:

while ! mkdir /tmp/backup/lockdir > /dev/null 2>&1
do
        sleep 100
        if [ -f /tmp/backup/lockdir/pid ] ; then
                OLDPID=$( cat /tmp/backup/lockdir/pid )
                if ! ps -ef \
                     | awk '{print $2}' \
                     | grep $OLDPID > /dev/null 2>&1; then
                        echo "Backup $OLDPID hasn't completet and isn't running" >> $ERR
                        rm /tmp/backup/lockdir/pid
                        rmdir /tmp/backup/lockdir
                fi
        fi
done
echo $$ > /tmp/backup/lockdir/pid

Don't forget to rm these files and use the trap command to get rid of them
even when the script is killed.

Hope this helps, Ralf.
--

- Intraplan Consult Gmbh  Orleansplatz 5a  81667 Muenchen  +49 89 45911-0 -

God, root, what is the difference? -Pitr (www.userfriendly.org: 11/11/1998)

 
 
 

How to find other copies of same shell script running inside shell?

Post by Michael Wa » Tue, 26 Jan 1999 04:00:00



Quote:

>Another way is to create lockdirs.

>Don't forget to rm these files and use the trap command to get rid of them
>even when the script is killed.

This approach works well only at normal situations. If the script has
an error and exit out, then you can never run the script again until
you manually delete the dir, example:

#!/bin/ksh

mkdir lock
if [[ [ "a" = "a" ]]    # error here
then
  echo b
fi
rmdir lock              # lock never gets removed

So that is why it is better to check process or something which will gone
if the process exits in one way or the other.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

How to find other copies of same shell script running inside shell?

Post by Michael Wa » Tue, 26 Jan 1999 04:00:00


Quote:>> Q: How to find other copies of same shell script running inside shell?
>> The easy answer does not always work:
>>     i=${0##*/}
>>     j=$(echo $i | cut -c1-8)
>>     if [[ $(ps -e | grep -c "[0-9] $j") -gt 1 ]]
>>     then
>>       echo "another instance is running."
>>     fi
>> Because the shell script may fork a child process. Sometimes it does
>> sometimes it does not. What is the best answer? Thanks.

>I think you could use:

>NUM_PROCESSES=$(ps -ef | \
>awk '/SCRIPT_NAME/ && !/awk/{a=a+1}END{print a}')

>to count running processes... (replace SCRIPT_NAME by the name of your script)

OK, I can count running processes, but then how do I know these processes
belong to the same process tree (no duplicate) or different process tree
(duplicate)? This is what I am asking.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

How to find other copies of same shell script running inside shell?

Post by Ralf Draege » Wed, 27 Jan 1999 04:00:00




> >Another way is to create lockdirs.

> >Don't forget to rm these files and use the trap command to get rid of them
> >even when the script is killed.

> This approach works well only at normal situations. If the script has
> an error and exit out, then you can never run the script again until
> you manually delete the dir, example:

> #!/bin/ksh

> mkdir lock
> if [[ [ "a" = "a" ]]    # error here
> then
>   echo b
> fi
> rmdir lock              # lock never gets removed

> So that is why it is better to check process or something which will gone
> if the process exits in one way or the other.

That's why I'm saving the PID. In my code there were the lines
        if [ -f /tmp/backup/lockdir/pid ] ; then
                OLDPID=$( cat /tmp/backup/lockdir/pid )
                if ! ps -ef \
                     | awk '{print $2}' \
                     | grep $OLDPID > /dev/null 2>&1; then
                        echo "Backup $OLDPID hasn't completet and isn't running"
$ERR
                        rm /tmp/backup/lockdir/pid
                        rmdir /tmp/backup/lockdir
                fi
        fi
which check weather the last instance of the code is running or not.
The echo warning should give you a hint something goes wrong.

Btw.: Scripts should be tested before they are running and I'll guess
with a little testing you'll find an error like the the one you mentioned
above. Before I was using this script in my real environment I did 2
days of coding and another two of testing for about 1000 lines of
script an the script runs without problems scince a year everyday in two
instances.

Regards, Ralf.
--

- Intraplan Consult Gmbh  Orleansplatz 5a  81667 Muenchen  +49 89 45911-0 -

God, root, what is the difference? -Pitr (www.userfriendly.org: 11/11/1998)

 
 
 

How to find other copies of same shell script running inside shell?

Post by Michael Wa » Wed, 27 Jan 1999 04:00:00


Quote:>if [ -f /tmp/backup/lockdir/pid ] ; then
>        OLDPID=$( cat /tmp/backup/lockdir/pid )
>        if ! ps -ef \
>             | awk '{print $2}' \
>             | grep $OLDPID > /dev/null 2>&1; then
>                echo "Backup $OLDPID hasn't completet and isn't running">>$ERR
>                rm /tmp/backup/lockdir/pid
>                rmdir /tmp/backup/lockdir
>        fi
>fi

checking PID is better than checking lockdir alone. much better. But there
still a good chance that the PID is used by other programs and that you
think a duplicate copy is running. I think checking PID and the program
name will significantly reduce this type of error. Thanks for the idea.
--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog

 
 
 

How to find other copies of same shell script running inside shell?

Post by Ralf Draege » Wed, 27 Jan 1999 04:00:00



> >if [ -f /tmp/backup/lockdir/pid ] ; then
> >        OLDPID=$( cat /tmp/backup/lockdir/pid )
> >        if ! ps -ef \
> >             | awk '{print $2}' \
> >             | grep $OLDPID > /dev/null 2>&1; then
> >                echo "Backup $OLDPID hasn't completet and isn't running">>$ERR
> >                rm /tmp/backup/lockdir/pid
> >                rmdir /tmp/backup/lockdir
> >        fi
> >fi

> checking PID is better than checking lockdir alone. much better. But there
> still a good chance that the PID is used by other programs and that you
> think a duplicate copy is running. I think checking PID and the program
> name will significantly reduce this type of error. Thanks for the idea.

And another one:
If $OLDPID exists and the old script is running which can be easily checked,
then it's a good idea to test weather it works fine ore something goes
wrong. If your script produces network traffic, you could test this with the
snoop command (since this posting goes also to comp.unix.solaris, I'd guess
you use it und thus have snoop).

Here's a code which does this (Thx to I. Sparry, who gave me the hint how
to do this :-). In my case the problem was, that if the backupprocess on
the client hangs or the client goes down during the backup, the program
on the backuphost just waited for data all the time since there is no
timeout spcified ...
The following code runs within the Backupscript:

startbackupjob &
BACKUP_PID=$!
sleep 60
if ps -p $BACKUP_PID > /dev/null 2>&1
then
        while testthebackup $HOST $BACKUP_PID >> /dev/null 2 >&1
        do
                sleep 600
                if ! ps -p $BACKUP_PID > /dev/null 2>&1
then
                        break
                fi
        done
fi
if ps -p $BACKUP_PID > /dev/null 2>&1
then
        if [ rsh $HOST ls > /dev/null 2>&1 ]; then
                rsh $HOST ps -ef | grep backupclient | einszeil $HOST >> $ERR
        fi
        kill -9 $BACKUP_PID >> $ERR 2>&1
fi

(einszeil $HOST just inserts $HOST at the beginning of each line of input)

And testthebackup is:

#!/bin/sh
HOST=$1
BACKUP_PID=$2
trap 'exit 1' 8
(sleep 600 ; kill -8 $$ ) &
SLEEP_PID=$!

if [ ! $HOST = $BACKUP ]
then
        snoop -d nf0 -P backupserver and $HOST | head > /dev/null
fi
kill -9 $SLEEP_PID
exit 0

Hope this helps, Ralf.
--

- Intraplan Consult Gmbh  Orleansplatz 5a  81667 Muenchen  +49 89 45911-0 -

God, root, what is the difference? -Pitr (www.userfriendly.org: 11/11/1998)

 
 
 

1. running 'alias' from inside shell script problem

Hey all,

I have a problem with a shell script I use to backup some data bases. This
script stops the data bases, and moves them to a test area and restarts them
as well as the test ones just copied. I need to set a variable during this
process to allow me to start them BOTH back up, i have an alias that is set
in the users profile as follows: alias
start_pads='PROCFG=/usr/dlc/progress.cfg.pads; export PROCFG'

typing start_pads at the shell promt works fine for any user, but when
called from from my script it says :

dbcopy () {
  checksite ${SITENAME}
  runcmd ${tst_LOC}/${tst_NAME}/stop_${tst_NAME}_servers
  runcmd ${PULSE_BIN}/prorest_site.sh ${tst_NAME} ${bup_ONLINE}/${SITENAME}
  start_pads
  runcmd bpro -pf ${tst_LOC}/${tst_NAME}/ut/common.pf \
             -U pulse -p ${PULSE_BIN}/set_testsys.p
  runcmd ${tst_LOC}/${tst_NAME}/start_${tst_NAME}_servers
  exit

it trys to execute 'start_pads like a program, not understanding its an
alias,

TIA

Regards

Oliver

2. mount VFAT partition in Linux

3. Special characters in find inside shell script

4. Mouse only moves up&right

5. c-shell script won't run from korn-shell / SETUID

6. Problem: Linux doesnt recognize my PCI NIC

7. How to run shell script without login shell?

8. newbie stb 128

9. how to run your tcsh shell script in the new shell

10. handling SIGINT in shell scripts when executing another shell script.

11. Q: How can I have a shell script call another shell script...

12. Copy files using filenames from text files with shell script or bash script

13. Shell script invoking other shell scripts