SajithP
SajithP

Reputation: 587

Coredumps are not generated when the process is running in the background

My program is written in C/C++ and compiled using -ggdb flag. And want to see corefiles are being generated.

I bring up the program from the below script.

#!/bin/sh
#
# Starts the application.
#
NAME=my_app
test -x /usr/bin/my_app || exit 0

start() {
        echo -n "Starting my app "
        $NAME > /dev/null 2>&1 &
        sysctl -w kernel.core_pattern='/var/core/%e.%p.%h.%t.core'
        ulimit -c unlimited
        echo "OK"
}

For testing I added the below crashing statement.

  char z[100];
  std::strncpy(z, NULL, 100);

I could generate corefiles when my application is run from the shell (bash) or even when it's run in the background using '&'. But when it's brought up from the script above, the corefiles are not generated.

What am I doing wrong here? Or is there any other way to sort this out? Thanks

Upvotes: 0

Views: 249

Answers (1)

Employed Russian
Employed Russian

Reputation: 213386

But when it's brought up from the script above, the corefiles are not generated.

The application will likely produce a core dump if your ulimit -c is set to unlimited before running the script.

Your script does 3 things:

  1. run the application in background
  2. set core pattern
  3. set ulimit -c unlimited.

Note that step 3 is completely pointless: it only affect processes that you create after you set the new limit (in your script, that's just the echo "OK").

What you want to do is perform steps 2, 3 and then 1. When performed in that order, ulimit -c unlimited will actually affect your application (which inherits the limit from current shell at the moment it is created).

Upvotes: 2

Related Questions