Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1 Script, 2 processes each with a different status

Status
Not open for further replies.

jones33

Programmer
Mar 20, 2007
14
0
0
GB

Hi,

I have a Unix script which I start and then immediately after starting it I check to see if another process with the same script name but not the same current process id is running.

What I noticed is that when I launch this script. Just by calling its name from another script there are 2 distinct processes created for a time. Even though I launch the script once. One process has a status of R and the other has a status of S.

After a few seconds the second process disappears again and only the first one remains.

Could anyone please help me understand why this is happening? Why might one script start 2 distinct processes (each with a different state) at any stage?

Any ideas or suggestions would be much appreciated.
 
Without seeing any code it's hard to say anything ...
Perhaps a fork and exec system calls ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

The script which is causing the problem is launched from another script as follows:
submitJob.sh ${JOB_PROFILE} $CONFIG_OVERRIDE_FILE
STATUS=$?
echo "submitJob.sh status: $STATUS"

Nothing complicated here. Just a call to the script.

The script submitJob.sh starts as follows:
NOW=`date '+%Y%m%d.%H%M%S'`
scriptName=`basename $0`

echo "Starting $scriptName at : $NOW"

RETURN_STATUS=0
CHECK_OUTCOME=
STARTED_JOBID=

# Determine APPHOME
_0=$0
while [ -h $_0 ]; do
_link=`/bin/ls -l $_0 | sed -e 's/.* //'`
case $_link in
/*) _0=$_link ;;
*) _0=`dirname $_0`/$_link ;;
esac
done
APPHOME=`dirname $_0`
APPHOME=`CDPATH= cd $APPHOME/..; pwd`

echo "APPHOME:$APPHOME"

CURRENT_PID=$$
echo "Current pid " $CURRENT_PID
echo "Current matching processes: `/usr/ucb/ps aux grep $scriptName | grep -v grep | grep -v ${CURRENT_PID}`"
PID_DUPLICATE=`/usr/ucb/ps aux grep $scriptName | grep -v grep | grep -v ${CURRENT_PID} | awk '{print $2}'`

echo "duplicate pid is '${PID_DUPLICATE}'"
if [ "_$PID_DUPLICATE" != "_" ] && [ "_$CURRENT_PID" != "_$PID_DUPLICATE" ]
then
send_mail_message "Duplicate instance of $scriptName already running ($PID_DUPLICATE)" "ERROR"
RETURN_STATUS=1
exit $RETURN_STATUS
fi

It is finding a duplicate PID in the above script, which is causing the job to exit. However, this doesn't happen all the time.

The processes running when the script exits are:
user 5216 0.7 0.1 1120 1000 ? S 18:01:10 0:00 /bin/sh submitJob.sh config.sh
user 5256 0.2 0.0 984 336 ? R 18:01:13 0:00 grep submitJob.sh
user 5254 0.0 0.0 1120 304 ? R 18:01:13 0:00 /bin/sh submitJob.sh config.sh

Process 5216 is the "proper" process which exists as long as the script is running.
Process 5254 is the duplicate process which only exists for a short time before disappearing.

Really don't understand how the process 5254 is being created.

Any ideas / suggestions would be much appreciated.
 
I think that each time a script use the `` redirection then it is cloned (fork) and the command is launched (exec).

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV,

but isn't `` just used to evaluate an expression?

What should I be using instead to prevent a new process being created?

Many Thanks
 
I came across this, which is what I think you were saying PHV

When you give a command to the Shell, it will fork a process to execute the command. While the child process is executing the command, the parent will go to sleep. Sleeping means that the process will not use any CPU time. It remains inactive until it is awakened. When the child process has finished executing the command, it dies. The parent process, which is running the Shell, wakes up and prompts you for another command.

Is my understanding of this correct?
When a Unix script is started it has a process id and for each command executed in the script a new process will be spawned and will die as soon as the command is executed.

At the beginnning of my script I want to check if the script I am trying to start is already running. The approach above is picking up child processes spawned by the script. Is there anyway to exclude these chilld processes?

Many Thanks
 
Each child process have a PPID (parent process ID) equal to the script's PID ($$)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV,

do you know if it is possible to return the parent process id with the command /usr/ucb/ps auxwww

I know it's possible with ps -ef

Just wondering about the other command.

Many Thanks
 
Sorry, I don't know your ps flavor.
You may be interested here: faq822-4603

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

the reason I am using the /usr/ucb/ps aux of ps is so that it will not truncate the name of the process returned.

Is there any version of ps that I can use or any flag I can use with ps so that the name of the process will not be truncated and the PPID will also be returned?

Many Thanks
 
At the beginning of a script I am writing I am trying to identify if the script is already running and if it is then exit the script.

At the moment I do a /usr/ucb/ps aux exclude the current process id of the script and look for every other process with the script name. This doesn't work because temporary child processes are picked up because they also have the script name in them.

If the /usr/ucb/ps aux included the PPID then the child processes would be excluded. However, it doesn't do this.

Using/usr/bin/ps would include the PPID however it might truncate the name of the process (limit of 80 character) depending on where the script is launched.

Does anyone have any other suggestions? I would imagine that checking if the script one is about to launch is already running must be a pretty standard thing????
 
Personally, rather than fiddling with awkward ps output, I think a neater way is to use a 'lock' file or similar to prevent duplicate jobs running. It certainly makes for simpler code, something along the lines of:

Code:
#!/bin/ksh

if [[ -f /var/lock/myscript ]]
then
    echo "myscript appears to be running already (PID $(< /var/lock/myscript))"
    exit 1
fi
echo $$ > /var/lock/myscript

# rest of script here

# on clean exit
rm /var/lock/myscript

You could test for the existence of the PID stored in the file as well if you wished.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top