How can I put the current running linux process in background?
Suspend the process with CTRL+Z then use the command bg
to resume it in background. For example:
sleep 60
^Z #Suspend character shown after hitting CTRL+Z
[1]+ Stopped sleep 60 #Message showing stopped process info
bg #Resume current job (last job stopped)
More about job control and bg
usage in bash
manual page:
JOB CONTROL
Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash. [...] The user may then manipulate the state of this job, using the bg command to continue it in the background, [...]. A ^Z takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.bg [jobspec ...]
Resume each suspended job jobspec in the background, as if it had been started with &. If jobspec is not present, the shell's notion of the current job is used.
EDIT
To start a process where you can even kill the terminal and it still carries on running
nohup [command] [-args] > [filename] 2>&1 &
e.g.
nohup /home/edheal/myprog -arg1 -arg2 > /home/edheal/output.txt 2>&1 &
To just ignore the output (not very wise) change the filename to /dev/null
To get the error message set to a different file change the &1
to a filename.
In addition: You can use the jobs
command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1
or kill %2
with the number being the index of the process.
How to move a running process to background (UNIX)
Press control + Z, which will pause it and send it to the background. Then enter bg
to continue it's running in the background.
Alternatively, if you put a &
at the end of the command to run it in the background from the start.
This will just make it run in the background and once you log out it will still be killed. In order to keep it running after logout you will need to "disown" the process with disown -h
, so that the shell doesn't count it among your processes needing to be killed on logout. See this post for more details.
How do I put an already-running process under nohup?
Using the Job Control of bash to send the process into the background:
- Ctrl+Z to stop (pause) the program and get back to the shell.
bg
to run it in the background.disown -h [job-spec]
where [job-spec] is the job number (like%1
for the first running job; find about your number with thejobs
command) so that the job isn't killed when the terminal closes.
Change back into a running process on Linux after you put it into the background
If it's started from current shell, use standard job-control e.g.
$ jobs
$ gedit &
[1] 3341
$ jobs
[1]+ Running gedit &
$ fg %1
gedit
Run a shell script and immediately background it, however keep the ability to inspect its output
To 'background' a process when you start it
Simply add an ampersand (&
) after the command.
If the program writes to standard out, it will still write to your console / terminal.
To foreground the process
Simply use the fg
command. You can see a list of jobs in the background with jobs
.
For example:
sh -c 'sleep 3 && echo I just woke up' & jobs
To background a currently running process
If you have already started the process in the foreground, but you want to move it to the background, you can do the following:
- Press Ctrl+z to put the current process to sleep and return to your shell. This process will be paused until you send it another signal.
- Run the
bg
command to resume the process, but have it run in the background instead of the foreground.
How to use nohup on a process already running in background
nohup
starts a new process. You cannot retroactively apply it to a process that you've already started.
However, if the shell from which you launched the job is bash
, ksh
, or zsh
then the disown
job-control builtin may provide what you want. It can either remove the job from job control altogether or just flag the job to not be sent a SIGHUP
when the parent shell itself receives one. This is similar, but not necessarily identical, to the effect of starting a process via the nohup
command.
Note well that your job may still have issues if any of its standard streams is connected to the session's terminal. That's something that nohup
typically clobbers preemptively, but disown
cannot modify after the fact. You're normally better off anticipating this need and starting the process with nohup
, but if you're not so foresightful then disown
is probably your next best bet.
Note also that as a job-control command, disown
takes a jobspec to identify the job to operate on, not a process ID. If necessary, you can use the jobs
builtin to help determine the appropriate jobspec.
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 &
linux - running process background
You have two options:
- Use GNU screen, which will allow you to run the command and detach it from your terminal, and later re-attach it to a different session. I use it for long-running processes whose output I want to be able to monitor at any time. Screen is a truly powerful tool and I would highly recommend spending some time to learn it.
- Run the command as
nohup some-command &
, which will run the command in the background, detach it from the console, and redirect its output intonohup.out
. It will swallow SIGHUPs that are sent to the process. (When you close the terminal or log out, SIGHUP is sent to all processes that were started by the login shell, and the default action the kernel will take is to kill the process off. This is why appending&
to put the process in the background is not enough for it to survive a logout.)
How can place a job of linux terminal to background after enter password?
CTRL-Z is doing exactly what it should, which is stop the process. If you then want to put it in the background, the shell command for doing that is bg
:
$ ssh -N -D 7070 -l user 192.168.1.51
user@192.168.1.51's password:
^Z
[1]+ Stopped ssh -N -D 7070 -l mjfraioli 192.168.1.51
$ bg
[1]+ ssh -N -D 7070 -l user 192.168.1.51 &
That way you can enter the password interactively, and only once that is complete, stop it and put it into the background.
Related Topics
What Actually Is $Rpm_Build_Root
Creating PDF Invoices - Are There Any Templating Solutions
Get the Last 4 Characters of Output from Standard Out
How to Use Sed to Remove the Last N Lines of a File
Bash - Update Terminal Title by Running a Second Command
How to Open Vim with a Particular Line Number at the Top
How to Automate the Installation of Eclipse Plugins with Command Line
Is Kernel Space Mapped into User Space on Linux X86
How to Export Dbus_Session_Bus_Address
Simple Way to Convert Hh:Mm:Ss (Hours:Minutes:Seconds.Split Seconds) to Seconds
Count Occurrences of Character Per Line/Field on Unix
Linux - Understanding the Mount Namespace & Clone Clone_Newns Flag
After Changing /Etc/Profile, What Do I Have to Do to Reset My Shell
How to Cut an Existing Variable and Assign to a New Variable in Bash
How to Find Substring Inside a String (Or How to Grep a Variable)