防止打开的终端被关闭

fix*_*it7 4 command-line ubuntu-mate 20.04

已经过去很长时间了,我对自己的命令已经生疏了。

我使用脚本来进行提醒。我运行的一个会显示“流星雨发生在午夜之后”。3小时后。

有没有办法防止我意外关闭正在运行计时器脚本的终端?

更新:

这是我的脚本。

#!/bin/bash
#
#  Sound alarm after specifying time and message 
# Must input time delay AND message in double quotes !!
#
#       
# ** sleep can also accept intergers ex. sleep 7.63
# Made alias for it type al 

    # Print a trace of simple commands, for commands, case commands, select commands, 
    # and arithmetic for commands and their # arguments or associated word lists after they are expanded 
    # and before they are executed. The value of the PS4 variable is # expanded and the resultant value is printed before the # command and its expanded arguments.

Red='\e[0;31m'     
BRed='\e[1;31m' 
BIRed='\e[1;91m' # ${BIRed} this works
Gre='\e[0;32m'     
BGre='\e[1;32m'
BBlu='\e[1;34m' # ${BBlu}
BWhi='\e[1;37m'
Black='\e[0;30' 
BWhite='\e[0m 1;37'
# This defines a variable containing the ANSI escape sequence to clear
# the effect of all previous ANSI escape sequences that define formatting (colors, underlining, etc).
RCol='\e[0m'; 

            

soundfile="/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3"

clear
amixer -D pulse sset Master 40% > /dev/null 2>&1
if [ -f "$soundfile" ];
then
     echo -e "${BGre}Soundfile is present."
else
      
  echo "File $soundfile does NOT exist."
  echo
  echo "Program will now exit."
  exit
fi

[ -z "$2" ] && {
echo 
echo -e " ${BIRed}Error!! No time value given and/or message specified !!"
echo
echo -e "   ${BBlu}Alarm Program 2018 ${RCol}"
echo
echo -e "   alarm.sh [time value in seconds] Message in double Quotes"; 
echo -e "   alarm 5m   = 5 minute alarm"
echo -e "   alarm 5h   = 5 hour alarm"
echo -e "   alarm 5d   = 5 day alarm"
echo -e "   alarm 1.5m = 1 minute 30 seconds alarm"
echo 
echo -e "   alarm.sh 1m "\"Take bread out of oven."\""  

echo 
exit 1; }
echo  -e "\033[32;5mTIMER COUNTING DOWN to $1 \033[0m"
sleep $1
{
    for ((volume = 15; volume <= 35; volume += 2)); do
        # 2> /dev/null suppresses messages for amixer AND (c)vlc
        amixer -D pulse sset Master ${volume}% > /dev/null
        sleep .5
    done
} &

mpg123 $soundfile > /dev/null 2>&1
#set back to original volume
amixer -D pulse sset Master %30

gxmessage -fg blue -font  'sans 20' -timeout 2 ' Tea is ready. !!'






 
Run Code Online (Sandbox Code Playgroud)

stu*_*bee 6

您可以使用以下命令将脚本作为守护进程运行daemon,该命令在后台运行脚本:

daemon --name="yourservicename" --output=./yourlog.txt ./yourscript
Run Code Online (Sandbox Code Playgroud)

要停止守护进程:

daemon --name="yourservicename" --stop
Run Code Online (Sandbox Code Playgroud)

要安装守护进程:

sudo apt update
sudo apt install daemon
Run Code Online (Sandbox Code Playgroud)

您需要做的唯一一件事是修改脚本以通过以下方式发送消息notify-send(如您​​的问题中所述):

notify-send -u critical -t 0 "Meteor shower occurs after midnight."

当您使用-u critical -t 0选项时,通知将保留在屏幕上,直到您单击它。

或者,您可以使用 nohup

nohup ./yourscript &

您仍然需要修改脚本以通过notify-send上述方式发送消息。

  • 您是否需要使脚本可执行,并且如果它不在您的 PATH 中的目录中,则还需要使用脚本的完整路径,例如“./yourscript”,如果脚本位于当前目录中? (2认同)
  • @mchid一般来说,没有必要使脚本可执行才能运行它。您可以使用其解释器运行它,例如,如果它是 bash 脚本,您可以运行“bash yourscript”。(在这种情况下,不需要在文件名前面添加“./”,尽管您通常仍然需要提供脚本文件的有效相对或绝对路径。) (2认同)