Process Management

From Colwiki.org

Jump to: navigation, search


Outcomes

In this Module you will learn about how to manage running linux processes including:
  • Managing the Init Process.
  • Viewing running processes
  • Modifying various attributes of running processes
  • Manage runlevels and system initialization from the CLI and configuration files(for example: /etc/inittab and init command, /etc/rc.d, rc.local)


Contents

Process Management

Processes have a unique Process ID the PID. This number can be used to modify a process' priority or to stop it.

A process is any running executable. If process_2 has been spawned by process_1, it is called a child process. The spawningprocess_1 is called the parent process.

The process family tree

The pstree command gives a good illustration of parent and child process hierarchy.

Part of the pstree output

 bash(1046)     ---xinit(1085)-+-X(1086)
                 `-xfwm(1094)-+-xfce(1100)---xterm(1111)---bash(1113)-+-pstree(1180)
                     |        |-soffice.bin(1139)---soffice.bin(1152)-+
                                          -soffice.bin(1153)
                     |                |                              |-
 soffice.bin(1154)                                                     
                     |                |                              |-
 soffice.bin(1155)                                                     
                     |                |                              |-
 soffice.bin(1156)                                                     
                     |                |                              `-
 soffice.bin(1157)                                                     
                     |           `-xclock(1138)                                        
                                     |-xfgnome(1109)
                                     |-xfpager(1108)
                                     |-xfsound(1107)
                                     `-xscreensaver(1098)


In the above figure all the process' PIDs are shown; these are clearly incremental. The most common used options are -p to display PIDs and -h to highlight users processes only.

Finding a running process

A more direct way to determine which processes are running is to use ps. Most users have a set combination of options which work for most situations. Here are three such options:

 ps -ux         all processes run by the user
 ps T           processes run under the current terminal by the user
 ps aux         all processes on the system

It is recommended you read the ps manpage and choose your own best options!

Image:Ps.PNG

Image:Options.PNG

Modifying a running process

Stopping processes The kill command can be used to send signals to processes. There are 63 signals available. The default signal terminates a process and is called SIGTERM with value 15.

kill

Syntax

kill SIGNAL process_PID Every process can choose whether or not to catch a signal except for the SIGKILL which is dealt with by the kernel. Most daemons redifine the SIGHUP to mean “re-read configuration file”.

Image:Common.PNG

One can also stop processes without knowing the process' PID using killall.

killall

Syntax killall SIGNAL process_NAME

Figure illustrating Interprocess signaling

Image:Process_signalling.PNG

Process priority and nice numbers

Nice numbers (NI) alter the CPU priority and are used to balance the CPU load in a multiuser environment. Each process is started with a default nice number of 0. Nice numbers range from 19 [lowest] to -20 [highest]. Only root can decrease the nice number of a process. Since all processes start with a default nice number of zero as a consequence negative nice numbers can only be set by root!

Image:Numbers.PNG

To modify a process' priority that is already running use renice. To set a process' priority use nice.

Syntax

Nice –<NI> <process>

renice <+/-NI> -p <PID>

Notice that renice works with PIDs and handles lists of processes at a time. A useful option to renice is the -u option which affects all processes run by a user.

Set nice number 1 for processes 234 and 765:

 Image:Network.PNG renice +1  -p 234 765   

Set nice number -5 for xclock:

 Image:Network.PNG nice  --5  xclock 

Processes and the Shell

Background and forground processes

After you have started a process from the shell you automatically leave the shell interpreter. You will notice that no commands will respond. The reason for this is that it is possible to run programs in the foreground fg or in the background bg of a shell. When a program is running in the foreground it is possible to recover the shell prompt but only by interrupting the program for while. The interruption signal is Ctrl Z.

Stopping and starting jobs

A process started from a shell is also called a job. Once the job receives the ^Z signal it is stopped and the shell prompt is recovered. To restart the program in the background simple type: bg.

Example

[mike localhost /bin]$xclock xclock running in forground, shell prompt lost [1]+ Stopped xclock xclock received ^Z signal [mike localhost /bin]$bg shell prompt recovered, issue the bg command [1]+ xclock & xclock is running in the background [mike localhost /bin]$

Notice the [1]+ symbol above. The integer is the process' job number, which it can be referred to as. The '+' sign indicates the last modified process. A '-' sign would indicate the second last modified process.

Listing jobs

The jobs utility lists all running processes started from the current shell. The job number, the job's state (running/stopped), as well as the two last modified processes, will be listed.

Image:Output.PNG

The job number One can conveniently stop and start a selection of jobs using the job number. This is achieved with the fg command.

Calling job 2 to the foreground and killing job 1

Image:Killingjob.PNG

Avoiding HUP with nohup

Finally there is a program called nohup which acts as a parent process independently from the user’s session. When a user logs off, the system sends a HUP to all processes owned by that process group. For example, to avoid this HUP signal a script called bigbang which attempts to calculate the age of the Universe should be started like this:

 Image:Network.PNG nohup bigbang &


Summary

As a system administrator, you will required to work with continuously running processes commonly reffered to as daemons. It is important to know how to troubleshoot these processes and ensure that they are available to deliver the services required to your users. What you have learned today is critical to your success as a system administrator.




Assignment

You should run X before starting these exercises.
  1. Check the current nice value of your running x-terminal. Change this value using top or renice.
  2. What is the equivalent signal of a ^Z sent to a process? (List all signals with kill –l)
  3. Which signal is redefined for most daemons and forces the configuration file to be reread?
  4. What is the default signal sent to a process, using kill or killall?
  5. Which signal is directly handled by the kernel and cannot be redefined?
  6. Make sure you log into a virtual terminal (tty1 to tty6) before doing this. We want to run a script that will continue to run once we logout using the nohup parent process.
  7. In the /tmp directory create a file called print-out with the following content:

Image:Killingjob.PNG



==

Headline text

==

Image:somerights20.png This work is licenced under a Creative Commons - By Attribution Licence - Share Alike License.

Personal tools
News & Events