go语言获取发送信号的进程pid
2016-09-18 20:47
417 查看
背景
今天在发布一个程序之前,给qa提测的时候,qa反馈程序运行10几分钟之后,退出了排查过程
在程序中加日志,发现程序捕获到了一个SIGTERM信号,然后做了一些退出前的清理工作(在退出之前,该发送的数据还是需要发送的)。然后就需要知道到底是那个进程向我发送SIGTERM信号代码
查了一下,貌似go语言没有直接的发送获取向自己发送信号的进程的pid,需要嵌入一段c语言代码,获取到pid之后,为了更直观的知道是那个可执行程序,可以去读取/proc/${pid}/exe这个软链package main /* #include <stdio.h> #include <signal.h> #include <string.h> #include <unistd.h> struct sigaction old_action; void handler(int signum, siginfo_t *info, void *context) { printf("Sent by %d\n", info->si_pid); char path[1024]; char res[1024]; memset(path, '\0', sizeof(path)); memset(res, '\0', sizeof(res)); snprintf(path, sizeof(path), "/proc/%d/exe", info->si_pid); if (-1 == readlink(path, res, sizeof(res))) { printf("fail to get the symblic link of %s\n", path); } else { printf("the symblic link of %s is: %s\n", path, res); } } void test() { struct sigaction action; sigaction(SIGTERM, NULL, &action); memset(&action, 0, sizeof action); sigfillset(&action.sa_mask); action.sa_sigaction = handler; action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_ONSTACK; sigaction(SIGTERM, &action, &old_action); } */ import "C" ...... ...... func main() { ...... C.test() ...... }
相关文章推荐
- 通过PID获取进程路径
- VB6 根据进程ID(PID)获取窗体句柄
- GUI_获取窗口的进程PID
- linux 下根据进程名字获取进程的进程号PID,类似pidof,C函数
- perl多进程数量控制-信号或者判断pid是否存在
- MFC中获取进程名、PID及进程所在路径
- 获取当前运行Java的进程标识符(PID)
- 编写一个任务管理器/进程获取/PID获取
- 通过PID获取进程名
- 在内核态通过PID获取进程名!!
- 根据系统进程pid获取sql【脚本收藏】
- VB6 根据进程ID(PID)获取窗体句柄
- Linux进程通信 发送信号
- bash shell获取进程的pid
- 例程之九_GUI_获取窗口的进程PID
- 通过进程名获取进程PID
- C语言 信号 获取段错误
- 通过PID获取进程相关信息,如cmdline
- shell获取当前进程pid和上一个进程pid
- 使用kill向进程发送信号