如果其他人已在 bash 内完成,则终止脚本
Kill a script if others have finished in bash
我正在尝试默默 运行 脚本和加载动画。我希望在执行初始脚本后停止加载动画,以便执行最终脚本。
run A.py |
| --> message + ∞ load animation | --> run C.py (uses A & B output)
run B.py |
我已经完成了 80%,但我使用的方法是等待无限加载动画完成后再继续。显然,它永远不会结束,也永远不会继续(我有一个解决方法,但我不太喜欢它,我已经投入时间来尝试让当前的方法起作用)。
# Run these silently
python "/home/solebay/Project/script_a.py" &
python "/home/solebay/Project/script_b.py" &
pid=$!
# If this script is killed, stop the loading animation.
trap "kill $pid 2> /dev/null" EXIT
# While is running...
message=false
while kill -0 $pid 2> /dev/null; do
if [ "$message" = false ]; then
python "/home/solebay/Projects/loading_message.py" # print loading message
message=true
fi
python "/home/solebay/Project/loading_animation.py" # Print looping animation
done
trap - EXIT
# Run the main script (dependent on script_a.py and script_b.py)
python "/home/solebay/Project/script_c.py"
我能否修改此方法以使其有效或是否不合适?我尝试过的一切最终只是将其恢复到 none 有效的状态。
实施 Léa Gris 的解决方案:
unset anim_pid
trap 'kill $anim_pid' EXT INT
python "/home/solebay/Project/loading_animation.py" & anim_pid=$!
python /home/solebay/Project/script_a.py & pid1=$!
python /home/solebay/Project/script_b.py & pid2=$!
# wait -n either task to finish
wait $pid1 $pid2
kill $anim_pid 2>/dev/null
printf '\n *** End of script ***\n'
wait -n pid pid
将等待任一 pid 完成
wait pid pid
将等待两个 pid 完成
#!/usr/bin/env bash
# Do not leave animation running in case script ends
unset anim_pid
trap 'kill $anim_pid 2>/dev/null' EXT INT
animation() {
# loop until killed
while :; do
for frame in '|' '/' '-' \; do
printf '\r%s' "$frame"
sleep .25
done
done
}
task() {
printf 'Hello I am task %d\n' ""
LC_NUMERIC=C printf 'Task %d sleeping for %f seconds\n' "" ""
sleep ""
printf 'Task %d finished\n' ""
}
animation & anim_pid=$!
task 1 2.2 & pid1=$!
task 2 1.5 & pid2=$!
# wait both tasks to finish
wait $pid1 $pid2
kill $anim_pid 2>/dev/null
printf '\n *** End of script ***\n'
我正在尝试默默 运行 脚本和加载动画。我希望在执行初始脚本后停止加载动画,以便执行最终脚本。
run A.py |
| --> message + ∞ load animation | --> run C.py (uses A & B output)
run B.py |
我已经完成了 80%,但我使用的方法是等待无限加载动画完成后再继续。显然,它永远不会结束,也永远不会继续(我有一个解决方法,但我不太喜欢它,我已经投入时间来尝试让当前的方法起作用)。
# Run these silently
python "/home/solebay/Project/script_a.py" &
python "/home/solebay/Project/script_b.py" &
pid=$!
# If this script is killed, stop the loading animation.
trap "kill $pid 2> /dev/null" EXIT
# While is running...
message=false
while kill -0 $pid 2> /dev/null; do
if [ "$message" = false ]; then
python "/home/solebay/Projects/loading_message.py" # print loading message
message=true
fi
python "/home/solebay/Project/loading_animation.py" # Print looping animation
done
trap - EXIT
# Run the main script (dependent on script_a.py and script_b.py)
python "/home/solebay/Project/script_c.py"
我能否修改此方法以使其有效或是否不合适?我尝试过的一切最终只是将其恢复到 none 有效的状态。
实施 Léa Gris 的解决方案:
unset anim_pid
trap 'kill $anim_pid' EXT INT
python "/home/solebay/Project/loading_animation.py" & anim_pid=$!
python /home/solebay/Project/script_a.py & pid1=$!
python /home/solebay/Project/script_b.py & pid2=$!
# wait -n either task to finish
wait $pid1 $pid2
kill $anim_pid 2>/dev/null
printf '\n *** End of script ***\n'
wait -n pid pid
将等待任一 pid 完成wait pid pid
将等待两个 pid 完成
#!/usr/bin/env bash
# Do not leave animation running in case script ends
unset anim_pid
trap 'kill $anim_pid 2>/dev/null' EXT INT
animation() {
# loop until killed
while :; do
for frame in '|' '/' '-' \; do
printf '\r%s' "$frame"
sleep .25
done
done
}
task() {
printf 'Hello I am task %d\n' ""
LC_NUMERIC=C printf 'Task %d sleeping for %f seconds\n' "" ""
sleep ""
printf 'Task %d finished\n' ""
}
animation & anim_pid=$!
task 1 2.2 & pid1=$!
task 2 1.5 & pid2=$!
# wait both tasks to finish
wait $pid1 $pid2
kill $anim_pid 2>/dev/null
printf '\n *** End of script ***\n'