9

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
    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 bug-bash@gnu.org – mklement0 Nov 26 '16 at 19:15
7

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.

  • set +m works in the terminal, but when I add it into my .bashrc, it has no effect. – Martin Vegter Jun 18 '14 at 21:18
  • Ah, you're right. That's localised to the specific script at that point. Then bash asserts its defaults in. – Falsenames Jun 19 '14 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. – Martin Vegter Jun 20 '14 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 Jun 20 '14 at 22:54
4

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.

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

  • 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? – Martin Vegter Jun 19 '14 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. – Martin Vegter Jun 19 '14 at 7:28
3

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
    ^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. – Volker Siegel Aug 21 '14 at 19:21
2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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