2

I want to write a script to display a message and possibly cancel a logout from an interactive shell. This is because I often run background jobs in some of my shells and in the spur of the moment I logout from all, closing the running jobs. What I'm after is a script like:

# If there are running jobs
if [ -z "$(jobs)" ]; then
    # Display messages, give choices, etc.
    ...code...
    # And finally, given some conditions (e.g. answers to questions, etc.)
    cancel_logout
else
    logout

Presumably, the above would go into ~/.bash_logout.

1 Answer 1

6

The proper way to do this is to have bash perform the check itself

shopt -s checkjobs

This will make bash display a notice if any jobs are left running. If there are jobs running you have to try and exit twice to actually exit.

2
  • This does almost exactly what I need. One point is that it only seems to remind me once, e.g. remind, process some commands, don't remind next time. But it might be a timeout thing. Also, is there something that would work with OSX as well? Commented Mar 19, 2012 at 21:31
  • @WojtekRzepala I would guess its a bug in bash. The documentation very clearly states multiple times that you must try to exit twice without any commands issued between the exit attempts (I get the same - apparently buggy - behavior). Commented Mar 19, 2012 at 23:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.