- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether a process with a given PID is running
Introduction
As a system administrator or developer, it's important to be able to monitor and manage processes running on your system. One of key pieces of information you need to know is whether a particular process is currently running. In this article, we'll explore different ways to check whether a process with a given PID is running.
What is a PID?
Before we dive into ways to check if a process with a given PID is running, let's first define what a PID is. A PID (Process Identifier) is a unique identification number assigned to each process running on a system. This number is used by operating system to keep track of and manage processes.
Using ps Command
One of easiest ways to check whether a process with a given PID is running is to use ps command. ps command is used to display information about currently running processes on a system.
To check whether a process with a given PID is running using ps command, run following command −
ps -p <PID>
Replace <PID> with PID of process you want to check.
If process is running, you will see output similar to following −
PID TTY TIME CMD 1234 pts/0 00:00:00 bash
The output shows process ID (PID), terminal it's running on (TTY), amount of CPU time it has used (TIME), and command that started process (CMD).
If process is not running, you will not see any output.
Using pgrep Command
Another way to check whether a process with a given PID is running is to use pgrep command. pgrep command is used to search for processes based on their name or other attributes.
To check whether a process with a given PID is running using pgrep command, run following command −
pgrep <PID>
Replace <PID> with PID of process you want to check.
If process is running, you will see PID printed to console.
If process is not running, you will not see any output.
Using kill Command
The kill command is used to send a signal to a process. One of signals that can be sent is 0 signal, which does not actually kill process, but instead checks whether it's running.
To check whether a process with a given PID is running using kill command, run following command −
kill -0 <PID>
Replace <PID> with PID of process you want to check.
If process is running, you will not see any output.
If process is not running, you will see an error message similar to following −
kill: (1234) - No such process
Using pidof Command
The pidof command is similar to pgrep command, but it only returns PID of process.
To check whether a process with a given PID is running using pidof command, run following command −
pidof <PID>
Replace <PID> with PID of process you want to check.
If process is running, you will see PID printed to console.
If process is not running, you will not see any output.
Here are some additional tips and tricks for checking whether a process with a given PID is running −
Use htop command for a more interactive and user-friendly process viewer. htop allows you to sort and filter processes, and provides more detailed information about CPU usage, memory usage, and other metrics.
If you want to check whether a process is running in a script, you can use ps command with q option to suppress output, and then check exit code of command. For example −
ps -p <PID> q >/dev/null if [ $? -eq 0 ]; then echo "Process is running" else echo "Process is not running" fi
You can use lsof command to check whether a process has open files or network connections. For example, to check whether process with PID 1234 has any open files, run following command −
lsof -p 1234
You can use systemctl command to check whether a systemd service is running. For example, to check whether Apache web server service is running, run following command −
systemctl status apache2
If you want to check whether a process is running on a remote system, you can use ssh command to run a command on remote system. For example, to check whether process with PID 1234 is running on a remote system with IP address 192.168.1.100, run following command −
ssh 192.168.1.100 ps -p 1234
These are just a few examples of many tools and commands available for checking whether a process with a given PID is running. By combining these tools with your own scripts and workflows, you can effectively monitor and manage processes running on your system.
Conclusion
In this article, we explored different ways to check whether a process with a given PID is running. We learned that we can use ps command to display information about currently running processes, pgrep command to search for processes based on their name or other attributes, kill command to send a signal to a process and check whether it's running, and pidof command to return PID of process.
Knowing how to check whether a process with a given PID is running is an essential skill for system administrators and developers. It can help in troubleshooting issues, monitoring system performance, and managing resources.