发送信号到linux进程并拦截它们 [英] Sending signals to a linux process and intercepting them

查看:74
本文介绍了发送信号到linux进程并拦截它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下bash脚本:

I have a following bash script:

#!/bin/bash
for i in {0..8}
do
  trap "echo received $i" $i
done
trap "echo 'receiving the SIGINT'; kill -9  $$" INT
for i in {10..64}
do
  trap "echo receiving the $i" $i
done

sleep 1h

如果运行它,并从其他终端将SIGINT发送给它,它什么也不做.我正在使用 kill -2 pid ,其中pid是正在运行的脚本的pid.

If run it and from other terminal send the SIGINT to it, it does nothing. I am using kill -2 pid where pid is the pid of the running script.

如果我在运行脚本的终端中按下 CTRL + C (SIGINT),它将杀死自己并编写消息.怎么了,从另一个终端向它发送信号时会不一样吗?

If I hit CTRL+C (SIGINT) in terminal where the script is running it kills itself and writes the message. How come, it is not the same when sending the signal to it from a different terminal?

推荐答案

如果我将 sleep 1h 替换为

while true ; do
    sleep 1
done

正在工作:-)

这篇关于发送信号到linux进程并拦截它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