Operating System - HP-UX
1848993 Members
6439 Online
104040 Solutions
New Discussion

Re: running scripts at background by order

 
SOLVED
Go to solution
perez Ilan
Occasional Advisor

running scripts at background by order

Hi all,

I'm trying to run a script that submit by many users. it is very important to keep the order (FIFO).
I tried the 'script | at now' and queuedefs 1j (only 1 job at a time). but when a few jobs send one after the other in short time, It does not keep the order.

any idea?
Thanks in advance,
Perez Ilan.
2 REPLIES 2
Steven Sim Kok Leong
Honored Contributor
Solution

Re: running scripts at background by order

Hi,

At the beginning of your script, check whether other instances of your script are running and if they are, check the PID of these instances. At the end of your script run, check that all these PIDs have disappeared from the ps output before exiting.

Of my head (you need to test and refine):

#!/sbin/sh

PIDS=`ps -fae | grep my_script | grep -v grep | awk '{print $2}'`

... # whatever your script runs here

# Exit only when other instances have completed

pidlist=
for pid in `echo $PIDS`
do
pidlist="$pidlist -e $pid"
done

while sleep 5
do
if ! ps -fae | awk '{print $2}' | grep $pidlist >/dev/null 2>&1
then
# none of the forerunning processes are alive
exit
fi
done

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: running scripts at background by order

Hi,

I forgot to consider the currently running script.

Change:

PIDS=`ps -fae | grep my_script | grep -v grep | awk '{print $2}'`

To:

PIDS=`ps -fae | grep my_script | grep -v grep | awk '{print $2}' | grep -v $$`

Hope this helps. Regards.

Steven Sim Kok Leong