Linux - Process

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





Discover More
Bash Liste Des Attaques Ovh
Bash - While

This article is dedicated to the while syntax in Bash. where: : is the no-op command; its exit status is always 0 The command: lists the file with the ls executable (ls -l) pipe the...
Bash Liste Des Attaques Ovh
Bash - process

This article is the bash Process. To see how to manage another general linux process, see The process running the command is created. The process inherits the stdin, stdout, and stderr from...
Bash Liste Des Attaques Ovh
How to Zip / Unzip on Linux

The zip manager in Linux Compression: take advantage of redundancy between files. split archives: storing a large archive on multiple removable media. split big file ? List the content...
How to manage Environment variables in Linux ?

This page is Process Environment variable in Linux. They are set generally with the shell. See pam permits also an admin to set environment variable: See pam-env...
Linux - (Proc|Process) Filesystem (procfs)

procfs (or the proc filesystem) is a special filesystem in UNIX-like operating systems that presents performance metrics linux processes and other system information in a hierarchical file-like structure...
Linux - Cgroups (Control Groups)

With cgroups, it's possible to allocate resources dynamically to process Installation (On CentOS) lscgroup list all cgroups Example: list all cgroups which are in hierarchy containing...
Process States
OS - Process (Main Thread) / Program

OS A process is the first thread started (called the main thread). It's the only thread that is authorized to start a new threads. A process is a unit of resources, while a thread is a unit of: scheduling...
What is a Process Manager? (aka supervisor)

process manager are applications that starts and manage processes. They are mostly used as VM entrypoint to start multiple processes. init systems are process manager. overmind...



Share this page:
Follow us:
Task Runner