views:

2001

answers:

7

The question is special because some keys, such as CTRL+Z, stopped working.

I tried to put the process to background by typing in the order:

  1. find /
  2. CTRL+Z
  3. bg

However, I can still see the stdout. The only difference to only doing the first step is that the command CTRL+Z does not work anymore. It is rather nasty when I have unsaved jobs and my harddrive is over 100GB. So

how can I put the process to background?

[Details]

I am using the fourth version of Bash on Mac.

[Crux Reply by Nicholas Riley]

The problem is really that I do not understand the "ramifications" of running process background. I cannot understand why the commnands, such as CTRL+Z, do not work to background processes. I was still able to kill the process in another shell with the command:

ps -ej | awk '! /grep/ && /find/ {print $2}' | xargs kill -9
+4  A: 

You can set the operation to a different key with stty on any UNIX-like system.

$ stty susp Q

will make Q your suspend key in place of CTRL-Z.

Charlie Martin
That's brillant! Now you cannot use the Q key for what $DEITY intended ("Q").
paxdiablo
+1 for the cool command.
Masi
You don't *have* to use Q, Pax.
Charlie Martin
A: 

I'll assume you're using a common variant of Linux or UNIX and possibly the bash shell. In which case:

CTRL-Z sends the SIGTSTP signal to the foreground process. You should be able to do the same thing with the kill command by specifying kill -s SIGTSTP [pid].

zombat
+2  A: 

^Z isn't working because the frontmost job is now the shell, and shells don't usually respond to SIGTSTP. (If you do really want to suspend a non-login shell, suspend usually works.)

The problem seems to be you misunderstand the ramifications of a job being in the background. Redirecting the job's standard output is unrelated.

In fact, it's unclear what you want to do. If you just want to stop find from running, then fg it and use ^C or ^\ (which by default send SIGINT and SIGQUIT respectively).

If you want to keep find running but suppress its further output, then the easiest solution I can think of is to use bg ; disown ; exit. That will stop the shell from killing its child process (find) and exit the shell; assuming it's at the top level of the Terminal window, you'll see a bit more output and find will keep running (but you'll have no way to interact with it).

Nicholas Riley
Can you clarify the sentence "Redirecting the job's standard output is unrelated."?
Masi
Sure. The main effect of backgrounding a process is that your *input* (including signals, like ^Z) no longer interact with that process; they interact with the parent shell instead. Unless the process is written to check if it's in the background and do something different, output will still emanate from that process.
Nicholas Riley
Riley: Thank you!
Masi
You could even skip the `fg` step and directly use `kill %` (maybe giving it `-INT` or `-QUIT` if desired, and changing to `%1` or other depending on what other jobs are running, see `jobs` output for details).
ephemient
+1  A: 

I usually do this kind of thing with nohup. Like this:

nohup find / > /tmp/myresults.txt &

nohup makes sure that the process doesn't stop, even if the console goes away (like, you close the window or lose your SSH or whatever). The ">" sends output to a file rather than to the console, and "&" puts the job in the background.

Evan P.
An idea just occcured to me from the nohup: "to use it with screen command" like "nohup screen". It may be helpful. +1 for sharing your experience
Masi
Why would you use screen with nohup? screen has a detach function built-in. (C-a, C-d)
Jeremy Visser
+2  A: 

Backgrounding a process will not disconnect the output from the terminal device, that's why you're still seeing output and the output may well contain control characters which can stuff up your TTY settings (cating binary files is a good way to do that).

If you want the job to run in the background, do it right:

find / >/tmp/out 2>&1 &

then examine the /tmp/out file when it's finished.

paxdiablo
Jeremy Visser
No, @Jeremy, because it's positional. My version directs stdout to the file then stderr to the same place as stdout (the file). Your suggestion would direct stderr to the same place as stdout (the terminal), then stdout to the file. And I rarely ignore questions, I'd rather educate.
paxdiablo
+1 for your comment :)
Masi
A: 

I know that in Ubuntu 9.04, you can start the process with a & after it to run it in the background. For example, "sudo gedit /boot/grub/menu.lst &" will start a text editor editing the GRUB config file in the background.

+1  A: 

I use disown.

find / & disown
exit # close the terminal and the command still runs

You can use disown after you ^Z as well:

find /
^Z
bg
disown
exit

disown is a bash command, I believe. Not sure about alternatives for other shells.

Jeremy Visser
+1 for your systematic explanation
Masi