- Linux/Unix command to determine if process is running?
- 15 Answers 15
- You SHOULD know the PID !
- How to determine if process is running (by pid)
- Linux List Processes – How to Check Running Processes
- Prerequisites
- A Quick Introduction to Linux Processes
- How to List Running Processes in Linux using the ps Command
- How to List Running Processes in Linux using the top and htop Commands
- How to Kill Running Processes in Linux
- Conclusion
Linux/Unix command to determine if process is running?
I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld , httpd . What is the simplest way/command to do this?
15 Answers 15
While pidof and pgrep are great tools for determining what’s running, they are both, unfortunately, unavailable on some operating systems. A definite fail safe would be to use the following: ps cax | grep command
The output on Gentoo Linux:
14484 ? S 0:00 apache2 14667 ? S 0:00 apache2 19620 ? Sl 0:00 apache2 21132 ? Ss 0:04 apache2
The output on OS X:
42582 ?? Z 0:00.00 (smbclient) 46529 ?? Z 0:00.00 (smbclient) 46539 ?? Z 0:00.00 (smbclient) 46547 ?? Z 0:00.00 (smbclient) 46586 ?? Z 0:00.00 (smbclient) 46594 ?? Z 0:00.00 (smbclient)
On both Linux and OS X, grep returns an exit code so it’s easy to check if the process was found or not:
#!/bin/bash ps cax | grep httpd > /dev/null if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi
Furthermore, if you would like the list of PIDs, you could easily grep for those as well:
ps cax | grep httpd | grep -o '^[ ]*4*'
Whose output is the same on Linux and OS X:
The output of the following is an empty string, making this approach safe for processes that are not running:
echo ps cax | grep aasdfasdf | grep -o '^[ ]*4*'
This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.
#!/bin/bash PROCESS=$1 PIDS=`ps cax | grep $PROCESS | grep -o '^[ ]*9*'` if [ -z "$PIDS" ]; then echo "Process not running." 1>&2 exit 1 else for PID in $PIDS; do echo $PID done fi
You can test it by saving it to a file (named «running») with execute permissions (chmod +x running) and executing it with a parameter: ./running «httpd»
#!/bin/bash ps cax | grep httpd if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi
Please keep in mind that you’re simply parsing the output of ps ax which means that, as seen in the Linux output, it is not simply matching on processes, but also the arguments passed to that program. I highly recommend being as specific as possible when using this method (e.g. ./running «mysql» will also match ‘mysqld’ processes). I highly recommend using which to check against a full path where possible.
The process can be running, but stopped. So if the goal is to test if mysqld or httpd are «up and running» (responding), you should also check if it is stopped or not.
Sorry, but while the answer is certainly right from a semantic point of view I’m fully against trying to find a process by pattern matching on the process arg vector. Any such approach is doomed to fail sooner or later (you actually admit to that yourself, by saying that more checks are needed). I’ve added my own recommendation in a separate answer.
grep will also find itself running (e.g. ps cax | grep randomname will always return 0 because grep finds grep randomname (hope this is clear. ). One fix is to add square brackets around the first letter of the process name, e.g. ps cax | grep [r]andomname .
ps cax may not output command name wholly. E.g it prints «chromium-browse» instead of «chromium-browser».
You SHOULD know the PID !
Finding a process by trying to do some kind of pattern recognition on the process arguments (like pgrep «mysqld» ) is a strategy that is doomed to fail sooner or later. What if you have two mysqld running? Forget that approach. You MAY get it right temporarily and it MAY work for a year or two but then something happens that you haven’t thought about.
Only the process id (pid) is truly unique.
Always store the pid when you launch something in the background. In Bash this can be done with the $! Bash variable. You will save yourself SO much trouble by doing so.
How to determine if process is running (by pid)
So now the question becomes how to know if a pid is running.
This is POSIX and hence portable. It will return the pid itself if the process is running or return nothing if the process is not running. Strictly speaking the command will return a single column, the pid , but since we’ve given that an empty title header (the stuff immediately preceding the equals sign) and this is the only column requested then the ps command will not use header at all. Which is what we want because it makes parsing easier.
This will work on Linux, BSD, Solaris, etc.
Another strategy would be to test on the exit value from the above ps command. It should be zero if the process is running and non-zero if it isn’t. The POSIX spec says that ps must exit >0 if an error has occurred but it is unclear to me what constitutes ‘an error’. Therefore I’m not personally using that strategy although I’m pretty sure it will work as well on all Unix/Linux platforms.
Linux List Processes – How to Check Running Processes
Bolaji Ayodeji
Every day, developers use various applications and run commands in the terminal. These applications can include a browser, code editor, terminal, video conferencing app, or music player.
For each of these software applications that you open or commands you run, it creates a process or task.
One beautiful feature of the Linux operating system and of modern computers in general is that they provide support for multitasking. So multiple programs can run at the same time.
Have you ever wondered how you can check all the programs running on your machine? Then this article is for you, as I’ll show you how to list, manage, and kill all the running processes on your Linux machine.
Prerequisites
- A Linux distro installed.
- Basic knowledge of navigating around the command-line.
- A smile on your face 🙂
A Quick Introduction to Linux Processes
A process is an instance of a running computer program that you can find in a software application or command.
For example, if you open your Visual Studio Code editor, that creates a process which will only stop (or die) once you terminate or close the Visual Studio Code application.
Likewise, when you run a command in the terminal (like curl ifconfig.me ), it creates a process that will only stop when the command finishes executing or is terminated.
How to List Running Processes in Linux using the ps Command
You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.
To test this, just open your terminal and run the ps command like so:
This will display the process for the current shell with four columns:
- PID returns the unique process ID
- TTY returns the terminal type you’re logged into
- TIME returns the total amount of CPU usage
- CMD returns the name of the command that launched the process.
You can choose to display a certain set of processes by using any combination of options (like -A -a , -C , -c , -d , -E , -e , -u , -X , -x , and others).
If you specify more than one of these options, then all processes which are matched by at least one of the given options will be displayed.
Type man ps in your terminal to read the manual for the ps command, which has a complete reference for all options and their uses.
To display all running processes for all users on your machine, including their usernames, and to show processes not attached to your terminal, you can use the command below:
Here’s a breakdown of the command:
- ps : is the process status command.
- a : displays information about other users’ processes as well as your own.
- u : displays the processes belonging to the specified usernames.
- x : includes processes that do not have a controlling terminal.
This will display the process for the current shell with eleven columns:
- USER returns the username of the user running the process
- PID returns the unique process ID
- %CPU returns the percentage of CPU usage
- %MEM returns the percentage memory usage
- VSV returns the virtual size in Kbytes
- RSS returns the resident set size
- TT returns the control terminal name
- STAT returns the symbolic process state
- STARTED returns the time started
- CMD returns the command that launched the process.
How to List Running Processes in Linux using the top and htop Commands
You can also use the top task manager command in Linux to see a real-time sorted list of top processes that use the most memory or CPU.
Type top in your terminal and you’ll get a result like the one you see in the screenshot below:
An alternative to top is htop which provides an interactive system-monitor to view and manage processes. It also displays a real-time sorted list of processes based on their CPU usage, and you can easily search, filter, and kill running processes.
htop is not installed on Linux by default, so you need to install it using the command below or download the binaries for your preferred Linux distro.
sudo apt update && sudo apt install htop
Just type htop in your terminal and you’ll get a result like the one you see in the screenshot below:
How to Kill Running Processes in Linux
Killing a process means that you terminate a running application or command. You can kill a process by running the kill command with the process ID or the pkill command with the process name like so:
To find the process ID of a running process, you can use the pgrep command followed by the name of the process like so:
To kill the iTerm2 process in the screenshot above, we will use any of the commands below. This will automatically terminate and close the iTerm2 process (application).
Conclusion
When you list running processes, it is usually a long and clustered list. You can pipe it through less to display the command output one page at a time in your terminal like so:
or display only a specific process that matches a particular name like so:
I hope that you now understand what Linux processes are and how to manage them using the ps , top , and htop commands.
Make sure to check out the manual for each command by running man ps , man top , or man htop respectively. The manual includes a comprehensive reference you can check if you need any more help at any point.
Thanks for reading – cheers! 💙