Search code examples
c++cunixcroncoredump

How can I still get my core dump when I run something from cron which would normally core?


Today something which I cron and for which I try to detect core dumps and alert me did in fact hit a false assertion (something which normally core dumps it when I run it on the commandline either foreground or background) but no core was dumped. I wrote this simple test:

int main
{
sleep(3);
assert(false);
}

which when I compile and run will core dump all the time. But when I put it on the crontab, i instead got an email from the cron daemon saying:

rocket: main.cpp:10: int main(int, char**): Assertion `false' failed.
/bin/sh: line 1: 32448 Aborted                 ./rocket

and no core file was ever placed in /cores. Why is this and how can I get my core?


Solution

  • For a core file to be generated on a crash, core dumps must be enabled in the current environment. From a shell, this can be done using ulimit:

    ulimit -c unlimited
    

    Which means "set the maximum core dump size to unlimited". Your system is probably configured to do this in interactive shells, but not in cron jobs. To do it from a cron job, you need to modify this limit. If the cron job is a shell script which calls other programs, you can just call ulimit as above. On the other hand, if the job is an executable, you can create a wrapper to run it with:

    #!/bin/bash
    ulimit -c unlimited
    exec "$@"
    

    Another option is to modify the program to set the limit itself using the setrlimit function.

    As to why your cores are going in /cores as opposed to the working directory: your distribution may have adjusted the core pattern, possibly using a program to handle core files and place them in /cores.