Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.