Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Obtaining the Process ID by Name

One of the difficulties with the ps command is that it is hard to obtain the process ID of a command by specifying only its name. In shell scripts that have to start and stop processes, the capability to look through the output of ps and retrieve a list of process IDs based on a command’s name is essential.

In this section, I will present a function that displays a list of process IDs (pids) based on a string supplied by the user.

################################################
# Name: getPID
# Desc: outputs a list of process id matching $1
# Args: $1 -> the command name to look for
################################################

getPID() {

    if [ $# -lt 1 ] ; then
        printERROR "Insufficient Arguments."
        return 1
    fi

    PSOPTS="-ef"

    /bin/ps $PSOPTS | grep "$1" | grep -v grep | awk `{ print $2; }'
}

As you can see, this function is a set of filters on top of the command /bin/ps -ef. The first grep command looks for all lines that match the first argument. As an example, executing this on the command line produces output similar to the following:

$ /bin/ps -ef | grep sshd

Here you are looking for all the lines that contain the word sshd. The output should look similar to the following:

root  1449     1  8 12:23:06 ?         0:02 /opt/bin/sshd
ranga  1451   944  5 12:23:08 pts/t0    0:00 grep sshd

As you can see, the output contains two lines. The first one contains the process ID of the commands that you are looking for, but the second contains the process ID of the grep command that just executed. In order to get rid of such lines, add the grep -v grep to the pipeline.

Because the process ID is stored in the second column, use awk to extract it.

If more than one line matches, this function displays each process ID.

For example, the following command

getPID httpd

returns the following list of process IDs on my system:

330
331
332
333
334
335
336
323

Readers who are using Linux or FreeBSD-based systems have to change this function slightly for it to function properly. The value of the variable PSOPTS should be -auwx on these systems. In Chapter 23, I will show you how to incorporate these changes into the function so that it runs on all versions of UNIX.

Getting a User’s Numeric User ID

Some shell scripts need to determine whether a user has sufficient permissions to execute commands. For example, an install script might need to run as root (UID 0) to modify system files correctly. In other instances, a script might need to detect whether a user has too many privileges.

To check the user’s ID, you can use the id command, which can be run in two forms. The first form specifies a username whose ID should be returned. For example, the command

$ id vathsa
uid=501(vathsa) gid=100(users) groups=100(users)

returns the UID for the user vathsa. The second form omits the user ID. In this form, the current user’s information is returned.

$ id
uid=500(ranga) gid=100(users) groups=100(users),101(ftpadmin)

Your function supports both.

################################################
# Name: getUID
# Desc: outputs a numeric user id
# Args: $1 -> a user name (optional)
################################################

getUID() {
    id $1 | sed -e 's/(.*$//' -e 's/^uid=//'
}

This function executes the id command and then uses a sed filter to delete all the unimportant information. When the function is called by itself

getUID

the output looks like the following:

500

When the function is called with a username

getUID vathsa

the output looks like the following:

500

Usually you need to compare this output to some known UID as follows:

if [ "`getUID'" -gt 100 ] ; then
    printERROR "You do not have sufficient privileges."
    exit 1
fi

Here the output of the getUID function is checked to see whether it is greater than 100.


Previous Table of Contents Next