Beginners Guide to Performing Basic Process Control in Solaris

Solaris OS Processes

Every program you run in the Solaris OS creates a process. When you log in and start the shell, you start a process. When you perform a command or when you open an application, you start a process. The system starts processes called daemons. Daemons are processes that run in the background and provide services. For instance, the desktop login daemon (dtlogin) provides a graphical prompt that you use to log in.

Using a PID

Every process has a unique process identification number (PID), which the kernel uses to track, control, and manage the process.

Using Process UID and GID Numbers

Each process is associated with a UID and a GID. These numbers indicate who owns a process and determine the functions of a process. Generally, the UID and GID associated with a process are the same as the UID and GID of the user who started the process.

Understanding the Parent Process

When one process creates another, the first process is considered to be the parent of the new process. The new process is called the child process. While the child process runs, the parent process waits. When the child finishes its task, it informs the parent process. The parent process then terminates the child process. If the parent process is an interactive shell, a prompt appears, indicating that it is ready for a new command.

Viewing a Process

You can use the process status (ps) command to list the processes that are scheduled to run in that shell. The ps command has several options you can use to determine which processes to display and how to format the output.

Using the ps Command

The syntax for the ps command is as follows:

$ ps options

For each process, the ps command displays the PID, the terminal identifier (TTY), the cumulative execution time (TIME), and the command name (CMD).

Identifying the ps Options

The table below describes some of the options you can use with the ps command.

Option Description
-e Prints information about every process on the system, including the PID, TTY, TIME, and CMD
-f Generates a full (verbose) listing, which adds fields including the UID, parent process identification number (PPID), and process start time (STIME)

Displaying a Listing of All Processes

You can use the ‘ps -ef’ command to view a listing of all the processes currently scheduled to run on the system. The following example shows how you can use the ps -ef command to display a listing of all processes.

$ ps -ef | more
UID     PID   PPID C STIME TTY      TIME CMD
root     0     0   0   Oct 23 ?     0:18 sched
root     1     0   0   Oct 23 ?     0:01 /etc/init
root     2     0   0   Oct 23 ?     0:00 pageout
root     3     0   0   Oct 23 ?     17:47 fsflush
root     7     1   0   Oct 23 ?     0:00 /lib/svc/bin/svc.startd
root     9     1   0   Oct 23 ?     0:00 svc.configd
--More-
... (output truncated)

The figure below shows a description of the fields in the output of the ps -ef command.

The ps -ef Command Output Description in Solaris

Searching for a Specific Process

In the Solaris OS, you need to be able to search for processes that are running on the system. You can use the ps and grep commands together, or the pgrep command alone, to search for specific processes.

Using the ps and grep Commands

To search for a specific process, you can combine the ps and grep commands using the pipe (|) character. The pipe character causes the ps command to send the list of processes to the grep command. The grep command then searches the list of processes for the text that you specify.

The following example shows how to find active processes with names that contain the string lp:

$ ps -e | grep lp
217 ?        0:00 lpsched
$

Using the pgrep Command

You can use the pgrep command to search for specific processes by name. By default, the pgrep command displays the PID of every process that matches the criteria you specify on the command line. The syntax for the pgrep command is:

pgrep options pattern

The table below describes some of the options you can use with the pgrep command.

Option Description
-x Displays the PIDs that match the pattern exactly.
-n Displays only the most recently created PID that contains the pattern.
-U uid Displays only the PIDs that belong to the specified user. This option uses either a user name or a UID.
-l Displays the name of the process along with the PID.
-t term Displays only those processes that are associated with a terminal in the term list.

The following example shows how to use the pgrep command display the PID of any process with a name that contains the string lp:

$ pgrep lp
217
$

The following example shows how to use the pgrep command with the -l option to display the PID and name of any process with a name that contains the string lp:

$ pgrep -l vold
217 vold

The following example shows how to use the pgrep command with the -l option to display the PID and name of any process with a name that contains the string mail:

$ pgrep -l dt
959 sdtvolch
548 dtlogin
975 dtfile
991 dtexec
950 dtwm
977 dtterm
992 dtterm
954 sdtperfmeter
948 dtsession
932 sdt_shell
976 dtexec
951 dtfile
842 dtlogin

