Monday, June 25, 2007

Linux Process Control

Tools for working with processes
Checking running processes

While logged in as root, type "ps -ax |more" or "ps -aux |more". You will get a list of all processes running on your computer. You will see the process id (PID), process status (STAT) various statistics, and the command name. You can kill a process by typing "kill" and the PID number right afterwards similar to the line below.

kill 1721

You can also stop and restart processes by sending them various signals as in the below examples:
Setting up and doing process control

The examples in this section use the "yes" command as an easy method for an example of a program that runs continually. The "yes" command outputs the string "y" until it is killed or stopped. When the output is ported to the /dev/null (null device or bit bucket), the output is basically dumped. Therefore this command is harmless, but is a good demonstration. To put the process in the background, append an "&" character to the end of the command as shown below.

yes > /dev/null &

The system will respond with a job number and process ID or PID similar to:

[1] 10419

Either number can be used to refer to the job. The "jobs" command can be used to check the job. When the command is entered the system will respond with a list of running jobs similar to the following:

[1]+ Running yes >/dev/null &

The job can be killed using the process ID or the job number. Either

kill %1

or:

kill 10419

Stopping and restarting jobs

Another way to put a job into the background is to
  1. Start the job normally like:

    yes > /dev/null

    The prompt does not come back.
  2. Use the CTRL-Z key to stop the job.
  3. Use the command "bg" or "bg %1" where 1 is the job number to put the process in the background. The system reports the job number when you stop the job.
    Before the last step, the job was suspended. The "fg" command could have been used to bring the job into the foreground rather than using the "bg" command to put it in the background. If the job is running in the foreground, you can type CTRL-C to terminate the process.
Killing or Reconfiguring a Daemon without Restarting
To make changes to inetd:
  1. Reconfigure /etc/inetd.conf
  2. Restart inetd by sending it the hangup signal
The easy way to reset a service that was started via the rc script files during system startup:
  1. Find the file for the service, you want to start. For example find the file for the print daemon "lpd". These files should typically be in the directory "/etc/rc.d/init.d". The file name in this case is "lpd". (Note this is a script file, that starts the daemon, not the actual binary daemon file).
  2. Go to that subdirectory "cd /etc/rc.d/init.d" and type "./lpd restart".
  3. You should get output to the screen that indicates this service has been shut down and then started.
Setting process priority

In Linux, processes have a priority number between -20 and 19. The value of -20 is the highest, and 19 is the lowest priority. Process priority can be set with the nice(1) command and changed using the renice(8) command. To set a process to have the highest priority find the process ID number using the ps command. If your process name is "myprog" type:

ps -ax |grep myprog

You should get something like:

756 tty1 S 0:00 myprog

The first number on the line is your process ID. Enter the command:

renice -20 756

This will set your process (PID=756) to priority of -20. Modify the process ID number for that of your program running on your system. You can use the nice command to determine the default priority of new processes by typing "nice" on the command line. If you want to start a process with a specific priority, use the nice(1) command when you invoke the process.

Setting limits on the number of processes that can run

The command "ulimit" is used to limit the number of processes users can run along with available system resources. All processes which will be started from the shell (bash in many cases), will have the same resource limits. See the bash manual page for more information. To set the limits for daemons which are running at boot time add ulimit command to boot scripts.

The command "ulimit -a" reports the current limits.

Share It: ADD TO DEL.ICIO.US · ADD TO REDDIT · ADD TO STUMBLEUPON · ADD TO FURL · ADD TO LINKSMARKER · ADD TO Technorati · ADD TO Netscape · More Bookmark More Bookmarks...





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]