- How to find the Process ID (PID) of a running terminal program?
- 11 Answers 11
- How to Find the PID and PPID of a Process in Linux
- Linux process basics
- What is a PID?
- What is a PPID?
- Getting the PID of a process
- Getting PPID from a child process’s PID
- Checking the PID and PPID of the currently running process
- Wrapping up
- How to see detailed information about a given PID?
- 3 Answers 3
How to find the Process ID (PID) of a running terminal program?
I am running a program in the terminal that I can’t escape with Ctrl — C and that I want to kill. How can I find its PID?
This is a branch of What should I do when Ubuntu freezes? as a reference to prevent details in that question from becoming too technical.
11 Answers 11
Open another terminal and run ps ax | grep foo where foo is the name of the unresponsive program. This should return a line of output that looks something like this:
$ ps ax | grep firefox 2222 ? S 0:00 /bin/sh /usr/lib/firefox-3.6.9/firefox 2231 ? Sl 514:36 /usr/lib/firefox-3.6.9/firefox-bin 30290 pts/2 S+ 0:00 grep --color=auto firefox
The first field of each line of output is a number which represents the Process ID of the program matched by grep (you can safely ignore the last one, which represents grep itself.
To halt the offending process, do: kill pid where pid is the Process ID of the program. You might have to use your judgment as to which of the matches needs to be kill ed, or you could use top instead. Using kill by itself sends SIGTERM, which you should try first as it allows the program to properly clean up after itself. If SIGTERM fails, try SIGHUP, which is stonger medicine: kill -HUP pid . If all else fails, send SIGKILL. But, you should only do so as a last resort, because SIGKILL causes the kernel to terminate the process immediately with no possibility for cleanup. This can at times result in data corruption or other problems. So again, only send SIGKILL as a last resort. To do so, do kill -KILL pid or kill -9 pid .
If you are running a graphical interface, of course, you don’t have to fool with this crazy command-line stuff to get the job done. Just open «System Monitor», navigate to the Processes tab, choose the process you want to halt (Hm, could it be the one using 90% CPU?) and right-click it. Since the process is already stopped, (that’s the problem, right?) choose End Process or Kill Process from the resulting menu.
How to Find the PID and PPID of a Process in Linux
Knowing the PID and PPID of a process can be helpful if you need to manage or interact with a process running on your system.
There are numerous ways to get the PID (Process ID) and PPID (Parent Process ID) of a given process in Linux.
Command | Description |
---|---|
pidof process_name | Works with exact process name |
pgrep process_name | Returns PID of all matches |
ps -o ppid= -p PID | Get the PPID from PID |
$$ | PID of current process/shell |
$ | PID of current process’s parent |
I’ll explain these commands in detail but before that a quick recap of process, PID and PPID.
Linux process basics
Everything that runs on your system is ran via something known as a process, with that simply being the running instance of a program.
All the processes that run on your system are assigned identifiers. These can be helpful if you want to monitor the process (for example, such as to see how much memory or CPU it is using), or maybe if you want to end it if it starts to hang or just act a bit funky.
The identifiers that get attached to all these processes are known as PIDs and PPIDs.
What is a PID?
PID stands for «process ID». Again, this is simply the identifier that gets attached to a program when it starts running, and can be helpful if you need to interact with the process in one way or another.
What is a PPID?
PPID is quite closely related to a PID. PPID stands for «parent process ID», and if you didn’t get it already, it simply stands for the process that created the process you are checking.
For example, let’s say that we have two processes. One is named «spawner», and has a process ID (or PID) of 7234. Our second process, «email client», has a process ID of 7456 when we create it. Our spawner program starts our email client, resulting in our email client having a PID of 7456, and a PPID of 7234, since the spawner (which had the PID of 7234) is what spawned the email client.
Now that you have brushed up your basic, let’s see how to get the process ID in Linux.
Getting the PID of a process
The important thing here is to know the name of the process whose PID you want to find.
If you know the exact process name, you can get its process ID using the pidof command:
Easier said than done because you may not always know the exact process name. Good thing here is that pidof command works with tab completion so if you know the starting few letters of the process name, you can hit tab to get matching suggestions.
However, that may not always work if the process name doesn’t match to what you think it is called. For example, the process for Edge browser on Linux is called msedge . It doesn’t start with ‘edge’ and the tab completion won’t work if you focus on ‘edge’.
So, what you can do is to resort to the ps command in Linux to list all the running processes from all users and then use grep on the output to filter the result.
ps aux | grep -i partial_process_name
There is a dedicated command that combines the features ps and grep command and it is unsurprisingly called pgrep:
pgrep partial_or_exact_process_name
The default output shows only the PIDs without any information on the process. This could be troublesome if there is more than one process IDs returned for your searched term.
Hence, I suggest using the listing feature to make sure that you are getting the PID of the desired process.
pgrep -l partial_or_exact_process_name
You may also use the top command to get the process information but it cannot be used in scripts.
You can use the pstree command to get the PIDs of all running process on your Linux system: pstree -p -a
Getting PPID from a child process’s PID
Once you know the PID of a process, it is effortless to find the PPID for that process.
You can simply run the following command, replacing PID with the current process (child) ID:
In a shell, the above command and $ should both return the same output:
And that’s about everything there is to finding PIDs and PPIDs!
Checking the PID and PPID of the currently running process
If you’re in a shell such as Bash, it’s extremely easy to find the PID and PPID of the calling process (which will usually be the shell).
Bash stores the PID’s value under the $$ variable, and the PPID under the $ variable:
And it’s that easy! Finding the PIDs and PPIDs of other processes isn’t much harder either.
Wrapping up
You should now know everything you need to find both PIDs and PPIDs for running processes on your system.
If you need any help getting something working, or just got some remaining questions, feel free to leave that and anything else in the comments below.
How to see detailed information about a given PID?
The headline basically says it all. I have a program where I am only given the PID, nothing more really, and I would like to know how I get more information about the given process.
If you are working in python you probably want to use the psutil library. Do: psutil.Process(pid) to obtain the process object and then use its interface to retrieve information about memory/cpu etc. etc.
3 Answers 3
will show you some information. See the ps manpage for more information about the ps command. The «STANDARD FORMAT SPECIFIERS» section explains what the different columns mean.
I don’t know what is your exact requirement. but this may help you.
There is separate directory for every process with name as pid number in /proc .
ps -ef | grep docker root 1700 1 0 Sep20 ? 00:03:04 /usr/bin/docker daemon --raw-logs
In above output PID is 1700 .
ls attr clear_refs cpuset fd limits mem net oom_score projid_map sessionid stat task autogroup cmdline cwd fdinfo loginuid mountinfo ns oom_score_adj root setgroups statm timers auxv comm environ gid_map map_files mounts numa_maps pagemap sched smaps status uid_map cgroup coredump_filter exe io maps mountstats oom_adj personality schedstat stack syscall wchan
there is many file that have all information about process.
cat /proc/1700/status Name: docker State: S (sleeping) Tgid: 1700 Ngid: 0 Pid: 1700 PPid: 1 TracerPid: 0 Uid: 0 0 0 0 Gid: 0 0 0 0 FDSize: 64 Groups: 0 999 VmPeak: 527576 kB VmSize: 527512 kB VmLck: 0 kB VmPin: 0 kB VmHWM: 46032 kB VmRSS: 34180 kB VmData: 449308 kB VmStk: 136 kB VmExe: 28324 kB VmLib: 4236 kB VmPTE: 296 kB VmSwap: 5324 kB Threads: 12 SigQ: 0/63662 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000000000000 SigCgt: ffffffffffc1feff CapInh: 0000000000000000 CapPrm: 0000003fffffffff CapEff: 0000003fffffffff CapBnd: 0000003fffffffff Seccomp: 0 Cpus_allowed: f Cpus_allowed_list: 0-3 Mems_allowed: 00000000,00000001 Mems_allowed_list: 0 voluntary_ctxt_switches: 437726 nonvoluntary_ctxt_switches: 27579
If you need basic command to get process information then you can easily get using command: