Linux崩溃时启动脚本获取进程相关信息
编写test.cpp
#include <stdlib.h>
#include <stdio.h>
#include <exception>
#include <string.h>
#include <unistd.h>
void terminate_handler()
{
char cmdline[1024] = {0,};
sprintf(cmdline, "bash term.sh %d %d", getpid(), getppid());
// printf("executing %s\n", cmdline);
system(cmdline);
}
int main()
{
std::set_terminate(terminate_handler);
//abort();
int a = 10 / 0;
throw "test";
return 0;
}
Makefile
CC= g++
CC_Flag= -O3
test: test.cpp
$(CC) $(CC_Flag) $< -o $@
term.sh
src_dir=/proc/$1
term_dir=$PWD/term
dst_dir=$term_dir/$1
#if [ -d $src_dir ]; then
# echo source folder \"$src_dir\" existing
#fi
if ! [ -d $term_dir ]; then
mkdir $term_dir
else
rm -rf $term_dir/*
fi
if ! [ -d $dst_dir ]; then
# echo destination folder \" $dst_dir \" doesn\'t existing
mkdir $dst_dir
#else
# echo destination folder \" $dst_dir \" existing
fi
if [ -f $src_dir/status ]; then
cat $src_dir/status > $dst_dir/status
chmod a+w $dst_dir/status
fi
if [ -d $src_dir/task ]; then
#ls $src_dir/task | tee | sed -e "s/^/task /"
for file in $(ls $src_dir/task)
do
thread_dir=$src_dir/task/$file
if [ -d $thread_dir ]; then
if [ -O $thread_dir/stack ]; then
mkdir $dst_dir/$file
sudo cat $thread_dir/stack > $dst_dir/$file/stack
chmod a+w $dst_dir/$file/stack
else
echo "Invalid permissions"
sudo cat $file/stack > $dst_dir/{basename $file}/stack
fi
fi
done
fi
接下来是测试结果
$ ./test
[sudo] password for daniel:
已放弃
$ tree
.
├── Makefile
├── term
│ └── 5886
│ ├── 5886
│ │ └── stack
│ └── status
├── term.sh
├── test
└── test.cpp
3 directories, 6 files
$ cat term/5886/5886/stack
[<ffffffff81063c72>] do_wait+0x1e2/0x240
[<ffffffff81064cb4>] SyS_wait4+0x64/0xe0
[<ffffffff816f521d>] system_call_fastpath+0x1a/0x1f
[<ffffffffffffffff>] 0xffffffffffffffff
$ cat term/5886/status
Name: test
State: S (sleeping)
Tgid: 5886
Pid: 5886
PPid: 2205
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 24 27 30 46 112 118 124 1000
VmPeak: 12652 kB
VmSize: 12652 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 1072 kB
VmRSS: 1072 kB
VmData: 272 kB
VmStk: 136 kB
VmExe: 4 kB
VmLib: 3960 kB
VmPTE: 44 kB
VmSwap: 0 kB
Threads: 1
SigQ: 0/30685
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000000006
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000001fffffffff
Seccomp: 0
Cpus_allowed: ff
Cpus_allowed_list: 0-7
Mems_allowed: 00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches: 3
nonvoluntary_ctxt_switches: 1
用autotools来替换手动编写Makefile
Makefile_Template.am
bin_PROGRAMS = XXX
XXX_SOURCES = YYY
autogen.sh
sed -e "{
s/XXX/$1/
s/YYY/$2/
}" Makefile_Template.am > Makefile.am
autoscan
sed -e "/AC_INIT/a\
AM_INIT_AUTOMAKE" configure.scan > configure.ac
autoheader
aclocal
touch README AUTHORS NEWS ChangeLog
automake --add-missing --copy
autoconf
./configure
make
调用autogen.sh
bash autogen.sh test test.cpp
./test
结果与上面是一样的
src_dir=/proc/$1
timestamp=$(date +%F | tr "-" "_")
timestamp="$timestamp"_"$(date +%T | tr ":" "_")"
term_dir=$PWD/$timestamp
dst_dir=$term_dir/$1
if ! [ -d $term_dir ]; then
mkdir $term_dir
else
rm -rf $term_dir/*
fi
if ! [ -d $dst_dir ]; then
mkdir $dst_dir
fi
if [ -f $src_dir/status ]; then
cat $src_dir/status > $dst_dir/status
chmod a+w $dst_dir/status
fi
if [ -f $src_dir/maps ]; then
cat $src_dir/maps > $dst_dir/maps
chmod a+w $dst_dir/maps
fi
if [ -d $src_dir/task ]; then
for file in $(ls $src_dir/task)
do
thread_dir=$src_dir/task/$file
if [ -d $thread_dir ]; then
if [ -O $thread_dir/stack ]; then
mkdir $dst_dir/$file
sudo cat $thread_dir/stack > $dst_dir/$file/stack
chmod a+w $dst_dir/$file/stack
else
echo "Invalid permissions"
sudo cat $file/stack > $dst_dir/$file/stack
fi
fi
done
fi
max_threads=$(cat /proc/sys/kernel/threads-max)
cur_threads=$(grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print sum; }')
max_mem=$(cat /proc/meminfo | grep MemTotal | awk '{print $2,$3}')
free_mem=$(cat /proc/meminfo | grep MemFree | awk '{print $2,$3}')
vm_peak=$(cat $src_dir/status | grep VmPeak | awk '{print $2,$3}')
vm_size=$(cat $src_dir/status | grep VmPeak | awk '{print $2,$3}')
echo "System Summary:" > $term_dir/summary
echo "Threads:"
echo -e "Maximum \e[1;31m" $max_threads "\e[0mthreads allowed!" >> $term_dir/summary
echo -e "Currently \e[1;31m" $cur_threads "\e[0mthreads consumed!" >> $term_dir/summary
echo "Memory:"
echo -e "Total \e[1;31m" $max_mem "\e[0mmemory in system!" >> $term_dir/summary
echo -e "Free \e[1;31m" $free_mem "\e[0mmemory in system!" >> $term_dir/summary
echo "Process " $1 "Memory:"
echo -e "Peak use \e[1;31m" $vm_peak "\e[0mmemory!" >> $term_dir/summary
echo -e "Current use \e[1;31m" $vm_size "\e[0mmemory!" >> $term_dir/summary
cat $term_dir/summary
Linux崩溃时启动脚本获取进程相关信息的更多相关文章
- 014-交互式Shell和shell脚本获取进程 pid
Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- Python基础:获取平台相关信息
Windows 10家庭中文版,Python 3.6.4, 本文介绍了使用os.platform.sys三个模块获取Python程序的运行平台相关的信息. os模块:提供 各种各样的操作系统接口 os ...
- Linux下使用fstatfs/statfs查询系统相关信息
Linux下使用fstatfs/statfs查询系统相关信息 1. 功能 #include < sys/statfs.h > int statfs(const char *path, ...
- 获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)
引言 在软件开个过程中,对于软件的稳定性和使用率也是我们需要关注的 . 使用sigar来监控,简单方便! 使用说明:下载sigar jar及配合sigar的dll文件来用,需要将dll文件放到JD ...
- 把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方。Duplicate entry
把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方.Duplicate entry ’3′ for key ‘PRIMARY’ 你的主键是不 ...
- PHP获取手机相关信息
该PHP操作类实现获取手机号手机头信息,取UA,取得手机类型,判断是否是opera,判断是否是m3gate,取得HA,取得手机IP 代码如下: <?php /** * @desc 手机操作类 获 ...
- 通过runtime获取对象相关信息
通过runtime获取对象相关信息 在这里,本人给大家提供一个runtime关于NSObject的扩展,用来显示各种NSObject中的信息,这有助于你来分析类的组成:) 先准备以下类供测试: Mod ...
- 获取IP相关信息和文件上传
获取IP相关信息 要获取用户访问者的IP地址相关信息,可以利用依赖注入,获取IHttpConnectionFeature的实例,从该实例上可以获取IP地址的相关信息,实例如下: var connect ...
随机推荐
- 利用Lua实现二叉查找树并进行各种遍历
-- author : coder_zhang-- date : 2014-6-25 root = nil function insert_node(number) if root == nil th ...
- [CF580C]Shortest Cycle(图论,最小环)
Description: 给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环. Solut ...
- Docker配置远程访问
近来学习Docker部署微服务,需要配置Docker的远程访问,由于实际环境和学习资料有出入,尝试着根据网上搜索的一些相关资料进行配置,未能成功.最终通过自己摸索,成功配置Docker远程访问.现和大 ...
- sqlserver创建索引语句
CREATE INDEX PersonIndex ON 表名 (字段名) DROP INDEX PersonIndex ON 表名
- this的指向问题 call apply bind 中的this
在函数中this指向谁: 函数中的this指向谁,是由函数被调用的那一刻就确定下来的 平时确定一个函数中的this是谁,我们需要通过调用模式来确定 1. 函数调用模式 this ---> ...
- 【彩彩只能变身队(第七组)】Beta版本
本篇博客包括前期博文汇总.任务墙.团队管理细节与交流细节.代码管理.Beta阶段冲刺.团队总结.用户使用报告.Postmortem报告. 服务器网址:http://47.106.227.154/ 彩彩 ...
- kafka2.3集群搭建
环境: 3台centos7.4 3台zookeeper3.4.14 1. wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.11-2.3 ...
- bzoj4145 [AMPPZ2014]The Prices 状压 DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4145 题解 好像这道题有不少方法呢. ...谁叫这道题有点简单,所以方法多呗. 我的方法: 求 ...
- 如何将 不确定的有穷自动机(NFA) 转化为 确定的有穷自动机(DFA) 并将DFA最简化
一.从NFA到DFA的转换 例如下图: DFA的每个状态都是一个由NFA中的状态构成的集合,即NFA状态集合的一个子集 r =aa*bb*cc* 二.从带有ε-边的NFA到DFA的转换 r=0*1*2 ...
- C++ 概率算法 利用蒙特卡罗算法计算圆周率
概率算法大致可分为4种形式: 数值概率算法: 蒙特卡罗算法: 拉斯维加斯算法: 舍伍德算法: 计算蒙特卡罗概率的算法实现: #include "stdio.h" #include ...