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

Bobi B.

Well-Known Member

Reaction score: 142
Messages: 345

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.
 

ShelLuser

Son of Beastie

Reaction score: 1,726
Messages: 3,546

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.
 
OP
OP
Bobi B.

Bobi B.

Well-Known Member

Reaction score: 142
Messages: 345

crashinfo(8) is for kernel core dumps. I'm looking for a way to hook to process dumping core, to be notified if/when that happens.
 

ralphbsz

Daemon

Reaction score: 1,165
Messages: 1,873

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.
 

tobik@

Daemon
Developer

Reaction score: 1,378
Messages: 1,909

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.
 
OP
OP
Bobi B.

Bobi B.

Well-Known Member

Reaction score: 142
Messages: 345

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!
 
Top