【发布时间】:2020-06-01 11:47:45
【问题描述】:
当我尝试从 bash 脚本发送 SIGINT 时,我遇到了非常奇怪的行为:
#!/bin/bash
./executable 6 &
PID=$!
sleep 2
kill -s SIGINT $PID
sleep 2
if wait $PID; then
echo "FAILURE"
else
echo "SUCCESS"
fi
从没有设置信号处理程序的 C++ 代码 (g++ source.spp --std=c++11 -o executable) 编译的可执行文件:
#include <iostream>
#include <chrono>
#include <thread>
int main(int argc, char* argv[])
{
std::this_thread::sleep_for(std::chrono::seconds(std::atoi(argv[1])));
std::cout << std::endl << "FINISHED" << std::endl;
}
进程没有终止(我得到“FAILURE”,然后是“FINISHED”)。
这完全符合预期,当我:
- 从命令行向后台进程发送 SIGINT
- 当进程在前台时按 CTRL-C
- 明确设置默认信号处理程序:
signal(SIGINT, SIG_DFL);
我可以使用不同的编译器在不同的发行版上重现它(例如,带有 Bash 5.0.16 的 Ubuntu 20.04、GCC 9.3.0)。理想情况下,我想了解发生了什么,但暂时我会对不需要更改可执行文件的解决方法感到满意。
【问题讨论】:
-
可能来自不同的站点,但请看一下unix.stackexchange.com/questions/485630/…