The following example shows how to use the pgrep command with the -lt option to display the PID and the name of any process associated with a terminal window:

$ pgrep -lt pts/2
697 ksh

The following example shows how to use the pgrep command with the -fl options to display:

$ pgrep -l mountd
155 automountd

$ pgrep -fl automountd
155 /usr/lib/autofs/automountd

Using the ptree Command

The ptree command displays a process tree based on the process ID that was passed as an argument to the command. The output has the specified PIDs or users, with child processes indented from their respective parent processes. An argument of all digits is taken to be a PID, otherwise it is assumed to be a user login name. The default behavior is all process will be displayed. The following is an example of the ptree command:

$ ps
PID TTY      TIME CMD
52109 pts/71   0:00 bash
30739 pts/71   0:00 ps

Using the PID for the bash shell that you are using as the argument to the ptree command causes the process tree for that process to be displayed:

NOTE: Output in the following example is modified for display purposes.

$ ptree 52109
 554   /usr/dt/bin/dtlogin -daemon
     45089 /usr/dt/bin/dtlogin -daemon
	     45214 /usr/dt/bin/dtlogin -daemon
		    45215 /bin/ksh /usr/dt/bin/Xsession
			   45291 /usr/dt/bin/sdt_shell -c unset DT; DISPLAY=:51;
			      45294 -bash -c unset DT; DISPLAY=:51;
				     45308 /usr/dt/bin/dtsession
					     45314 dtwm
						    52091 /usr/dt/bin/dtexec -open 0 -ttprocid
							   52109 /bin/bash
							     30917 ptree 52109

From this output it can be seen that PID 52109 has PID 52109 as its parent.

Sending a Signal to a Process

A signal is a message that you can send to a process. Processes respond to signals by performing the action that the signal requests. Signals are identified by a signal number and by a signal name, and each signal has an associated action. For example, signal 2 is known as the SIGINT signal. If you press Control-C, you send the SIGINT signal to the process running in your current terminal window. The process responds by exiting. Generally, you use either the kill command or the pkill command to send signals to stop processes.

The table below describes some of the available signals.

Signal Number Signal Name Event Definition Default Response
1 SIGHUP Hang up A hang-up signal that drops a telephone line or terminal connection. This signal also causes some programs to re-initialize themselves without terminating. exit
2 SIGINT Interrupt An interrupt signal you generate from your keyboard, usually using Control-C. exit
9 SIGKILL Kill A signal that kills a process. A process cannot ignore this signal. exit
15 SIGTERM Terminate A signal that terminates a process in an orderly manner. Some processes ignore this signal. This is the default signal that kill and pkill send. exit

Terminating by Using the kill Command

You can use the kill command to send a signal to one or more processes, The kill command terminates only those processes that you own. The root user can use the kill command on any process. The kill command sends signal 15, the terminate signal, by default. This signal causes the process to terminate in an orderly manner. The syntax for the kill command is:

$ kill [ signal ] PIDs

Before you can terminate a process using the kill command, you must know the PID to specify. Use either the ps command or the pgrep command to locate the PID for the process you want to terminate. You can terminate several processes at the same time by entering multiple PIDs on one command line. The following example shows how to use the kill command to terminate the dtmail process:

$ pgrep -l mail
215 sendmail
12047 dtmail
$ kill 12047
$ pgrep -l mail
215 sendmail

Some processes ignore the default signal that the kill command sends. For example, a process waiting for a tape drive to complete an operation might ignore signal 15. If a process does not respond to signal 15, you can force it to terminate by using signal 9 with the kill command. To terminate a process using signal 9, perform the command:

$ kill -9 PID

Terminating by Using the pkill Command

You use the pkill command to send signals to processes. The pkill command sends signal 15, the terminate signal, by default. The pkill command allows you to use process names to identify the process you want to terminate. The syntax for the pkill command is:

$ pkill [options ] pattern

The options for the pkill command are similar to those for the pgrep command. The following example shows how to terminate the mail session process by using the dtmail command name:

$ pgrep -l mail
215 sendmail
470 dtmail
$ pkill dtmail
$ pgrep -l mail
215 sendmail
$

You can force processes that do not respond to signal 15 to terminate by using signal 9 with the pkill command. The following example shows how to terminate a process using signal 9:

$ pkill -9 -x process_name