Operating System - HP-UX
1834105 Members
2660 Online
110063 Solutions
New Discussion

How to the background job is running or not?

 
marktam
Occasional Contributor

How to the background job is running or not?

Dear all,
I have write a script that will run another script in backupground.(e.g. a.sh &). Sometime a.sh seem to be hang up. Thus, how can i trace or determine this background is run successfully or failed.

Thanks

Mark
mt
4 REPLIES 4
Steven Sim Kok Leong
Honored Contributor

Re: How to the background job is running or not?

Hi,

A simple method would be to create a wrapper script:

# cat wrapper.sh
a.sh
echo $? > /tmp/a.sh.status

The wrapper script will store the exit code of a.sh into /tmp/a.sh.status

In your main script e.g. main.sh:

# cat main.sh
wrapper.sh &
while sleep 5
do
if grep 0 /tmp/a.sh.status >/dev/null 2>&1
then
echo a.sh has completed successfully
exit 0
fi
done

This is just one method. Hope this helps. Regards.

Steven Sim Kok Leong
Animesh Chakraborty
Honored Contributor

Re: How to the background job is running or not?

Hi,
You should have a log file for any scripts that you run.
like a.sh & >/tmp/log 2>&1
Did you take a backup?
Deepak Extross
Honored Contributor

Re: How to the background job is running or not?

Here's another simple way:

Let a.sh create a "lock file" as soon as it starts up to indicate that it is running. Just before exiting, it should delete this lock file.

a.sh will look like this:
#!/usr/bin/ksh
touch $HOME/lock_file
...do your stuff here...
rm -f $HOME/lock_file

Just before calling a.sh, delete any old lock file that may be present. As long as the lock file exists, you can assume that the backgroung process is running.

You can also add a trp statement in a.sh like so:
trap 'rm -f $HOME/lock_file; exit 1' 2 15
Chris Wilshaw
Honored Contributor

Re: How to the background job is running or not?

For a step-by-step trace try

ksh -x a.sh > /tmp/a.log 2>&1 &

The -x option to ksh will cause it to display all commands (and their output) that it runs to the specified log file

cat /tmp/a.log

+ TERM=vt320
+ PS1=[$HOSTNAME] ${PWD##${PWD%/*/*}/} =>
+ [ -f /home/chris/.profile ]
+ echo 1111
1111
+ echo 2222
2222