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
【s/YYY/$2是无法支持多个.cpp文件的,需要进一步优化】
 

调用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

转载于:https://www.cnblogs.com/long123king/p/3747353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以使用脚本启动进程。以下是一般的步骤: 1. 创建启动脚本:创建一个包含启动进程所需命令的脚本文件。可以使用任何文本编辑器创建一个新文件,例如 `my_script.sh`。 2. 添加执行权限:使用 `chmod` 命令为该脚本文件添加执行权限,使其可执行。可以运行以下命令:`chmod +x my_script.sh`。 3. 编写脚本内容:在脚本文件中编写启动进程的命令。例如,如果要启动一个名为 `my_process` 的进程,可以在脚本中添加如下内容: ``` #!/bin/bash /path/to/my_process ``` 其中 `/path/to/my_process` 是要启动进程的可执行文件路径。 4. 将脚本添加到系统启动项:将脚本文件复制到适当的启动目录中,以便在系统启动自动运行。具体的目录位置可能会因Linux发行版而异,但是常见的位置包括 `/etc/init.d/` 或 `/etc/rc.d/` 目录。 5. 设置启动脚本运行级别:使用 `chkconfig` 或类似的工具(如 `update-rc.d`)将启动脚本与特定的运行级别关联起来。运行级别定义了系统启动过程中执行不同任务的顺序。可以运行类似以下的命令来设置启动脚本运行级别: ``` sudo chkconfig --add my_script ``` 其中 `my_script` 是你的脚本文件名。 6. 启动进程:现在,当系统启动启动脚本将自动运行,并启动指定的进程。 请注意,以上步骤可能因Linux发行版而有所差异。确保根据你所使用的具体发行版的文档和指南进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值