how to use kill SIGUSR2 in bash?

佐手、 提交于 2019-12-12 10:32:16

问题


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

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:

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:

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.

the complete script file is:

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

I get nothing after these commands.

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


回答1:


SIGUSR2 is architecture depended and can have a value out of 31, 12 or 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:

/usr/include/asm/signal.h 

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

#define SIGUSR2     12

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

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



回答2:


On my Linux box it works.

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

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

kill -l has showed the signal and kill -SIGUSR2 has sent the signal (killing the process).

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




回答3:


The Posix'y way to do this is by using -s without the SIG prefix. E.g.,:

kill -s USR2 $pid

This seems to work on both MacOS and linux.



来源:https://stackoverflow.com/questions/17588767/how-to-use-kill-sigusr2-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!