Example 1 : Failure case with exit 24
[smpici@c712f6n06 myscripts]$ cat simple_bkGroundproc.sh
#!/bin/sh
cmd() { sleep 5; exit 24; }
cmd & # Run the long running process
pid=$! # Record the pid
# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!
# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0
# Wait for the process to finish
if wait $pid; then
echo "cmd succeeded"
else
echo "cmd FAILED!! (returned $?)"
fi
[sachin@host1 myscripts]$ ./simple_bkGroundproc.sh
Wed Mar 4 06:36:34 EST 2020: 96853 is still running
Wed Mar 4 06:36:35 EST 2020: 96853 is still running
Wed Mar 4 06:36:36 EST 2020: 96853 is still running
Wed Mar 4 06:36:37 EST 2020: 96853 is still running
Wed Mar 4 06:36:38 EST 2020: 96853 is still running
cmd FAILED!! (returned 24)
[sachin@host1 myscripts]$
--------------------------------------------
Example 1 : Passed case with exit 0
#!/bin/sh
cmd() { sleep 5; exit 0; }
cmd & # Run the long running process
pid=$! # Record the pid
# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!
# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0
# Wait for the process to finish
if wait $pid; then
echo "cmd succeeded"
else
echo "cmd FAILED!! (returned $?)"
fi
------------
OUTPUT
[sachin@host1 myscripts]$ ./simple_bkGroundproc.sh
Wed Mar 4 06:37:04 EST 2020: 97118 is still running
Wed Mar 4 06:37:05 EST 2020: 97118 is still running
Wed Mar 4 06:37:06 EST 2020: 97118 is still running
Wed Mar 4 06:37:07 EST 2020: 97118 is still running
Wed Mar 4 06:37:08 EST 2020: 97118 is still running
cmd succeeded
++++++++++++++++++++++++++++++++++++++++
Example 3 :
#! /bin/bash
fvt_test_arg="Default HCOLL IBV_ENABLE pami_noib"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"
items="1 2 3 4"
pids=""
for item in $items; do
sleep $item &
pids+="$! "
done
echo "list of pids $pids"
i=0
for pid in $pids; do
wait $pid
if [ $? -eq 0 ]; then
echo "${fvt_test_arg_array[i]} SUCCESS - Job $pid exited with a status of $?"
else
echo "${fvt_test_arg_array[i]} FAILED - Job $pid exited with a status of $?"
fi
i=$((i+1))
done
-----------------------
OUTPUT:
[sachin@host1 myscripts]$ ./pid_array2.sh
list of pids 81822 81823 81824 81825
Default SUCCESS - Job 81822 exited with a status of 0
HCOLL SUCCESS - Job 81823 exited with a status of 0
IBV_ENABLE SUCCESS - Job 81824 exited with a status of 0
pami_noib SUCCESS - Job 81825 exited with a status of 0
[sachin@host1 myscripts]$
-----------------------
Reference:
https://stackoverflow.com/questions/1570262/get-exit-code-of-a-background-process
[smpici@c712f6n06 myscripts]$ cat simple_bkGroundproc.sh
#!/bin/sh
cmd() { sleep 5; exit 24; }
cmd & # Run the long running process
pid=$! # Record the pid
# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!
# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0
# Wait for the process to finish
if wait $pid; then
echo "cmd succeeded"
else
echo "cmd FAILED!! (returned $?)"
fi
[sachin@host1 myscripts]$ ./simple_bkGroundproc.sh
Wed Mar 4 06:36:34 EST 2020: 96853 is still running
Wed Mar 4 06:36:35 EST 2020: 96853 is still running
Wed Mar 4 06:36:36 EST 2020: 96853 is still running
Wed Mar 4 06:36:37 EST 2020: 96853 is still running
Wed Mar 4 06:36:38 EST 2020: 96853 is still running
cmd FAILED!! (returned 24)
[sachin@host1 myscripts]$
--------------------------------------------
Example 1 : Passed case with exit 0
#!/bin/sh
cmd() { sleep 5; exit 0; }
cmd & # Run the long running process
pid=$! # Record the pid
# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!
# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0
# Wait for the process to finish
if wait $pid; then
echo "cmd succeeded"
else
echo "cmd FAILED!! (returned $?)"
fi
------------
OUTPUT
[sachin@host1 myscripts]$ ./simple_bkGroundproc.sh
Wed Mar 4 06:37:04 EST 2020: 97118 is still running
Wed Mar 4 06:37:05 EST 2020: 97118 is still running
Wed Mar 4 06:37:06 EST 2020: 97118 is still running
Wed Mar 4 06:37:07 EST 2020: 97118 is still running
Wed Mar 4 06:37:08 EST 2020: 97118 is still running
cmd succeeded
++++++++++++++++++++++++++++++++++++++++
Example 3 :
#! /bin/bash
fvt_test_arg="Default HCOLL IBV_ENABLE pami_noib"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"
items="1 2 3 4"
pids=""
for item in $items; do
sleep $item &
pids+="$! "
done
echo "list of pids $pids"
i=0
for pid in $pids; do
wait $pid
if [ $? -eq 0 ]; then
echo "${fvt_test_arg_array[i]} SUCCESS - Job $pid exited with a status of $?"
else
echo "${fvt_test_arg_array[i]} FAILED - Job $pid exited with a status of $?"
fi
i=$((i+1))
done
-----------------------
OUTPUT:
[sachin@host1 myscripts]$ ./pid_array2.sh
list of pids 81822 81823 81824 81825
Default SUCCESS - Job 81822 exited with a status of 0
HCOLL SUCCESS - Job 81823 exited with a status of 0
IBV_ENABLE SUCCESS - Job 81824 exited with a status of 0
pami_noib SUCCESS - Job 81825 exited with a status of 0
[sachin@host1 myscripts]$
-----------------------
Reference:
https://stackoverflow.com/questions/1570262/get-exit-code-of-a-background-process