Linux Process Control

Posted: September 24, 2010 in Uncategorized

Tools for working with processes

  • accton – Turns process accounting on and off. Uses the file /var/log/pacct. To turn it on type “accton /var/log/pacct”. Use the command with no arguments to turn it off.
  • kill – Kill a process by number
  • killall – Send a signal to a process by name
  • lastcomm (1) – Display information about previous commands in reverse order. Works only if process accounting is on.
  • nice – Set process priority of new processes.
  • ps(1) – Used to report the status of one or more processes.
  • pstree(1) – Display the tree of running processes.
  • renice(8) – Can be used to change the process priority of a currently running process.
  • sa(8) – Generates a summary of information about users’ processes that are stored in the /var/log/pacct file.
  • skill – Report process status.
  • snice – Report process status.
  • top – Displays the processes that are using the most CPU resources.

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:

kill -STOP 1721 Stops (suspends) process 1721 by sending the STOP signal to the process. This process will still be on the task list. The process can’t catch or ignore the STOP signal.
kill -CONT 1721 Continue process 1721 causing it to resume. The CONT signal is sent to the process.
kill -TERM 1721 Terminates process 1721 by sending the TERM signal to the process. This process will no longer show up on the task list if it is actually terminated. Process terminated cannot be continued. The TERM signal can be caught so TERM is not guaranteed to kill the process.
kill -HUP 1721 Stops, then restarts process 1721. This is usually done when a process is not working properly or the configuration files for that process have been changed. This command sends the HUP signal to the process which means hangup. This signal can be caught by the process.
killall -HUP myprint Restarts any process with the name “myprint”.
kill -TERM myprint Terminates any process with the name “myprint”.

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 &@60Ctrl-C> to terminate the process.

Killing or Reconfiguring a Daemon without Restarting

killall -1 inetd Restarts inetd by sending signal number 1 which is the hangup signal.
killall -HUP inetd Causes the daemon to reload its config file by sending the hangup signal. The difference between this example and the previous one is the signal is called by name here rather than number.

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s