Linux process in background - Stopped in jobs?
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:
- 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 &
. - Exit the terminal after starting the program, which will cause the association with the program's
stdin
to go away. But this will cause aSIGHUP
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 usingnohup
- 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 &
background process is always Stopped
Try running with nohup. You can analyse the nohup.out log if it still stops.
nohup script.sh
How can I resume a stopped job in Linux?
The command fg
is what you want to use. You can also give it a job number if there are more than one stopped jobs.
How to prevent a background process from being stopped after closing SSH client in Linux
Check out the "nohup" program.
`jobs` command doesn't show any background processes
Based on the problem statement of the question, IMHO I do not see any reason for using background
or foreground
. All you care is to find a process which is running in background so that you can kill it.
Run ps -ef | grep parameter3
to find processes which has parameter3
in the process name. You can adapt the grep
to uniquely identify a process, given you don't have two processes with exactly same process name.
Once you have it, just do kill -9 PID
and that process will be killed. So no need for bringing that process to the foreground for killing it.
Hope this helps.
Background job getting stopped
the first line of your script:
/#!/bin/bash
should actually be:
#!/bin/bash
I assume this was just a typo when posting your question (but you might want to check that out, just in case). Now, if i replace "command1" with an ls -las, it works for me:
#!/bin/bash
while [ 1 ]; do
ls -las | cut -f 3
sleep 5;
done
notice that i've only changed your "command1" by an ls.
so what is "command1" exactly? your job may be stopped if that "command1" needs stdin input (just one of the cases)
Preventing background process from writing to console
If you have started the process already and want to stop it from printing to stdout
, while still keeping it running, you may use:
stty tostop
When you give stty
the tostop
argument it stops the background processes that try to write to stdout
To enable the process to write again you may use fg
.
Original source can be found here
Related Topics
How to Mount a Usb Drive on Android Things
How to Assign a Name for a Screen
How to Pass Arguments with Special Characters to Call Shell Script
How to Remove Folders with a Certain Name
Bash Store Output as a Variable
How to Imshow with Invisible Figure in Matlab Running on Linux
Why Doing I/O in Linux Is Uninterruptible
How to Create a Statically Linked Position Independent Executable Elf in Linux
How to List Running Screen Sessions
Removing Sensitive Data from Git. "Fatal: Ambiguous Argument 'Rm'"
Platform Independent Resource Management
Print Field 'N' to End of Line
How to Get the List of Dependent Child Images in Docker
How to Non-Interactively Turn on Features in a Linux Kernel .Config File