Bash – make “There are stopped jobs” harder to kill

background-processbashshell

Typical scenario:

I temporarily need to access a file that my vhost prevents me from accessing. I fire up vim, edit the settings and background vim to tell apache to reload its configuration.

Problem:

If I now forget about this, my shell will tell me "There are stopped jobs", when I CTRL+D the session. However, since I have a habit of having shells in shells in shells, due to ssh, tmux and similar, I also frequently repeatedly send EOF when I wrap my work up and close my windows. This in turn causes me to accidentally kill the shell, despite the warning.

Can I make it harder to kill such shells?

Best Answer

If you are using bash, you can set the IGNOREEOF shell variable to a number that specifies how many consecutive EOF chars the shell should ignore before treating the EOF as an exit signal. Check the man page for specifics.

However, that triggers before the "there are stopped jobs" message triggers, so you still have the same problem - you get that message, and one more ^D exits the shell.

An alternative is to put the number of shell jobs into your prompt if that number is greater than zero.

For example, an excerpt from my .bashrc:

PROMPT_COMMAND=prompt_command
prompt_command() {
    job_count=$(jobs | wc -l)
    if [ $job_count -gt 0 ] ; then
        prompt_job="[$job_count] "
    else
        prompt_job=""
    fi
}
PS1="...\${prompt_job}..."

After this the shell may look like ...[1] ...

That puts a job count in your prompt if it is greater than zero. This makes it easy to see when you have incomplete jobs and works well (for me) as a visual reminder that jobs are still running.

Related Question