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 ...
随机推荐
- Ubuntu 18.04安装docker 以及Nginx服务设置
1.安装需要的包sudo apt install apt-transport-https ca-certificates software-properties-common curl 2.添加 GP ...
- python学习三十三天函数匿名函数lambda用法
python函数匿名函数lambda用法,是在多行语句转换一行语句,有点像三元运算符,只可以表示一些简单运算的,lambda做一些复杂的运算不太可能.分别对比普通函数和匿名函数的区别 1,普通的函数用 ...
- project euler-34
145是个奇怪的数字.由于1!+ 4! + 5! = 1 + 24 + 120 = 145. 请求出能表示成其每位数的阶乘的和的全部数的和. 请注意:由于1! = 1和2! = 2不是和,故它们并不包 ...
- Windows系统时间会偶尔自动回拨吗?
为什么80%的码农都做不了架构师?->>> Spring boot 项目 通过日志记录插入sql操作用时 long start2 = System.currentTimeMi ...
- 【问题解决方案】关于Python中的语句 ' %matplotlib inline '
跟进小项目#GirlsInAI#-可视化时遇到的语句,之前没有遇到过 在Stack Overflow上看到了一个解释: IPython有一组预定义的"魔术函数",您可以使用命令行样 ...
- 剑指offer学习--初级c++面试题
定义一个空的类型,里面没有任何成员函数和成员变量,对该类型求sizeof,得到的结果是多少? 答案是1.空类型中的实例中不包含任何信息,本来求sizeof应该是0,但是当我们声明该类型的实例的时候,他 ...
- 工作中常用的linux命令大全
文章内容参考:https://www.cnblogs.com/yjd_hycf_space/p/7730690.html 谢谢大佬的分享 系统信息 date 显示系统日期 cal + 年份 显示 ...
- 变量管理 dotenv 的 使用
python-dotenv 安装 pip install python-dotenv 或 pipenv install python-dotenv --skip-lock 创建目标文件 在项目根目录下 ...
- poj2689 Prime Distance(素数区间筛法)
题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...
- setenv和dos2unix碰到的问题
两个比较傻的小问题 setenv ethaddr 00:0a:35:00:01:26 提示只能修改一次,束手无策,难道要改uboot吗 同事提示加上-f setenv -f ethaddr 00:0 ...