How to save process id number

From Wiki-UX.info
Jump to: navigation, search

Abstract

The following document demostrates how to capture the process id (pid) number of an application on it's startup script.

The "fakeproc" application

This script simulates an system application. It basically keeps a date command sending the system date to /dev/null and sleeping for 30 seconds. Current system aplication will probably do something less trivial!

while true
do
date > /dev/null
sleep 30
done

The start script

This "start" up script captures the "fakeproc" pid number using POSIX shell $! variable into the fakeproc.pid file every time that is executed.

# Starts the fakeproc process
PID="-1"
$(pwd)/fakeproc &
PID=$!
echo "fakeproc pid $PID" > $(pwd)/fakeproc.pid

Kill "fakeproc" process

To kill "fakeproc" send a TERM or KILL signal using the captured pid number.

# kill $(awk '{print $3}' fakeproc.pid)

or

# kill -9 $(awk '{print $3}' fakeproc.pid)

Reference

Authors