Prevent an open terminal from being closed

It has been a long time, so I am rusty with my commands.

I use a script to have reminders. One I have running will display "Meteor shower occurs after midnight." after 3 hours.

Is there a way to prevent me from accidentally closing a terminal which has my timer script running?


Here is my script.

#  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.

BIRed='e[1;91m' # ${BIRed} this works
BBlu='e[1;34m' # ${BBlu}
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).



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

[ -z "$2" ] && {
echo -e " ${BIRed}Error!! No time value given and/or message specified !!"
echo -e "   ${BBlu}Alarm Program 2018 ${RCol}"
echo -e " [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 -e " 1m ""Take bread out of oven."""  

exit 1; }
echo  -e "33[32;5mTIMER COUNTING DOWN to $1 33[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
} &

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. !!'

Asked By: fixit7


You can run your script as a daemon using the daemon command, which runs the script in the background :

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

To stop the daemon:

daemon --name="yourservicename" --stop

To install daemon:

sudo apt update
sudo apt install daemon

The only other thing you will need to do is modify your script to send your messages via notify-send (As mentioned in your question) :

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

When you use -u critical -t 0 options, The notification will stay on the screen until you click on it.

Alternatively, you can use nohup:

nohup ./yourscript &

You will still need to modify your script to send your messages via notify-send as described above.

Answered By: stumblebee

Depending on how you would display the text, this may or may not work for you. More generally though, this is a valid way to keep something running.

You can use tmux. You’ll need to install it with sudo apt update && sudo apt install tmux.

Once installed, you can launch a tmux session by running tmux in a shell. Once in the session, you can run commands and then do Ctrl + B followed by D to hide the session. But, once hidden, even if you close the terminal, you can get back to your session by running tmux a from a shell. You can run exit from in the tmux session to, well, exit it.

This doesn’t actually prevent it from being closed. But, it keeps it running even if you close the terminal window, so that may be sufficient for your use case.

Answered By: cocomac