Bash 脚本 运行 超时不会在 SIGINT 上退出
Bash script run with timeout won't exit on SIGINT
我有一个 bash 脚本,它在 for 循环中(在超时条件下)调用另一个 bash 脚本,格式如下:
#!/bin/bash
trap 'trap - SIGTERM && kill 0' SIGINT SIGTERM EXIT
INNER_SCRIPT_PATH="./inner_script.sh"
for file in "$SAMPLEDIR"/*
do
if [[ "${file: -4}" == ".csv" ]]; then
CSVPATH="$file"
CSVNAME=${CSVPATH##*/} # extract file name
CSVNAME=${CSVNAME%.*} # remove extension
timeout -k 10s 30m bash "$INNER_SCRIPT_PATH"
fi
done
wait
按 Ctrl-C 不会退出所有进程,我感觉我在这里调用内部 bash 脚本的方式可能有问题(尤其是超时)。非常感谢有关如何改进的反馈!
问题出在 timeout
命令上,它使您的脚本不受 Ctrl+C 调用的影响。由于默认情况下 timeout
运行s 在其自己的进程组中,而 not 在前台进程组中,因此它不受交互式终端调用的信号的影响。
您可以 运行 它与 --foreground
一起接受来自交互式 shell 的信号。参见 timeout Man page
我有一个 bash 脚本,它在 for 循环中(在超时条件下)调用另一个 bash 脚本,格式如下:
#!/bin/bash
trap 'trap - SIGTERM && kill 0' SIGINT SIGTERM EXIT
INNER_SCRIPT_PATH="./inner_script.sh"
for file in "$SAMPLEDIR"/*
do
if [[ "${file: -4}" == ".csv" ]]; then
CSVPATH="$file"
CSVNAME=${CSVPATH##*/} # extract file name
CSVNAME=${CSVNAME%.*} # remove extension
timeout -k 10s 30m bash "$INNER_SCRIPT_PATH"
fi
done
wait
按 Ctrl-C 不会退出所有进程,我感觉我在这里调用内部 bash 脚本的方式可能有问题(尤其是超时)。非常感谢有关如何改进的反馈!
问题出在 timeout
命令上,它使您的脚本不受 Ctrl+C 调用的影响。由于默认情况下 timeout
运行s 在其自己的进程组中,而 not 在前台进程组中,因此它不受交互式终端调用的信号的影响。
您可以 运行 它与 --foreground
一起接受来自交互式 shell 的信号。参见 timeout Man page