Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.
    # And finally, given some conditions (e.g. answers to questions, etc.)

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

share|improve this question

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.

share|improve this answer
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? – Wojtek Rzepala Mar 19 '12 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). – Patrick Mar 19 '12 at 23:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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