可以在/etc/sysctl.conf里设置。
cat sysctl.conf
kernel.core_pattern =%e.core.%p
kernel.core_uses_pid = 0
%e: 可执行文件名
%p: 进程的PID
%u: real uid of the process
%g: real gid of the process
%t: the time when the core is dumped
%h: host name
%c: the max size of the core dump file
在上面的设置下,如果一个名为 test的程序产生了core dump, 则他的名字为 test.core.123 (这里假设其pid为 123)
如果在 sysctl.conf里改变了设置,之后要执行 sysctl -p 使之生效。
设置生成压缩的 core文件
首先,创建文件 core_helper:
#cat usr/sbin/core_helper
#!/bin/sh
exec gzip - > /root/$1.core.$2.gz
修改 sysctl.conf文件内容:
#cat etc/sysctl.conf
kernel.core_pattern = |/usr/sbin/core_helper %e %p
kernel.core_uses_pid = 0
#cat /proc/sys/kernel/core_pattern
|/usr/sbin/core_helper %e %p
重新执行 sysctl -p 之后,就会生成 test.core.1234.gz 这样的文件。
man core 里有对于这个管道符号的解释:
Since kernel 2.6.19, Linux supports an alternate syntax for the
/proc/sys/kernel/core_pattern file. If the first character of this
file is a pipe symbol (|), then the remainder of the line is inter‐
preted as a program to be executed. Instead of being written to a disk
file, the core dump is given as standard input to the program. Note
the following points:
* The program must be specified using an absolute pathname (or a path‐
name relative to the root directory, /), and must immediately follow
the '|' character.
* The process created to run the program runs as user and group root.
* Command-line arguments can be supplied to the program (since kernel
2.6.24), delimited by white space (up to a total line length of 128
bytes).
* The command-line arguments can include any of the % specifiers
listed above. For example, to pass the PID of the process that is
being dumped, specify %p in an argument.