======================================= Shell background/foreground job control ======================================= -IAN! idallen@idallen.ca (This file is not a required part of CST8129 - it is useful but optional.) Normally the shell starts every command and waits for it to finish before prompting you for the next command. You can tell the shell not to wait for the command to finish by ending the command line with an ampersand ('&') character, or by using the ampersand to separate commands instead of using the semicolon (';'): $ sleep 5 & [1] 27369 $ echo hi hi $ [1]+ Done sleep 5 $ sleep 5 & echo hi [1] 27380 hi $ [1]+ Done sleep 5 To put multiple commands into the background, enclose the list of commands in parentheses: $ ( sleep 5 ; echo hi ) & [1] 27469 The "jobs" command lists the background processes of the current shell (but not of any other shells): $ sleep 888 & [1] 27380 $ jobs [1]+ Running sleep 888 & $ bash $ jobs $ sleep 999 & [1] 27382 $ jobs [1]+ Running sleep 999 & $ exit $ jobs [1]+ Running sleep 888 & A background job continues to run even after the shell that started it exits. You can use the "ps gx" command to see all your processes, including background processes: $ ps gx 27811 pts/3 00:00:00 bash 27997 pts/3 00:00:00 sleep 28001 pts/3 00:00:00 ps Background jobs cannot read your keyboard. If they try, the Linux kernel will suspend ("stop") the job until you bring it to the foreground again: $ ( sleep 5 ; cat >out ) & $ [1]+ Stopped ( sleep 5; cat >out ) $ fg ( sleep 5; cat >out ) hi ^D $ You can suspend ("stop") a foreground process and turn it into a suspended job using the ^Z character from your keyboard. You can put the stopped job into the background using "bg" and into the foreground using "fg". You can kill a job by number using "kill": $ sleep 777 # start a foreground sleep process ^Z # ^Z suspends ("stops") the process [1]+ Stopped sleep 777 $ jobs [1]+ Stopped sleep 777 $ bg # bg continues the process in the background [1]+ sleep 777 & $ jobs [1]+ Running sleep 777 & $ kill %1 $ [1]+ Terminated sleep 777 A job that is suspended ("stopped") is not using any CPU. It is suspended in mid-execution, unfinished. It occupies computer memory. If the job has files open, the files remain open. Since the job is unfinished, open files are likely empty or half-written. Your shell will warn you (only once) if you try to exit the shell with stopped jobs: $ sleep 777 ^Z [1]+ Stopped sleep 777 $ jobs [1]+ Stopped sleep 777 $ exit There are stopped jobs. $ If you exit a shell that has stopped jobs, the stopped jobs will be sent a signal that usually causes them to terminate, unfinished. If the stopped job is an editor (e.g. VI/VIM), you will have to recover the file being edited using the recover feature of the editor ("vim -r"). Having multiple stopped editors, all editing the same file, is a recipe for disaster. The last editor session that saves the file will overwrite all the work done by the other editor sessions. The VIM series of editors try to warn you about this; but, if you ignore the warning, you will eventually overwrite a good file with a bad one.