Setup core dump (Linux)

Setup core dump (Linux)

【原文】

1. sudo vim /etc/security/limits.conf

#*               soft    core            0

To

# 1GB
*               soft    core             1048576

2. Prepare coredump files path

sudo mkdir -p /corefiles && sudo chmod 1777 /corefiles

3. Set coredump enable config and file name pattern

Format of core file name

  • %% – 符号%
  • %e – 程序文件名
  • %t – 生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970)
  • %p – 进程号
  • %s – 生成 core 文件时收到的信号
  • %h – 主机名
  • %u – 进程用户 id
  • %g – 进程用户组 id

Config

sudo vim /etc/sysctl.conf

kernel.core_uses_pid = 1
kernel.core_pattern = /corefiles/%e-%t-%p.coredump

Fix for ubuntu

If system is ubuntu, remove apport:

sudo apt-get purge apport -y

4. Apply system config

sudo sysctl -p

reboot

5. 查看是否启用了 core dump

ulimit -c

# Or
ulimit -a

临时启用

ulimit -c SIZE

# Or:
ulimit -c unlimited

Make RPM package 创建 RPM 包

Make RPM package 创建 RPM 包

…NOT recommended, maybe better :point_right: DEB/APT … see


  • Example on centos7 18.04.
  • 直接在 源码目录 / GIT 仓库创建
  • 没有 tarball — 没必要
  • 没有 configure — 使用 CMAKE
  • 创建的包不安装到 /usr/...
  • 创建包的根目录不是 ~/... — 就在当前工程下面
  • 此外对于 APP 包还有 strippost install 等需要处理
  • 不要使用 root 用户, 普通用户创建权限足够了

Install building tools

sudo yum install rpm-build
sudo yum install rpmdevtools

创建使用的配置文件 .spec 示例

详见:

## MY_PROJ_NAME.desc
# topdir is here, NOT ~/...
%define _topdir %(pwd)/rpmbuild
# %(pwd)/MY_SRC_DIR
%define srcdir %(pwd)/yuiwongcppbase
# MY_PREFIX
%define prefix opt/lib/yuiwongcppbase
%define builddir %{srcdir}/build
Name:         libyuiwongcppbase-devel
Version:      1.1.1
Release:      1%{?dist}
# MY_SHORT_DESCRIPTION
Summary:      C++ common library.

Group:        yuiwong.org
License:      LGPL-3.0
URL:          https://yuiwong.org/gitlab/cpp/cppbase
Source0:      gitsrc

#BuildRequires:
#Requires:

%description
# MY_DESCRIPTION
C++ common library.

%prep
# NOT xf tarball
#%setup -q

%build
cd %{srcdir}
rm -rf build
mkdir -p build
cd build
echo %{buildroot}
cmake .. \
  -DCMAKE_INSTALL_PREFIX=%{buildroot}/%{prefix} \
  -DENABLETEST=0 \
  -DEIGEN3=/opt/lib/eigen3 \
  -Dboost=/usr
echo "exit 0" > configure
chmod 700 configure
%configure
#make %{?_smp_mflags}
#make

%install
#make install DESTDIR=%{buildroot}
cd %{builddir}
make install
cd -

%files
/%{prefix}
%doc

%clean
rm -rf %{buildroot}/%{prefix}
rm -rf %{builddir}

%changelog

Make

rpmbuild -bb rpmbuild/yuiwongcppbase.spec

Auto startup by service

Auto startup by service

#

Example

  • Notes
    • ### BEGIN INIT INFO + ... + ### END INIT INFO 最好有,
      否则会有一个警告
    • 如果不在主分区, 注意等待分区或者文件系统挂载!
  • E.g. https://yuiwong.org/gitlab/pkgman/debrepo + ubuntu xenial
#!/bin/sh
### BEGIN INIT INFO
# Provides:          yuiwonguploadinotify
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     1 2 3 4 5
# Default-Stop:      0 6
# Short-Description: My upload inotify
# Description:       My upload inotify.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HAS_DEBIAN=true
HAS_UBUNTU=true
prog="/opt/debrepo/bin/yuiwonguploadinotify"
#if [ -f /etc/default/rcS ]; then
#  . /etc/default/rcS
#fi
#. /lib/lsb/init-functions
case "$1" in
start)
  #killall yuiwonguploadinotify
  # Wait mount
  i=0
  echo "Wait mount .."
  #log_progress_msg "Wait mount .."
  while [ $i -lt 60 ]; do
    if [ -e "$prog" ]; then
      break
    fi
    sleep 1
    i=$(($i+1))
  done
  if [ ! -e "$prog" ]; then
    #log_progress_msg "Mount timed out"
    echo "Mount timed out" >&2
    exit 1
  fi
  #log_progress_msg "Mounted"
  echo "Mounted"
  if [ "true" = "${HAS_UBUNTU}" ]; then
    "$prog" yuiwong /my/debrepo/.ubuntuincoming \
      /my/debrepo/tools/reprepro-import_ubuntu /opt/debrepo/ubuntu-upload.log &
  fi
  if [ "true" = "${HAS_DEBIAN}" ]; then
    "$prog" yuiwong /my/debrepo/.debianincoming \
      /my/debrepo/tools/reprepro-import_debian /opt/debrepo/debian-upload.log &
  fi
  ;;
stop)
  killall yuiwonguploadinotify
  exit 0
  ;;
restart)
  $0 stop
  sleep 1
  $0 start
  ;;
*)
  echo "Usage: $0 {start|stop|restart}" >&2
  exit 1
  ;;
esac
exit 0

Install

E.g.

install_service: build
ifeq ($(REPO_ROOT_PATH),)
    @echo "No REPO_ROOT_PATH=..." > /dev/stderr
    @exit 1
endif
ifeq ($(IMPORTER_USER),)
    @echo "No IMPORTER_USER=..." > /dev/stderr
    @exit 1
endif
    @/bin/cp -f yuiwonguploadinotifyd.in yuiwonguploadinotifyd
    sed -i "s/REPO_ROOT_PATH/$$(echo -n $(REPO_ROOT_PATH) | sed 's/\//\\\//g')/g" yuiwonguploadinotifyd
    sed -i "s/IMPORTER_USER/$$(echo -n $(IMPORTER_USER) | sed 's/\//\\\//g')/g" yuiwonguploadinotifyd
    chmod a+x yuiwonguploadinotifyd
    cat yuiwonguploadinotifyd
    sudo cp yuiwonguploadinotifyd /etc/init.d/
    cd /etc/init.d/ && sudo update-rc.d yuiwonguploadinotifyd defaults 95

Uninstall

E.g.

uninstall_service:
    @test -n /etc/init.d/yuiwonguploadinotifyd \
    || sudo service yuiwonguploadinotifyd stop \
    && sudo update-rc.d -f yuiwonguploadinotifyd remove \
    && sudo rm -f yuiwonguploadinotifyd /etc/init.d/yuiwonguploadinotifyd \
    && sudo systemctl daemon-reload
    @sudo killall yuiwonguploadinotify || true

https://yuiwong.org/gitlab/computerscience/use-unix