Linux 如何在bash中使用kill SIGUSR2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17588767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:13:45  来源:igfitidea点击:

how to use kill SIGUSR2 in bash?

linuxshellkill

提问by Mark

I use iptraf to monitor the network traffic in linux, and the shell command is(make iptraf running in background):

我在linux中使用iptraf来监控网络流量,shell命令是(使iptraf在后台运行):

iptraf -s eth0 -f -B -L ./traffic.dat

if I want to get the result, I have to stop iptraf first, so I use the shell command:

如果我想得到结果,我必须先停止iptraf,所以我使用shell命令:

kill -SIGUSR2 $pid

however, I could not stop iptraf if I move these shell commands into a bash script file(net.sh), and I get an error:

但是,如果将这些 shell 命令移动到 bash 脚本文件 (net.sh) 中,则无法停止 iptraf,并且出现错误:

kill: SIGUSR2: invalid signal specification

I use 'kill -l' in the script file(net.sh), and I find there is no parameter which name is SIGUSR2. and I would get nothing if I use USR2 or -9.

我在脚本文件(net.sh)中使用了'kill -l',我发现没有名称是SIGUSR2的参数。如果我使用 USR2 或 -9,我将一无所获。

the complete script file is:

完整的脚本文件是:

    iptraf -s eth0 -f -B -L ./temp.txt
    pid=`ps -ef | grep iptraf | grep -v grep | awk '{print }'`
    kill -USR2 $pid
    cat temp.txt

I get nothing after these commands.

这些命令后我什么也没得到。

what shoud I do if I want to get the result?

如果我想得到结果应该怎么做?

采纳答案by hek2mgl

SIGUSR2is architecture depended and can have a value out of 31, 12or 17. This is described in man 7 signal. You'll have to find out which value is appropriate for your system. Usually this is done by having a look into:

SIGUSR2依赖于架构,可以有一个值 out 31, 12or 17。这在 中描述man 7 signal。您必须找出适合您系统的值。通常这是通过查看以下内容来完成的:

/usr/include/asm/signal.h 

On my system - Ubuntu 12.04 AMD 64 - it has a value of 12:

在我的系统上 - Ubuntu 12.04 AMD 64 - 它的值为12

#define SIGUSR2     12

Once you know the proper numeric value for SIGUSR2on your system, you can send this signal using:

一旦您知道系统上的正确数值SIGUSR2,您就可以使用以下命令发送此信号:

kill -SIGNO PID
# In this case
kill -12 PID

回答by Claudio

On my Linux box it works.

在我的 Linux 机器上它可以工作。

I ran an infinite loop (pid = 4574), then I ran

我跑了一个无限循环(pid = 4574),然后我跑了

#!/bin/bash
kill -l | grep USR2
kill -SIGUSR2 4574

kill -lhas showed the signal and kill -SIGUSR2has sent the signal (killing the process).

kill -l已显示信号,而kill -SIGUSR2已发送信号(终止进程)。

Check if you are running Bash or some other shell (e.g., dash, busybox, etc.)

检查您是否正在运行 Bash 或其他一些 shell(例如,dash、busybox 等)

回答by Avindra Goolcharan

Cross-platform way to do this: use -swithout the SIGprefix. E.g.,:

跨平台的方式来做到这一点:使用-s不带SIG前缀。例如,:

kill -s USR2 $pid

kill -s USR2 $pid

This seems to work on both MacOS and linux.

这似乎适用于 MacOS 和 Linux。