Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux process in background - "Stopped" in jobs?

Tags:

linux

bash

I'm currently running a process with the & sign.

$ example & 

However, (please note i'm a newbie to Linux) I realised that pretty much a second after such command I'm getting a note that my process received a stopped signal. If I do

$ jobs 

I'll get the list with my example process with a little note "Stopped". Is it really stopped and not working at all in the background? How does it exactly work? I'm getting mixed info from the Internet.

like image 994
user1255410 Avatar asked Jul 12 '13 18:07

user1255410


People also ask

How do you stop a stopped job in Linux?

We can terminate jobs using the kill command followed by either the job ID, a substring, or the process ID.

What is a stopped job Linux?

The first thing you need to understand is what stopped jobs are. Basically, they are jobs that have been temporarily placed in the background. Say, for instance, you run the top command. Instead of actually closing it with Ctrl+C, you'd rather keep it running in the background, so you can recall it later.

Can a job run in the background Linux?

To run a job in the background, you need to enter the command that you want to run, followed by an ampersand (&) symbol at the end of the command line. For example, run the sleep command in the background. The shell returns the job ID, in brackets, that it assigns to the command and the associated PID.

How to kill all stopped jobs in a Linux system?

For us to kill all stopped jobs, we need to tie two commands together. The first will get the PIDs of all stopped jobs, and the next will kill all the jobs provided. This command shows all the stopped jobs. Having this, we can get the PIDs of the stopped jobs and pipe them to kill command as:

How do I put a job in the background in Linux?

To initiate a job in the shell as a background job, we use the ampersand (&) symbol. Using this tells the shell to put whatever commands come before the ampersand in the background and immediately show the shell prompt. The example below shows how to put the Firefox job (in the above example) in the background.

What are Linux jobs and their statuses?

A key concept to understand about Linux jobs is their statuses. There are two main statuses for Linux jobs: A foreground job refers to a command or a program executed in the shell and occupies the terminal session until it completes. An example would be launching a file manager or browser in the terminal

What is foreground job in Linux?

A foreground job refers to a command or a program executed in the shell and occupies the terminal session until it completes. An example would be launching a file manager or browser in the terminal


2 Answers

In Linux and other Unix systems, a job that is running in the background, but still has its stdin (or std::cin) associated with its controlling terminal (a.k.a. the window it was run in) will be sent a SIGTTIN signal, which by default causes the program to be completely stopped, pending the user bringing it to the foreground (fg %job or similar) to allow input to actually be given to the program. To avoid the program being paused in this way, you can either:

  1. Make sure the programs stdin channel is no longer associated with the terminal, by either redirecting it to a file with appropriate contents for the program to input, or to /dev/null if it really doesn't need input - e.g. myprogram < /dev/null &.
  2. Exit the terminal after starting the program, which will cause the association with the program's stdin to go away. But this will cause a SIGHUP to be delivered to the program (meaning the input/output channel experienced a "hangup") - this normally causes a program to be terminated, but this can be avoided by using nohup - e.g. nohup myprogram &.

If you are at all interested in capturing the output of the program, this is probably the best option, as it prevents both of the above signals (as well as a couple others), and saves the output for you to look at to determine if there are any issues with the programs execution:

nohup myprogram < /dev/null > ${HOME}/myprogram.log 2>&1 & 
like image 176
twalberg Avatar answered Sep 16 '22 15:09

twalberg


Yes it really is stopped and no longer working in the background. To bring it back to life type fg job_number

like image 24
PP. Avatar answered Sep 18 '22 15:09

PP.