A Rather Long Introduction

Can put a process into background using “command &“, typing “fg” will bring it to the foreground, you can press control-z to pause it and put it in the background, after that you probably want to unpause it while its still in the background (it will keep running in the back) type “bg“. Pressing control-C on a current process will cancel it completely. While a process is in the background, you can type “disown” to remove the job from the process list.
If you want to have the freedom of running commands in the background and being able to close the shell without killing the process you need to untie the shell (such as putty) from sending a HUP command to the process  when you close the shell. This is called “nohup”ing. You can untie it from shell (aka nohup) using “nohup command &“, all output of the commmand will go to file in the current working directory called “nohup.out”, you can redirect the output to a different file like so “nohup command 2> file2 > file1 &” or “nohup command 2>&1 >file1” (which is the same as “nohup command &> file1 &“. In the first redirection example, file1 gets normal output and file2 gets the error output, in the second and third example both normal & error output go to file1.
sidenote: untying a process from the shell means that when the shell closes it will not send it a hangup signal (hup), if it was still tied to the shell it would receive a hup message and it will close. The shell naturally throws hup signals when it itself is hupped(closed), it throws them at all processes that are running or stopped, but not to processes that were untied from the shell.
An alternative way to nohup a process is to start a process into the background and then disown it (with the -h): so first run this “command &“, that will start the commmmmand into the background but it will still be tied to the shell (so closing this putty session, for example, will kill the command you ran), now to untie it from the shell type “disown -h“.  This is useful if you launched a process into the background and decided later you want the process to continue to run when you close the shell.
Another alternative way to nohup a process is to first start the process like normal (into the foreground): “command“. Now put it into the background with control-z, it will be paused so type “bg” to start it up (it will still be in the background). Finally type “disown -h” to untie it from the shell. This is useful if you started a process and decided you need to close down the shell and still have the process run.
The last three paragraphs above talk about nohupping a process, its a great way to continue doing other things without having to worry about your background task ever closing, its a great safety blanket knowing that your process will run. For example your working on something super important remotely and storm rolls by where your at (not where the server is at), so you can nohup the process and if your computer (where your terminal shell is) gets shutoff you can have the assurance that your process still runs. There are other great tools that give you alot more features using this “nohupping” technique, I recommend checking out and fully learning the following three and in this order: “dtach” (yup thats detach without the e), “screen“, “tmux“. The big picture here is “Process Control” and I would have the following sections if I were to teach it in a logical order: “normal launching process”, control-c, control-z, “kill“, foreground and background using “bg“/”fg“, listing jobs with “jobs” and “jobs -l“, what it means to nohup and how to “nohup“, and then disowning/nohupping background task with “disown“, disowning/nohupping foreground tasks with “control-z,bg and disown -h“, “dtach” (thats detach without the e again), “screen” and “tmux“.
One thing I forgot to mention in the summmary, you can list the current jobs with “jobs” and if you want to see the process id type “jobs -l“. The rest of the article will be a more indepth look on all of this. Also it will show you how to deal with multiple processes. Everything above in the summary deals with one process in the background, if you have more then the following notations will become handy %n, where n is the job number such as %1, %2, which can be appended to most of the commands above to signify what job number you want to affect with “fg %2“,”bg %2“,”disown %2“, or “disown -h %2“.
NOTE: THE DIFFERENCE BETWEEN disown AND disown -h: you can use “disown” or “disown -h” to remove a process from the shell (so that when you turn off putty for example the process still runs – as side note to jog your head you can also use “nohup command &” to unie a process from the shell but thats not what is being asked here) – so whats the difference between “disown” and “disown -h“? The difference between them is that “disown” will untie the process from a shell (by supressing HUP/hang up messages) but it will also remove the process from the job list (seen with “jobs” or “jobs -l“), so you wont be able to control it with “bg” or “fg“, where as “disown -h” only unties the process from a shell (by supressing HUP/hang up messages), so that disown -h doesnt remove the process from the job list, so you will be able to use “bg” or “fg“. So “disown -h” and “disown” both untie a process from a shell, but “disown” without -h removes the process from the job list, where “disown -h” does not.
NOTE: WHY SHOULD I RUN “nohup command &” WITH THE BACKGROUND SYMBOL? Because “nohup command” redirects all output to a file, so might as well send the whole process to the background using “&“. Common practice is to do “nohup command &“, you never see “nohup command
NOTE: WHATS THE DIFFERENCE BETWEEN disown AND disown -h AND nohup? Well “disown” and “disown -h” have in common that you have to start a process first and then get it into the background (or do it together “command &“) and then you can unattach it from the shell via “disown” or “disown -h” – nohup does all that in one move “nohup command &“. There are other differences too. nohup keeps processes in the job list (until you turn off the shell – when you come back to a shell the process still runs but your job list is gone).” disown -h” keeps processes in the jobs list and it also unties the process from a shell, however your output doesn’t get redirected to a file (it all stays on the terminal), with “disown” (and “disown -h“) your stderr and stdout go to the terminal (so it can get confusing if you have a couple of apps/processes disowned with “disown” or “disown -h” as they all will be writing to the terminal/screen at the same time). With “disown” & “disown -h” & “nohup” If you close the shell and return, your process will still be running but your job list will be blank just like I mentioned happens with nohup previously. The between regular “disown” (so not “disown -h“) and “nohup” is that regular “disown” removes the job from the job list upon entering the command “disown” where as “nohup” & “disown -h” does not, and the other difference “nohup” redirects output to a file (nohup.out) where as “disown” (and “disown -h“) does not, but the similarity is that all three untie the process from a shell.
disown: untie from shell (so can turn off shell & process still runs); untie from stdin (so keyboard is not listened to by the command); stdout and stderr go to terminal; removed from job list;
disown -h: untie from shell (so can turn off shell & process still runs); untie from stdin (so keyboard is not listened to by the command); stdout and stderr go to terminal; (side note: notice its not removed from the job list, so it can be listed with “jobs” and “jobs -l”, up until you get out of the shell, upon return to a new shell you will not see the process with “jobs” and “jobs -l”)
– nohup command &: untie from the shell (so can turn off shell & process still runs); and fully unties from the terminal (stdin from keyboard not listened to, stdout and stderr go to nohup.out, but that can be pointed/redirected to another file if you want – checkout articles on “nohup redirection” on google.com and kossboss.com);  (side note: notice its not removed from the job list, so it can be listed with “jobs” and “jobs -l”, up until you get out of the shell, upon return to a new shell you will not see the process with “jobs” and “jobs -l”)
NOTE: WHAT DOES UNTYING A PROCESS FROM THE SHELL MEAN? When you close a shell, it sends a HUP/hangup signal to all of its processes (stopped and running processes) so they all close. When you untie a process from the shell, that process will not get that HUP/hangup signal when the shell closes, thus allowing you to continue working. A shell is launched when you login with something like xterm,ssh or putty, so when you close out of lets say putty, all processes will get shut that you launched (unless they were untied from the shell using nohup,disown, or any of the other tools that I lightly mentioned: dtach,screen,tmux)
NOTE: WHAT DOES UNTYING A PROCESS FROM THE TERMINAL MEAN? It just means basically that the output will not go to the terminal / your screen. So normally stderr and stdout are the terminal your working on (so that you see messages on the screen) and stdin is the keyboard so that it accepts your keystrokes. When you fully untie a process from the terminal, that process will no longer obey commands from your keyboard and it will no longer throw stuff on the screen (from stderr or from stdin). nohup unties processes from the terminal, all output from stderr and stdin goes to a file called nohup.out, also all input from the keyboard is ignored, its essentially impossible to send it keyboard commands (so this brings up an interesting question ill mention in a second). When you disown a process it doesn’t fully untie from the terminal, its stdin from the keyboard is untied, but its output from stderr and stdout still goes to the screen. Notice how disown/disown -h/nohup all untie the process from the terminal in some way (disown/disown -h only untie it from stdin/keybaord, and nohup fully unties a process so that not only stdin/keyboard is supressed, but stdout & stderr go to a file), but they are only tools to untie a process from a shell. So how do you untie a process from a shell, but not untie it from the terminal? The answer is with shell sessions such as those made with dtach,screen, or tmux – they can make a shell session which is untied from the shell, but you still have full shell control, when you close the putty, your session continues to run (and so do its processes), when you come back to the shell (your processes are still running) and normally (with using disown/disown -h/nohup) you wouldnt be able to control those processes (tie a terminal back to it so that keyboard gets stdin, and terminal/your display gets stdout & stderr), however with dtach/screen/tmux when you come back to a shell, you can log back into your session. Dtach keeps sessions in sizeless files called sockets files, and its as simple as it gets. Screen and tmux do so without socket files (hell maybe they exist somewhere in /tmp or /var – i didnt look), but they allow for sessions to be created in panes/panels or in different tabs (called windows). So with screen & tmux you can literally have 10 bash shells running under one session (its pretty cool), and all of those 10 bash shells are untied from putty (so when you close putty, your 10 bash shells and whatever processes they are running dont close). Check out articles on dtach/screen/tmux on google and kossboss.

Showing some examples

(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 🙂
 
 
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/
To simulate this type of effect with nohup you could type  nohup seq 1 10000000 | tee /tmp/file1. 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.
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/

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.

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:

seq 1 1000000000 > /tmp/file3

ctrl+z

seq 1 1000000000 > /tmp/file4

ctrl+z

jobs

will show you that you have 2 jobs

bg 1

disown -h %1

bg 2

disown -h %2
exit


(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
 
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”

One thought on “JOBS & PROCESS CONTROL – moving process to background – a new process, or a running process – bg/fg & the difference between disown/disown -h/nohup

Leave a Reply

Your email address will not be published. Required fields are marked *