Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

(1). Since background job can still run even if the user logs out, what's the point of using nohup and screen?

(2). What is the difference between CTRL+Z and stop to suspend a background job? There is another command "stop" which stops a background job. This doesn't kill the job, but it will not continue executing until it is restarted. I wonder what is the difference between "stop" and CTRL+Z? for "stop", see http://www.ece.osu.edu/computing/background_jobs.html#commands

(3). to make a background job noninteractive, is this correct?

command &lt<inputfile> &gt<outputfile>

What if I want to append the output to an existing file while keep the job background?

Thanks and regards!

share|improve this question

migrated from stackoverflow.com Feb 5 '10 at 1:02

This question came from our site for professional and enthusiast programmers.

1  
This should be on superuser.com –  danben Feb 4 '10 at 0:54
    
That's part of Bash, which is why I ask here. –  Tim Feb 4 '10 at 1:05
2  
@Tim: stop is not bash, it's irrelevant. This is why noone knows what you're talking about. –  hobodave Feb 4 '10 at 1:17
1  
@everyone: the link the OP provides is about CSH, not Bash, and in particular CSH on HPUX. @Tim -- did you mean to ask this question about C-shell instead of Bash? –  quack quixote Feb 5 '10 at 1:17
    
Please re-title and re-tag this question so that it doesn't show up in searches for "bash" since it is a bash-specific question, and the references are for csh, not bash. –  echo on Mar 30 '10 at 16:32

3 Answers 3

up vote 4 down vote accepted

Background jobs

Normal background jobs do not survive a termination of the parent shell:

pigpen ~: yes > /dev/null &

This will not survive a logout.

However, this will:

pigpen ~: nohup yes &

nohup prevents the process from receiving a SIGHUP signal on logout.

Screen, on the other hand, maintains the illusion that the user is always logged in, and allows the user to reattach at any time. This has the advantage of being able to continue to interact with the program once reattached (impossible with nohup alone).

Ctrl+C

Ctrl+C sends a SIGINT to the process, which it may catch and ignore (uncommon).

I suggest reading the bash man page; specifically the sections on Signals and Job Control.

Ctrl+Z

You edited your question to use Ctrl+Z instead. Ctrl+Z sends a SIGTSTP signal to the process, suspending it. This process can be later continued by sending it a SIGCONT signal.

SIGTSTP signals can be caught by the program and ignored.

SIGSTP signal

The SIGSTP signal is different from SIGTSTP in that it cannot be caught by the running program. It's behavior is identical to the behavior of an uncaught SIGTSTP.

Resources

share|improve this answer
    
Sorry I should say CTRL+Z, not CTRL+C. What is the difference between CTRL+Z and stop? –  Tim Feb 4 '10 at 1:07
    
Tim: 'stop' isn't a bash command, so it's open to interpretation what you mean by it. –  hobodave Feb 4 '10 at 1:08
    
There is another command "stop" which stops a background job. This doesn't kill the job, but it will not continue executing until it is restarted. I wonder what is the difference between "stop" and CTRL+Z? –  Tim Feb 4 '10 at 1:09
    
@Tim: stop isn't native to bash, so I don't see how it's relevant to bash. Either way it sends some type of signal to a running process. –  hobodave Feb 4 '10 at 1:12
    
Um...I'm pretty sure that nohup prevents the utility so invoked from receiving SIGHUP when the parent shell terminates, especially seeing as SIGSTOP is not catchable. –  dmckee Feb 4 '10 at 1:56
  1. Background jobs will only continue if you detach them (I believe), and you cannot get output or send input to them later. Screen will let you connect to it later, as though you never left it.
  2. Control+c is a keyboard interrupt, all it does is send a signal to the running program, which will normally respond by exiting, but it not forced to. Stopping it will send a different signal, where the application will generally stop what it's doing until it gets a continue signal (this can be done with control+z).
share|improve this answer
    
Oh oops, I was thinking of the kill signal –  Jeffrey Aylesworth Feb 4 '10 at 1:02
  1. the background job will die if you exit the shell you started it in. (eg SSH), that's why you use nohup or screen. If you have modern bash shell, there's a disown -h command. see man page of bash for more info
  2. Ctrl-C and suspend are different. Ctrl-C terminates the process.
  3. To append output to existing file : 1>>/path/existing_file
  4. ctrl-Z suspend the process, bring it into the background and give the control back to you. I don't know the exact meaning of your "stop"
  5. To append output and put in background: command 1>>/path/existing_file &
share|improve this answer
    
Sorry I should say CTRL+Z, not CTRL+C. What is the difference between CTRL+Z and stop? –  Tim Feb 4 '10 at 1:01
    
Also I wanted to ask how to append output to existing file while still make the job background. –  Tim Feb 4 '10 at 1:03
    
for "stop", see ece.osu.edu/computing/background_jobs.html#commands –  Tim Feb 4 '10 at 1:13
2  
@Tim: the page you linked to is about the C shell (csh) which has a stop command. Bash does not. –  Dennis Williamson Feb 4 '10 at 1:20
    
@tim, if you do a man stop ,you can see it says "request that the job be stopped". Basically that's that. –  user31894 Feb 4 '10 at 1:34

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.