I want to view a status of a process among 2000 processes, Without using top
and ps
commands.
The name of the process is tom.
I want to view a status of a process among 2000 processes, Without using top
and ps
commands.
The name of the process is tom.
A process don't have any name (only the program it is running has one, but see also pthread_setname_np(3)... you might have pathological cases like this). It has a pid (which is some integer number, like 1234, of type pid_t
). See credentials(7) and fork(2) and execve(2). Use pidof(1) and pgrep(1) to find the pid of some process. An executable program (e.g. /bin/bash
) can be run by several processes (or none, or only one).
You may use kill(2) with a zero signal number to check that the process exists.
Most importantly, you should consider using /proc/
(see proc(5) for more). For the process of pid 1234, see /proc/1234/
which has several files and subdirectories (notably /proc/1234/status
and /proc/1234/maps
). Try cat /proc/$$/status
and cat /proc/$$/maps
and stat /proc/$$/exe
and ls -l /proc/$$/
in a terminal (then replace $$
by whatever pid is interesting for you).
The top
and ps
utilities (and also pidof
, pgrep
, ...) are using that /proc/
(which is the mean by which the Linux kernel shows information on processes, and on the system itself). And you can write your program (or script) doing that too and using /proc/
. See also this.
From inside a program, you can explore /proc/
like you would explore other file trees, e.g. using stat(2), opendir(3), readdir(3), nftw(3) etc.