Other A way to execute a command when process dumps a core?

I'm looking for a way to bind a command to be executed when process dumps core (i.e. at application crash). Cannot find a sysctl for that. Anyone knows another way? Perhaps kernel will emit some kind of notification event that can be received and processed on the userland, hopefully configuring and running an existing tool?

An example use would be to install such a handler, that would compress app.core file, along with some host/site information, and will upload it for further analysis.
 
Look into /etc/defaults/rc.conf, I'm not sure but these settings might be useful (note: I'm assuming that 'crash dump' refers to dumping core):

Code:
crashinfo_enable="YES"  # Automatically generate crash dump summary.
crashinfo_program="/usr/sbin/crashinfo" # Script to generate crash dump summary.
 
I don't think you can do that with a second command (a second executable). Why? The only information the "shell" which ran the first program gets about how the first program ended is the return status. And "crash" or "core dump" does not have a unique return status; matter of fact, nothing prevents me from writing a program that has an exit handler which returns a status = 0 at the end, even if it internally completely blew sky high.

What you can accomplish: catch the case when a program finishes with a non-zero exit status. Here is a snippet you could use in a shell script:
Code:
my_program -option argument1 argument2
if [ $? -ne 0 ] ; then
    my_crash_cleaner ...
fi
I don't know a way to tell the usual shells to do this automatically. You could download the source code for sh (bash, ksh) or tcsh and modify it to automatically run a second program if the first one fails. Or you could write a smallish shell script which reads one line of input from the command line, executes it through a shell of your choice, and if the result is non-zero immediately runs the second program.

A better solution is to modify the program itself. Various programming languages have ways to implement exit handlers, which get run right before a program exits. In languages with exception handling (like Java or Python) you can combine that with putting an outer ring around the program, which catches and handles exceptions. That outer ring or exit handler could then do the functionality you desire from the second program.
 
I'm looking for a way to bind a command to be executed when process dumps core (i.e. at application crash). Cannot find a sysctl for that. Anyone knows another way? Perhaps kernel will emit some kind of notification event that can be received and processed on the userland, hopefully configuring and running an existing tool?

An example use would be to install such a handler, that would compress app.core file, along with some host/site information, and will upload it for further analysis.
Look into kern.coredump_devctl. If you enable it you should be able to create a devd action to run your command. See the example at the end of /etc/devd.conf.
 
I don't think you can do that with a second command (a second executable). Why? The only information the "shell" which ran the first program gets about how the first program ended is the return status. And "crash" or "core dump" does not have a unique return status; matter of fact, nothing prevents me from writing a program that has an exit handler which returns a status = 0 at the end, even if it internally completely blew sky high.

What you can accomplish: catch the case when a program finishes with a non-zero exit status. Here is a snippet you could use in a shell script:
Code:
my_program -option argument1 argument2
if [ $? -ne 0 ] ; then
    my_crash_cleaner ...
fi
I don't know a way to tell the usual shells to do this automatically. You could download the source code for sh (bash, ksh) or tcsh and modify it to automatically run a second program if the first one fails. Or you could write a smallish shell script which reads one line of input from the command line, executes it through a shell of your choice, and if the result is non-zero immediately runs the second program.

A better solution is to modify the program itself. Various programming languages have ways to implement exit handlers, which get run right before a program exits. In languages with exception handling (like Java or Python) you can combine that with putting an outer ring around the program, which catches and handles exceptions. That outer ring or exit handler could then do the functionality you desire from the second program.
I've done this on a Linux embedded box; when a process is terminated by a signal, that is reflected in process' exit code. I'm not sure if it is possible to fake such exit code programatically. However that would require explicit exit code check and will catch only specific process, not any (like some forked daemon).

Look into kern.coredump_devctl. If you enable it you should be able to create a devd action to run your command. See the example at the end of /etc/devd.conf.
This seems exactly what I need. Will look into it. Thanks!
 
Back
Top