19

It happens to me sometimes, that I press CTRL+Z by accident and my application disappears into background. I know, I can bring it back with fg, so it's not such a big deal. But I am wondering about turning this job control off anyway. In my whole life, I cannot remember one instance when I needed it, it just looks to me as a relic form the past.

Is this OK to disable job control entirely? Or am I missing something, and this feature can be useful? How would I disable it in my .bashrc

UPDATE:

I have tried set +m as suggested by @Falsenames. However, this only works when I type it in the terminal. Adding set +m into my .bashrc has no effect.

1
  • 3
    Re update: The fact that $- (list of active options) no longer contains m when you place set +m in your ~/.bashrc suggests that Bash is trying to turn job control off, but in the end doesn't do so because of a bug - may be worth reporting via [email protected]
    – mklement0
    Commented Nov 26, 2016 at 19:15

4 Answers 4

12

You can add the following into your command line to stop using monitoring mode.

set +m

If you really need the ctrl-z functionality later, you can just type 'set -m' to enable monitoring for that session.

From man bash. Note that this is for '-m', with "+m" toggling that setting to disable.

set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...] 
....
-m
    Monitor mode. Job control is enabled. This option is on by default for interactive
    shells on systems that support it (see JOB CONTROL above). Background processes 
    run in a separate process group and a line containing their exit status is printed
    upon their completion. 

As a last ditch effort, you may want to manually compile a version of bash without the "--enable-job-control" flag. Here is a quick install guide from GNU. If you do choose to go this route, DO NOT replace /bin/bash just in case background processes run through bash expect job control. Instead, make a /bin/bash.alt or another file. Your default shell can be changed to this alternate one by running usermod or editing /etc/passwd as root.

4
  • set +m works in the terminal, but when I add it into my .bashrc, it has no effect. Commented Jun 18, 2014 at 21:18
  • Ah, you're right. That's localised to the specific script at that point. Then bash asserts its defaults in.
    – Falsenames
    Commented Jun 19, 2014 at 18:44
  • compiling my own bash seems quite a high price to pay for disabling job control. There must be some setting that works via .bashrc. Commented Jun 20, 2014 at 14:45
  • Since this is all set at compile time, there doesn't necessary have to be a way to change it in a setup file that loads after the shell starts running. The issue here is that someone decided to compile bash WITH job control enabled as the default for the distro you are using. I cannot really personally argue against that, since I use various job controls every day at work, and I'm not sure I could easily adjust to life without it. Granted, that is my experience, not yours... this is why there are flags on compiling, to give options.
    – Falsenames
    Commented Jun 20, 2014 at 22:54
6

stty susp undef will disable the keyboard-initiated suspend signal for most programs, however commands like vim and emacs that have specific bindings for Ctrl-Z will have to be reconfigured individually. In emacs, you can run (global-unset-key "\C-z").

You can add that stty command to your ~/.bash_profile or ~/.profile, logout, login again.

4
  • this actually works for some programs. But for example for mc it does not work. Which basically confirms what you said. Is there no universal solution that works for all? Commented Jun 19, 2014 at 7:20
  • I just noticed, set +m works for mc, but only if I type it in the terminal, not in .bashrc or .profile. Commented Jun 19, 2014 at 7:28
  • I can I configure emacs for this? @MartinVegter
    – alper
    Commented Jul 16, 2020 at 12:12
  • I want to able to suspend emacs but don't want to see [2] + ... (signal).... message
    – alper
    Commented Jul 19, 2020 at 21:37
5

Or am I missing something, and this feature can be useful?

To answer this part of the question: C-z / fg combo is essential to my vim workflow. For example, I prefer C-z > git commit > fg to using git wrappers from within vim.

C-z helps with treating the shell as IDE, conforming to the "do one thing" principle. (This instead of "editor as IDE").

1
  • 1
    ^Z is absolutely still useful - but it was even more useful when a terminal was not a window, but a box in front of you. And you had only one. Commented Aug 21, 2014 at 19:21
3

Add trap "" SIGTSTP in your script. It will catch the signal and ignore it.

#!/bin/bash
trap "" SIGTSTP
watch date

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .