Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid generating core file when killing child_process

Tags:

node.js

I have a Node script that starts up a a mongod and a node process. I have setup a process listener in the script that listens for SIGINT, in case I should cancel using Ctrl-C. The listener then uses child_process#kill() to kill these other two processes

This all works, but it almost always ends up generating a core file from the mongod process. I would like to avoid creating this file. How would I do this? (Yes, I do realize I can just issue a file system call to remove it).

Output

^C[mongod]  2017-07-23T09:23:07.181+0200 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2017-07-23T09:23:07.181+0200 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2017-07-23T09:23:07.181+0200 I NETWORK  [signalProcessingThread] closing listening socket: 6
2017-07-23T09:23:07.181+0200 I NETWORK  [signalProcessingThread] closing listening socket: 7
2017-07-23T09:23:07.181+0200 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-23451.sock
2017-07-23T09:23:07.181+0200 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2017-07-23T09:23:07.181+0200 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
Caught SIGINT. Exiting gracefully ...
like image 841
oligofren Avatar asked Oct 27 '25 18:10

oligofren


1 Answers

When using the kill syscall, you should avoid sending signals whose default action is to terminate with a a core dump (see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html).

If you're not sending such signals, you can eliminate the core dump by setting your process limit on core dumps to 0.

This is done through the rlimit syscall, but since NodeJs probably doesn't have an API for it, you can achieve the same result by spawning the child procesess through a shell where you can set the limit with ulimit -c 0 (the limit is inherited by child processes).

Example:

const child_process = require('child_process')
const child = child_process.spawn("ulimit -c 0; sh -c 'kill -ABRT $$'; ls core", [], {shell: true });
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stdout);

If you run the above with:

rm -f core; nodejs the_script.js

it should output:

Aborted
ls: cannot access core: No such file or directory

If you don't want core dumps anywhere in your nodejs-created process hierarchy, you can set ulimit -c 0 before you start nodejs.

like image 54
PSkocik Avatar answered Oct 30 '25 07:10

PSkocik