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?

Update:

Here is my script.

#!/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 "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
    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. !!'






 
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