Table of Contents

About

OS - Process (Main Thread) / Program in linux

Management

Command

With the ps utility

# for all process
ps -eo args
# for one process
ps -p [PID] -o args

Name

ps axco pid,command

PID

How to get the PID (process identification number) of a process (

The PID of the shell can be get with $$.

# ps aux'' includes the full command line (path and parameters), 
ps aux | grep pattern
  • pgrep
# pgrep by default only looks at the first 15 characters of the executable's names
# That's why you need the -f option
pgrep -f pattern

Function example

get_pid ()
{
    if [ "$ANA_VARIANT" = "AIX" ]; then
    	echo `${bin_ps} -u${ana_username} | ${bin_grep} $1 | ${bin_awk} '{print $2}'`
    else
    	echo `${bin_ps} -u${ana_username} | ${bin_grep} $1 | ${bin_awk} '{print $1}'`
    fi
}

Kill

Bash - Kill a process

Priority

Priority gives the process more or less CPU time than other processes.

See: wiki/Nice_(Unix)

Information

All utilities obtains processes information via the Filesystem (procfs)

variable

$$

From the bash man page: $ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the sub-shell.

echo $$

The $$ variable often finds use in scripts to construct “unique” temp file names

BASHPID

BASHPID: Process ID of the current instance of Bash.

bash 4
echo $BASHPID
PPID

The PPID of a process is the process ID (pid) of its parent process.

$!

The PID (process ID) of last job run in background

Environment variable

cat /proc/{pid}/environ | tr \\0 \\n
# tr \\0 \\n because you want get rid of the null characters (\0)

Example for a process named nqsserver:

cat /proc/$(pidof nqsserver)/environ | tr \\0 \\n

How to copy an environment variable of a process to the shell dynamically

export LD_LIBRARY_PATH=$(cat /proc/$(pidof nqsserver)/environ | tr \\0 \\n | grep LD_LIBRARY_PATH= | cut -c17-)

List process

Using its name (pidof)

pidof: Pidof finds the process id’s (pids) of the named programs.

pidof nqsserver
23686

  • Java
pidof java
26986 26856 26284 24546 6358

Using a port

sudo netstat -nlp | grep :9703
tcp        0      0 0.0.0.0:9703                0.0.0.0:*                   LISTEN      23066/nqsserver

The PID is 23066

Get

Process info by PID

ps -f --pid $PID
ps -f -p $PID
ps -p $PID -o pid,vsz=MEMORY -o user,group=GROUP -o comm,args=ARGS

Example:

ps -f --pid 23066
ps -f -p 23066
UID        PID  PPID  C STIME TTY          TIME CMD
oracle   23066 22951  0 Feb15 ?        00:07:33 nqsserver -quiet

Open file

File System - Open File

  • by port
lsof -i port
  • by process id
lsof -p $(pgrep nqsserver) | awk '{print $9}' 
# for 3 process
lsof -p 456,123,789 
  • by user
lsof -u 1234,userName

Monitoring

What are the Metrics / Counters of Linux ?

Loop until process exit

With the while bash statement (Replace with your pid)

pid=4277
count=0
while kill -0 $pid 2> /dev/null; do
    count=$(( $count + 1 ))
    echo "${count} - Process is running"
    sleep 10
done
echo "${count} - Process has exited"

file.PID

Two methods:

  • the file.PID method. Check if the file exist in the directory /home/lsc/

Working Directory

pwdx $PID

Status

  • by port
lsof -i port