LINUX - Jobs - Move Running Process to Background & Nohup
(method 1)
ALREADY RUNNING PROCESS INTO BACKGROUND
Pro: Puts running process into background
Con: If you quit out of the shell window the process stops
1. Ctrl-z
2. jobs
or alternate method which lists the PID (note the PID is not the jobnum, the job number is shell specific to the current bash session): jobs -l
3. bg %jobnum
or alternate method %jobnum & for example for the first job %1 &
To place a foreground process in the background: suspend the foreground process (with Ctrl-z) then enter the bg command to move the process into the background.
Show the status of all background and suspended jobs: jobs
Bring a job back into the foreground: fg %jobnumber
Bring a job back into the background: bg %jobnumber
(method 2 - my favorite)
ALREADY RUNNING PROCESS INTO NOHUP
Pro: Puts running process into background, and if you quit out of the shell it still runs
Con: Costs a pinch of more memory (a negligiable amount) - I couldnt think of any cons so I had to mention this :-)
Reference: http://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup
Using the Job Control of bash to send the process into the background:
0. Run some SOMECOMMAND
1. ctrl+z to stop (pause) the program and get back to the shell
2. bg to run it in the background
3. disown -h so that the process isn't killed when the terminal closes
4. Type exit to get out of the shell because now your good to go as the operation will run in the background in it own process so its not tied to a shell
This process is the equivalent of running nohup SOMECOMMAND
NOTE1: Example - When running SOMECOMMAND at step 0, if the command outputs alot of text on the screen to where it seems like its overloading you and not accepting your commands - dont worry, just be patient -it actually is taking your commands give a second or two rest in between each step. For example test this command like so:
The seq START FINISH will output every integer from START to FINISH. seq 1 to 5. prints 1 thru 5, with each new
number on a new line. the Tee command outputs to the screen and to a file, so I can simulate the purpose of this NOTE1.
seq 1 10000000 | tee /tmp/file1
note this command will out put alot of text on the screen
ctrl+z
once you press ctrl+z wait a second the output should stop, you can confirm the stopped job with
the command "jobs". Also note Once you press ctrl+z the command stops and your presented with the command prompt.
bg
Once you type bg the heavy amount of screen output will resume - it will seem almost futile to try and enter any
commands, but dont worry, it still accepts your commands its just hard/impossible to see what you enter
disown -h
After typing disown -h while the text is flowing up the screen you know you put it into that mode
confirm that it works by exiting the shell
exit
The command will still run in the background under its own process - just like nohup
LOG BACK INTO THE SHELL: and check the size of the file a few times - if its increasing then the disown worked and the command is still running
ls -ls /tmp/
NOTE2: Example - Running SOMECOMMAND at step0, that doesnt output to the screen. This is easier to work with because you can see the disown and exit commands
seq 1 10000000 > /tmp/file2 note the command will not output any text to the screen like before, but you will not be able to put any more commands the command will be gone
ctrl+z
once you press ctrl+z the command stops and your presented with the command prompt
bg
Once you type bg you will be presented with a command prompt where you can still type commands freely
disown -h
The disown -h will put that command into its own process so that you can exit the shell and the command still
runs
exit
The command will still run in the background under its own process - just like nohup
LOG BACK INTO THE SHELL: and check the size of the file a few times - if its increasing then the disown worked and the command is still running
ls -ls /tmp/
NOTE 3: you can specify which job to disown - otherwise disown -h without a job specifier will just put every job in the background. Type "jobs" to see all the jobs - note if you have alot of text flowing on the screen from a command you want to put in the background like in NOTE1 it will be near impossible to see the jobs output. Anyhow disown -h %n where n is the job number will but job n in the background. The job number is like the PID but not instead of the process it relates to the job number. Your job numbers will most likely be 1 or 2 or 3 etc., not like 1000 or 30000 which is what PIDs look like. An example:To simulate this type of effect with nohup you could type nohup seq 1 10000000 > tee /tmp/file2. But the whole point of this exercise is to show you how to put something into the background incase you forgot to nohup it in the beginning. seq 1 1000000000 > /tmp/file3ctrl+zseq 1 1000000000 > /tmp/file4ctrl+zjobswill show you that you have 2 jobsbg 1disown -h %1bg 2 (method 3)
PUT BACKGROUND PROCESS INTO NOHUP
This is like method2, except lets say you put the process into the background but you want to nohup it. Why would you want to nohup it? Simple - Because you need close out the shell and you dont want your process to stop:
Solution:
jobs
disown %n
where n is the job number
Example:
# rsync -avz /source /destination &
and its running in the background taking its sweet time to complete
# jobs
shows me its running with job number 1
# disown %1
now I can exit the shell with it still running safely
(method 4)
MANUAL PROCESS TO BACKGROUND (this is just like method1)
Pro: Same as first step but more Matrix Style - puts running process into backgroung with low level kernel signals
Con: If you quit out of the shell window the process stops
Reference: http://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup
If Ctrl-Z is not working
Open another shell and:
ps -aux
(or)
ps aux
Then identify the PID of the process for example. And put that number in the PID field below.
kill -20 PID
kill -18 PID
kill -20 will suspend the process
kill -18 will resume the process, in background. So now, closing both your terminals won't stop your process.
Notes
KILL SIGNALS BELOW - COMMON SIG COMMANDS
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at tty
SIGTTIN 21,21,26 Stop tty input for background process
SIGTTOU 22,22,27 Stop tty output for background process
ALTERNATE SOLUTION TO NOHUP AND BACKGROUND PROCESSES
1. dtach - this starts a bash/sh session in another process so if you exit out of your shell it still runs
Quick lesson on dtach
To start a new sesssion type the following (note it makes a socket file called dtach.session): dtach -A /tmp/dtach.session /bin/bash
CONTROL+\ (key above enter key) = this gets you out of your dtach session and back in the shell you originally were in
You can now close out of your shell (putty, or xterm, etc.) and it will still run
To get back in your session dtach -A /tmp/dtach.session
To end the dtach session, either restart or get back in the session and then type cd / and then exit to close the session once and for all then remove the socket file
rm /tmp/dtach.session
NOTE1: It doesnt matter if you call it dtach.session, i could of just called it sally without the .dtach. the /bin/bash argument is needed but you can put another shell like
/bin/sh or even another program like rtorrent
2. screen - this is like dtach but with more "dtachs" you can even split the screen horizontaly and verticaly
Google it to find out how to use it - just google "how to install screen linux" and "screen linux cheatsheet"
3. byobu - this is like screen but with a cool looking bash screen border and everything and easier to use keys
Google it to find out how to use it - just google "how to install byobu" and "how to use byobu"
|
No comments:
Post a Comment