指定core文件生成位置

针对各个程序可能产生core文件的问题,可以采用设置系统统一的core文件目录,所有程序均将core文件生成到指定目录的方式,具体方法如下:

1. 创建存放core文件的目录并添加写入权限

以root用户执行如下命令

1
2
3
mkdir /corefile
chown -R root:xmail /corefile #改变目录属组为xmail组
chmod g+w /corefile #赋予属组用户写权限

2. 打开所有用户下的 ulimit -c unlimited

以root用户执行如下命令你个,

1
vi /etc/profile

在文件的最后添加如下代码,确保各个用户均可产生core文件

1
ulimit -c unlimited

3. 将所有core文件生成路径指向固定位置

以root用户执行如下命令,将所有生成的core文件放到/corefile/目录下,以core-命令名-pid-unix时间戳来命名产生的core文件,其中/corefile目录需所有用户均有写入权限

1
2
echo "/tmp/core-%e-%p-%t" >/proc/sys/kernel/core_pattern
echo "1" > /proc/sys/kernel/core_uses_pid

以下是core-后的参数列表:

1
2
3
4
5
6
7
8
%p - insert pid into filename 添加pid
%u - insert current uid into filename 添加当前uid
%g - insert current gid into filename 添加当前gid
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加命令名
nagios通过扫描core文件目录是否有新的core文件来决定是否发邮件通知

4. 说明:

1
2
1、如果系统在修改/etc/profile前已经重启了一些服务,那么ulimit -c unlimited对已经在ulimit -c 为的0的情况下启动的服务无效,
2、如果服务在ulimit -c unlimited情况下启动,则设置/proc/sys/kernel/core_pattern即时生效

5. 备注

以下方式将core文件生成到当前目录下

1
echo "|/usr/libexec/abrt-hook-ccpp %s %c %p %u %g %t e" >/proc/sys/kernel/core_pattern  

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!