First, the question: How can I warn the user that a script is running when the user clicks the close button and the terminal itself (and the process running in it) was started from the command line.

I have a script that performs actions that the user will want to reverse before the script terminates. There is a read statement that pauses the process until the user hits [enter]. But, a hapless user will occasionally click the close button [x] in the top right-hand corner. I have not found a trap that catches the close button, but a warning will do. The gnome-terminal provides the needed warning.

But, when the gnome-terminal is launched from a script (say a Thunar custom action) the feature does not work. To clarify, consider the script:

gnome-terminal --command="sh readx.sh"

Where readx.sh contains a single line: read x

On clicking the top right [x] button, a terminal launched this way closes without warning while the script is still active.

Any method for trapping or warning will work for me.

  • Try running gnome-terminal -e "bash -c readx.sh;bash"
    – kirill-a
    Jan 6, 2015 at 10:09
  • That's close. The command will open the gnome terminal in warning mode. As written, the gnome terminal opens and then stumbles on the command readx.sh: bash: readx.sh: command not found.
    – markb
    Jan 6, 2015 at 20:43
  • May be add ./ before filename: gnome-terminal -e "bash -c ./readx.sh;bash" (if you're running it from current directory)
    – kirill-a
    Jan 6, 2015 at 21:30
  • Yup. Been there, done that, even dropped in the entire filespec. Also tried as both an sh script and a bash script.
    – markb
    Jan 7, 2015 at 0:56
  • How about `gnome-terminal -x bash -c "./readx.sh;bash"
    – kirill-a
    Jan 7, 2015 at 8:05

2 Answers 2


Yes! Including a zenity prompt works well for me. When the script is executed with nohup and a zenity prompt is included, the thread will block as desired.


You can actually detach you process of running readx.sh from terminal window, so even if you close the gnome-terminal window, it will continue to run in background and complete what it should do. In order to do this, you should use nohup command - it disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.

I tested it with the following command: gnome-terminal -e "nohup bash ./123.sh" Script 123.sh contains only sleep 7 && ls -a > 1.txt. So, when I run the previous command and close the window of gnome-terminal, file 1.txt is created nevertheless.

  • This answer applies to a script that can complete its task autonomously. The readx script requires user input from an interactive terminal. Like the readx script, the actual thunar custom action requires the following interaction:"please press enter to gracefully close your encrypted volume when you are through working with it." But your answer does provide an idea that may work: detach a script that blocks at a zenity dialogue. I'll give this a try... a zenity dialogue is probably less likely to be occidentally closed in an un-trappable way.
    – markb
    Jan 8, 2015 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.