Saturday, 21 May 2005

Job Control in Linux

How to Put a "Running Job" in the Background.
Linux is a multitasking operating system. Which means that you can run multiple jobs at the same time. Suppose you are logged in to a terminal which does not have X server running. And you're running a job at the terminal prompt, which is taking a very long time to complete. You want to put the job in the background. This is how you do it.
Press "CTRL - z" which temporarily suspends the job. Now run the command :
$ jobs
This will list all the jobs along with a job number. Now to resume a particular job in the background, do the following:
$ bg %jobnumber
in our case it will be
$ bg %1
To bring the job back to the foreground again, do :
$ fg %jobnumber
which in our case is
$ fg %1
Need to kill all jobs ?
Suppose you're using several suspended vi sessions and you just want everything to exit. This is achieved by running the following command:
$ kill -9 `jobs -p`
The "jobs -p" gives the process number of each job, and the "kill -9" kills everything. Sometimes "kill -9" is excessive and you should issue a "kill -15" that allows jobs to clean-up.
Sometimes you need to list the process id along with job information. For instance, here's process id with the listing.
$ jobs -pl
How to lower or raise the priority of a job
You use the 'renice' command for changing the priority of a running job. For example :
$ nice -n +15 find . -ctime 2 -type f -exec ls {} \; > last48hours
[Ctrl+z]
$ bg
Above we execute a 'find' command with a priority of 15. Then we suspended the job with a ctrl+z . Then, the job was run in the background by using the command 'bg'. Now, if you want to change the priority of the job to a lower value, you just 'renice' it as follows:
First get the process ID of the job.
$ jobs -pl
[1]+ 29388 Running
Now lower its priority.
$ renice +30 -p 29388
29388: old priority 15, new priority 19
The range of priority goes from -20 (highest priority) to +19 (lowest priority). You can lower the priority of your program but you cannot increase the priority unless you are root.

No comments:

Post a Comment