![]() You can then do anything else you like, including putting the suspended program into the background using the bg command. The program pauses, and you get a new shell prompt. OS X’s Unix shells also have a feature (mentioned earlier) called job control that allows you to use the suspend character (usually Control-Z) to suspend a program running in the foreground. In other shells, enclose the command sequence in parentheses before adding the ampersand: In bash, you can put an entire sequence of commands separated by semicolons ( ) into the background by putting an ampersand ( &) at the end of the command line. Also, bash writes a status line to your screen when a background process finishes. In this instance, the command would look like:įortunately, you don’t need to remember the PID every time, because there are Unix commands (explained in the next section) to check on the processes you have running. ![]() To cancel a process, use the kill command, followed by the PID of the process you want to cancel. To check on the status of the process, use the ps command with the following two options: -f to have expanded output, and -p because you’re specifying a process ID. The PID is useful when you want to check the status of a background process or if you need to cancel it. Sorting is a good example, because it can take a while to sort huge files. The shell then assigns and displays a process ID (PID) number for the program: To run a program in the background, all you need to do is add the & character at the end of the command line before pressing the Return key. It’s also done whenever you want to launch a new application from an existing Terminal window, so you can keep working in the existing Terminal window, as well as within the new application. Running a program as a background process is most often done to free a Terminal when you know the program is going to take a long time to complete. Depending on your system and your shell, you may be able to close the Terminal window or even completely log off from OS X while the background process completes. The original program runs in the background, but you can use the same Terminal window to do other things during that time. When you enter a command as a background process, the shell prompt reappears immediately so that you can enter a new command. In OS X and other modern operating systems, however, you can enter new commands in the “foreground” while one or more programs are running in the “background.” On an old-school, single-task operating system, you would enter the command and wait for the job to finish, returning you to the command prompt (which is your indication that you’re free to enter a new command). Why else would you want job control? Suppose you’re launching a Unix program that takes a long time to run. Get a free trial today and find answers on the fly, or master something new and useful. Join the O'Reilly online learning platform. For instance, you may prefer to do most of your work from one Terminal window, instead of having multiple Terminal windows open when you really don’t need to. Even if you’re using a window system, you may want to use job control to do several things inside the same Terminal window. These processes can all be run and managed through a single Terminal window, with a little help from something called job control. But on the Unix side, OS X allows you to run multiple Unix programs and/or processes at the same time as well. ![]() Most users think of multitasking in terms of the way OS X handles applications like Adobe Photoshop, Microsoft Word, Mail, Messages, Safari, and so on-allowing you to have multiple applications open, each with its own windows. As new applications are launched, processes are started, and others go idle or shut down entirely, the system monitors each of these tasks and doles out memory and CPU resources on the fly to make sure everything runs smoothly. OS X can do many jobs at once, dividing the processor’s time between running applications and system processes so quickly that it looks as if everything is running at the same time. This is an excerpt from Learning Unix for OS X, 2nd Edition, by Dave Taylor. ![]()
0 Comments
Leave a Reply. |