gpt4 book ai didi

bash - 通过键盘输入暂停脚本

转载 作者:行者123 更新时间:2023-11-29 09:27:12 26 4
gpt4 key购买 nike

(抱歉我的英语不好。)我想通过按 [SPACE] 栏来暂停正在运行的脚本。脚本必须运行,直到用户不按[SPACE] 栏,然后暂停20 秒,然后继续运行。如何在脚本运行时持续观察键盘输入?

最佳答案

一种方法:

#!/bin/bash -eu
script(){ #a mock for your script
while :; do
echo working
sleep 1
done
}

set -m #use job control
script & #run it in the background in a separate process group
read -sd ' ' #silently read until a space is read
kill -STOP -$! #stop the background process group
sleep 2 #wait 2 seconds (change it to 20 for your case)
kill -CONT -$! #resume the background process group
fg #put it in the forground so it's killable with Ctrl+C

关于bash - 通过键盘输入暂停脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49647715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com