我需要弄清楚如何捕获 Ctrl C,这样如果我想提前结束脚本,它可以重新启用挂起和休眠模式。
我查看了其他有关捕获 Ctrl C 的讨论,但没有找到有帮助的。
谢谢。
# TimerInTerminal.sh
# To prevent your Linux system from suspending or going into hibernation, you need to disable the following systemd targets:
# sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# To re-enable the suspend and hibernation modes, run the command:
# sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
soundfile="/usr/share/sounds/My_Sounds/Electronic_Chime.wav"
# Stop computer from sleeping while timer is running
# prevent your Linux system from suspending or going into hibernation
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# This allows supend ?
#trap "echo marlin | sudo -S systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target" INT EXIT
if [ $# -eq 1 ]
then
DURATION="$1"
else
read -r -p "Timer for how many minutes?( for fractional, use decimal notation , 0.5==30s, 1.25==75s etc) : " DURATION
read -r -p "Enter text to display at the end of the timer : " n1
fi
DURATION=$(echo "$DURATION * 60 / 1" | bc) # lets us deal with fractional inputs
START=$(date +%s) # only do this once (anchor's the time)
countdown () {
NOW=$(date +%s) # Get time now in seconds
DIF=$((NOW - START)) # Compute diff in seconds
ELAPSE=$((DURATION - DIF)) # Compute elapsed time in seconds
MINS=$((ELAPSE / 60)) # Convert to minutes... (dumps remainder from division)
SECS=$((ELAPSE - (MINS*60))) # ... and seconds
#banner "$MINS:$SECS"
echo "$MINS:$SECS"
sleep "$1"
}
while true
do
clear
countdown 0 # calc time remaining
if [ $MINS -le 0 ]
then
# Blink screen
while [ $SECS -gt 0 ]
do
clear # Flash on
#setterm -term linux -back red -fore white
countdown 0.5
clear # Flash off
#setterm -term linux -default
countdown 0.5
done # End for loop
setterm -term linux -default
clear
break # time has expired lets get out of here
else
countdown 1
fi
done
echo $n1
amixer -D pulse sset Master 30% > /dev/null 2>&1
# Play a sound
cvlc --play-and-exit "$soundfile" > /dev/null 2>&1
# To re-enable the suspend and hibernation modes, run the command:
echo marlin | sudo -S systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
答案1
将其添加到脚本顶部附近的某个位置:
trap cleanup SIGINT
cleanup () {
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
}
我不确定为什么marlin
当您重新启用时要通过管道发送到 systemctl,但是您注释掉的 trap 命令可能仍然有效,除非您使用它mask
而不是unmask
fwiw。尽管值得注意的是,通过捕获 forINT
和EXIT
当您按ctrl+时,c您的命令将被执行两次。
答案2
我已经尝试过jesse_b 的回答上面,但它对我不起作用,因为Ctrl+之前的进程C有点阻塞进程,所以我要做的就是将该函数放在陷阱调用之前。
#!/bin/bash
trp()
{
sudo airmon-ng stop wlp2s0mon
service NetworkManager start
}
trap trp SIGINT
sudo airmon-ng check kill
sudo airmon-ng start wlp2s0
sudo aireplay-ng --deauth 0 -a bb:bb:bb:bb:bb:bb -c cc:cc:cc:cc:cc:cc wlp2s0mon
所以在这里,它在陷阱之前注册了该函数,然后陷阱才能成功使用,否则当该函数位于该aireplay-ng
行之后时,我收到此错误:
Line 1: trp: command not found
对于那些想知道的人来说,这只是一个脚本,可以让我的侄子在午夜时关闭 wifi,这样他就可以离开 TikTok 去睡觉了。