Issue
I have a process myprocess
that I run from the script. Is it possible to check if this process executed successfully or crashed?
This is what my script looks like:
myprocess 12 &
sleep 12
# I want to check here if the process crashed
The reason why I am running the process in the background is simple. I want to do other tasks before sleep
ing. After I wake up, I want to see if the process exited gracefully or if it crashed (dumped core).
PS: Please comment below if any other details or more code is required.
Solution
Assuming that you DON'T HAVE a file named core
on your $PWD
(otherwise it will be removed), this is the lazy way to do it:
(Note that this [the lazy core file approach only] assumes the correct parametrization of sysctl fs.suid_dumpable
if myprocess
will have its privilege levels changed or is execute only. Also note that kernel.core_pattern
settings may cause the core file to be dumped somewhere else and not at $PWD
. See this article in order to properly set it. Thanks to @alvits for pointing out these two potential issues. Anyway, I don't really recommend the usage of the following core file approach.)
#!/bin/bash
rm -f core
ulimit -c unlimited
myprocess 12 &
# ... Do your stuff ...
sleep 12
if [ -f core ]; then
echo "This is a minimal example to show that the program dumped core."
fi
Also note that this only works if meanwhile nothing else dumps a core to $PWD
A cleaner approach:
#!/bin/bash
(rm -f /tmp/myprocess.success && myprocess 12 && touch /tmp/myprocess.success) &
# ... Do your stuff ...
sleep 12
if [ -f /tmp/myprocess.success ]; then
echo "myprocess returned EXIT_SUCCESS. But if it didn't returned before sleep 12 elapsed, this will fail."
fi
The correct way to do it:
#!/bin/bash
myprocess &
# Store myprocess PID
MYPROCESS_PID="$!"
# ... Do your stuff here ....
# Wait until myprocess returns
wait "${MYPROCESS_PID}"
# Store myprocess exit status (returned by wait)
$ret="$?"
if [ "${ret}" -gt 0 ]; then
echo "Myprocess didn't exited with EXIT_SUCCESS"
fi
Answered By - pah Answer Checked By - Gilberto Lyons (WPSolving Admin)