AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1491672
Accepted
fixit7
fixit7
Asked: 2023-11-06 10:51:05 +0800 CST2023-11-06 10:51:05 +0800 CST 2023-11-06 10:51:05 +0800 CST

防止打开的终端被关闭

  • 772

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

我使用脚本来进行提醒。我运行的一个会显示“流星雨发生在午夜之后”。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. !!'






 
command-line
  • 2 2 个回答
  • 804 Views

2 个回答

  • Voted
  1. stumblebee
    2023-11-06T12:22:20+08:002023-11-06T12:22:20+08:00

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

    daemon --name="yourservicename" --output=./yourlog.txt ./yourscript
    

    要停止守护进程:

    daemon --name="yourservicename" --stop
    

    要安装守护进程:

    sudo apt update
    sudo apt install daemon
    

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

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

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

    或者,您可以使用 nohup:

    nohup ./yourscript &

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

    • 6
  2. Best Answer
    cocomac
    2023-11-07T04:31:22+08:002023-11-07T04:31:22+08:00

    根据您显示文本的方式,这可能适合您,也可能不适合您。但更一般地说,这是保持某些东西运行的有效方法。

    您可以使用tmux。您需要使用 来安装它sudo apt update && sudo apt install tmux。

    tmux安装后,您可以通过在 shell 中运行来启动 tmux 会话。进入会话后,您可以运行命令,然后执行Ctrl+B操作D以隐藏会话。但是,一旦隐藏,即使关闭终端,您也可以通过tmux a从 shell 运行来返回到会话。您可以从 tmux 会话中运行exit,然后退出它。

    这实际上并不能阻止它被关闭。但是,即使您关闭终端窗口,它也会保持运行,因此这可能足以满足您的用例。

    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve