vinod
asked on
Howto find exit status of child process
In a bash script running in a cron job on my Linux box, I fork a child process to test something. The child process may exit with exit status of 0 or 1 within one second, or may hang waiting for something else. If it hangs, I simply kill it after a timeout of 5 secs. If it exits I need to know the exit status. How can I get exit status of the child process? Normally I could sync the parent to the child with "wait $child_pid" but I can't because of possibility of hang.
Vinod
Vinod
when process exits , you can check the $? value. 0 means successful, 1 means non-successful
ASKER
How?
you may check it by
echo $?
or
if [ $? -eq 0 ]
then
echo "successful"
else
echo "unsuccessful"
fi
echo $?
or
if [ $? -eq 0 ]
then
echo "successful"
else
echo "unsuccessful"
fi
ASKER
Let me ask the question with a pseudo script called in my cron job:
#!/bin/bash
child_script &
timeout = 5
while [ $timeout -gt 0 ]; do
if [ `child_still_running` = yes ]; then
timeout = $((timeout -1))
sleep 1
else # child exited, do something based on its exit status
if [ $child_exit_status -eq 0 ]; then
do_something;
exit 0
else
do_something_else;
exit 1
fi
fi
done
echo "child_script hanging"
kill %1
exit 2
My question is how do I find child_exit_status so that I could use it in the above code?
#!/bin/bash
child_script &
timeout = 5
while [ $timeout -gt 0 ]; do
if [ `child_still_running` = yes ]; then
timeout = $((timeout -1))
sleep 1
else # child exited, do something based on its exit status
if [ $child_exit_status -eq 0 ]; then
do_something;
exit 0
else
do_something_else;
exit 1
fi
fi
done
echo "child_script hanging"
kill %1
exit 2
My question is how do I find child_exit_status so that I could use it in the above code?
very interesting command wait. here an example
command &
the above will put the command in background.
you may capture its pid via
p=$!
then at a later stage use
wait $p
and it will set $? to the exit code of that process or command
so after calling wait you may check the value of $?
for more info, please see
man wait
command &
the above will put the command in background.
you may capture its pid via
p=$!
then at a later stage use
wait $p
and it will set $? to the exit code of that process or command
so after calling wait you may check the value of $?
for more info, please see
man wait
ASKER
> wait $p
> and it will set $? to the exit code of that process or command
If the child process hangs, so will the parent (wait $p), precisely what I must avoid.
> and it will set $? to the exit code of that process or command
If the child process hangs, so will the parent (wait $p), precisely what I must avoid.
to avoid waiting for a process that hangs, you first check if it finished:
ps -ef | grep -v grep | grep -w $p
this should give you 0 lines if the process finished running, then you use wait $p
ps -ef | grep -v grep | grep -w $p
this should give you 0 lines if the process finished running, then you use wait $p
ASKER
> ps -ef | grep -v grep | grep -w $p
> this should give you 0 lines if the process finished running, then you use wait $p
If you have wait long enough that the child process has exited, do you think "wait $p" will work for a non-existent process?
> this should give you 0 lines if the process finished running, then you use wait $p
If you have wait long enough that the child process has exited, do you think "wait $p" will work for a non-existent process?
yes :)
You may try it:
ksh or bash
ls -l /etc/hostz > /dev/null 2>&1 &
you should see the process id of the above command on your screen. Then
p=$!
run different command now like
who
you may check if process $p is running or not
ps -ef | grep -v grep | grep -w $p
then you run
wait $p
echo $?
You may try it:
ksh or bash
ls -l /etc/hostz > /dev/null 2>&1 &
you should see the process id of the above command on your screen. Then
p=$!
run different command now like
who
you may check if process $p is running or not
ps -ef | grep -v grep | grep -w $p
then you run
wait $p
echo $?
ASKER
> yes :)
Well, not for me :( I use bash on RHEL4:
# ls -l /etc/hosts > /dev/null 2>&1 &
[1] 20228
[1]+ Done ls --color=tty -l /etc/hosts >/dev/null 2>&1
# wait 20228 && echo $?
bash: wait: pid 20228 is not a child of this shell
127
Well, not for me :( I use bash on RHEL4:
# ls -l /etc/hosts > /dev/null 2>&1 &
[1] 20228
[1]+ Done ls --color=tty -l /etc/hosts >/dev/null 2>&1
# wait 20228 && echo $?
bash: wait: pid 20228 is not a child of this shell
127
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.