Skip to content
systemcoding.dev
Debugging

Name core files by program and PID with %e and %p in core_pattern

Name Linux core files after the program and PID with kernel.core_pattern and the %e and %p specifiers, so crashes do not overwrite each other.

1 min read
Summarize in AI

Every crash writes a file called core in the crashing process's working directory, and the second crash overwrites the first. kernel.core_pattern is the template the kernel uses for that name, and %e and %p put the program name and the PID in it, so each crash gets its own file.

Text
sudo sysctl -w kernel.core_pattern=/var/cores/core.%e.%p
mkdir -p /var/cores && chmod 1777 /var/cores
ulimit -c unlimited
sleep 100 &
kill -SEGV $!
ls /var/cores
Text
core.sleep.4242

The PID will differ. Other specifiers include %t for the time of the dump in seconds since the epoch, %h for the hostname and %s for the signal number, all listed in core(5) for Linux 6.12. Two limits catch people. %e is the process's comm value, which is cut to 15 characters. And the directory must be writable by the crashing process, since the kernel writes the file as that user, or nothing is written and nothing says so. If the pattern starts with a pipe character, the kernel runs a program instead and gives it the dump on stdin, which is how systemd-coredump works, so check cat /proc/sys/kernel/core_pattern first on a systemd machine.

Taggedclinux
Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.