** Credit Web
Job control facilities allow you to have the system work on a job in the background while you do something else at the keyboard.
This article covers the following:
If you are simply trying to get logged out, but have encountered the "There are stopped jobs" message, see the discussion of terminating background jobs below.
control-z
|
Stop (don't kill) the foreground job, and then return to the shell
|
jobs
|
Check the status of jobs in the current session
|
ps -u username
|
Check the status of processes, including those from other sessions. On BSD systems, use 'ps -gx'.
|
kill -9 %1
|
Kill a job, by specifying its job number after the percent sign
|
kill -9 123
|
Kill a process, by specifying its process id (PID) number
|
bg
|
Run the most recently stopped job in the background
|
fg
|
Bring most recently backgrounded job to the foreground
|
fg %1
|
Bring a job to foreground by specifying its job number after the percent sign
|
Jobs can be put in the background either by initiating them in the background or by stopping a foreground job and then specifying that it be continued in the background.
The way a job is initiated in the background is by putting an ampersand (&) at the end of the command line. For example, to start the excellent web browser Mozilla, if you plan on using the terminal for other activities while the web browser is running, you can type:
mozilla &
A second technique is to stop the foreground job with control-Z, and then to specify that the job be continued in the background with the "bg" command (The Carrot, ^, represents holding down the control key while you press the letter after it). So, if you start Mozilla
mozilla
^Z
bg
When you run in the background a program that uses a graphical user interface for interaction, you will still be able to interact with its graphical elements in your windowing system. If you suspend it with ^Z, you will not be able to interact with it until you start it running, either in the foreground (fg) or the background (bg).
The commands "jobs" and "ps" can be used to monitor the status of background jobs. As a demonstration, suppose that
1. A large compilation job is running in the background
2. Mozilla is started, and then stopped with ^Z
The report from the command "jobs -l" would look something like this:
[1] - 12527 Running make oberon
[2] + 12530 Stopped mozilla
The "[ ]" numbers are the job numbers that "bg" (background), "fg" (foreground), and "kill" use. (See the sections below for the details).
The report from the command "ps -u username" would look something like this:
PID TTY TIME CMD
10272 pts/10 3:40 mozilla-
9418 pts/10 0:00 csh
10198 pts/10 0:00 run-mozi
12527 pts/10 2:07 make oberon
The information in this display is explained in greater detail in "man ps". It shows the running processes in greater detail than the "jobs" output. The numbers in the PID column are the process identification numbers associated with the processes named in the COMMAND column, and can be used by the "kill" command (see below).
A background job may be brought to the foreground if it was initiated during the current login session. Jobs which were initiated during other login sessions and which are still running cannot be brought to the foreground. However, it is possible to send signals to such jobs using the "kill" command-- see below. A background job or a stopped job may be brought to the foreground with the command fg.
fg %1
The number following the percent sign is obtained from the leftmost column of the table given by the "jobs" command. See the discussion above, "Monitoring Background Jobs".
Jobs from the current session can usually be terminated by bringing them to the foreground and interrupting them with control-c key.
Another technique for terminating jobs running under your user id employs the "kill" command. There are two ways to refer to a particular job. The first uses the number in the leftmost column of the jobs command, as follows:
kill -9 %1
The second way uses the process id given in the leftmost column of the "ps -gx" command, as follows:
kill -9 pid-number
This can be used to terminate stopped and background jobs from the current session. It can be used to terminate jobs initiated by other login sessions of your user id.
The effect of background jobs on system performance depends upon the priority at which they run and how much RAM they require to run. Large jobs can consume immense amounts of RAM which can severely limit system performance. If the program requires too much RAM, the CPU will be wasting a lot of time swapping memory to and from disk rather than executing the jobs it needs to.
The other variable which affects system performance is job priority. If your jobs run at low priority, they will be done at the system's leisure and system performance will not be affected. If they are run at normal priority, they will increase the competition for resources and performance may be degraded. On a heavily loaded system, jobs should only be put into the background at low priority, using the "nice" command. To "nice" a job, just put the word "nice" before the command as you would regularly give it.
The current load on the system may be determined with:
uptime
The three numbers at the right of the resulting report indicate the load. If the first of the three is over 3, background jobs should only be initiated with low priority (niced). For example:
nice make oberon
To change the priority of a running job, use the command "renice". For example, if the command 'make oberon' with process ID 12527 is slowing things down too much for everyone else, you could use the command;
renice 19 12527
Process Priority
All processes have a priority assigned to them. By default, your jobs and processes are 10. A higher priority number means lower priority. Any processes found running on the system with a total of more than 5 minutes of system time used will be nice'd (priority set down). Processes found with over 60 minutes of system time used that are not nice'ed will be kill'ed.
nice command arguments is the command syntax for this. An example would be to make a program called bigjob run with the command option -o at a lower priority:
is the command syntax for this. An example would be to make a program called run with the command option at a lower priority:
frontier:˜> nice bigjob -o
Checking on Processes
The ps command is used to check on processes. An example with the results looks like:
frontier:˜> ps
| |||
PID
|
TTY
|
TIME
|
CMD
|
3427
|
pts/12
|
0:00
|
zsh
|
The first column, PID, is the process ID. The second column, TTY, is the port on which this process had been started. The third column, TIME, is the amount of CPU time the process has used. The fourth column,CMD, is the name of the command being run.
Running Processes While Logged Out
Normally, upon log out a hang-up signal (HUP) is sent to all processes started during that session. It is possible, however, to continue a process even after you have logged out. This may be beneficial for particularly lengthy processes. The shell command used to continue a process is nohup. Again, we ask you to nice any computationally intensive process. An example would be:
frontier:˜> nohup nice bigjob &
The output from a nohup'ed command will be sent to a file called nohup.out, unless otherwise redirected. You can no longer have command line interaction with a job that has been nohup'ed. The nohup'ed process will run to completion or until it is killed. To find such jobs use the ps -ef | grep username command.
0 comments:
Post a Comment