Linux - Process
Table of Contents
1 - About
OS - Process (Main Thread) / Program in linux
2 - Articles Related
3 - Management
3.1 - PID
How to get the PID (process identification number) of a process (
# 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
}
3.2 - Kill
3.3 - Priority
Priority gives the process more or less CPU time than other processes.
See: Nice_(Unix)
3.4 - Information
3.4.1 - variable
3.4.1.1 - $$
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
3.4.1.2 - BASHPID
BASHPID: Process ID of the current instance of Bash. <warning>bash 4</note>
echo $BASHPID
3.4.1.3 - $PPID
The $PPID of a process is the process ID (pid) of its parent process.
3.4.1.4 - $!
The PID (process ID) of last job run in background
3.4.2 - 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-)
3.5 - List process
3.5.1 - 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
3.5.2 - 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
3.6 - Get
3.6.1 - 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
3.6.2 - 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
3.7 - Monitoring
3.7.1 - 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"
3.8 - file.PID
Two methods:
- the file.PID method. Check if the file exist in the directory /home/lsc/
3.9 - Working Directory
pwdx $PID
3.10 - Status
- by port
lsof -i port