Sonu Mishra Sonu Mishra - 1 year ago 63
Bash Question

How to know if a running background process cored

I have a process

that I run from the script. Is it possible to check if this process executed successfully or crashed?

This is how 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 background is simple. I want to do other tasks before
ing. After I wake up, I want to see if the process exited gracefully or it crashed (dumped core).

PS: Please comment below if any other details or more code is required.

pah pah
Answer Source

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.)


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."

Also note that this only works if meanwhile nothing else dumps a core to $PWD

A cleaner approach:


(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."

The correct way to do it:


myprocess &

# Store myprocess PID

# ... Do your stuff here ....

# Wait until myprocess returns

# Store myprocess exit status (returned by wait)

if [ "${ret}" -gt 0 ]; then
    echo "Myprocess didn't exited with EXIT_SUCCESS"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download