How to Know If a Process Is Running

How can I know if a process is running?

This is a way to do it with the name:

Process[] pname = Process.GetProcessesByName("notepad");
if (pname.Length == 0)
MessageBox.Show("nothing");
else
MessageBox.Show("run");

You can loop all process to get the ID for later manipulation:

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}

Linux/Unix command to determine if process is running?

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 '^[ ]*[0-9]*'

Whose output is the same on Linux and OS X:

3519 3521 3523 3524

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 '^[ ]*[0-9]*'

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 '^[ ]*[0-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

WARNING!!!

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.


References:

http://linux.about.com/od/commands/l/blcmdl1_ps.htm

http://linux.about.com/od/commands/l/blcmdl1_grep.htm

How to check if a process is running on Windows?

There is no direct way to query general processes as each OS handles them differently.

You kinda stuck with using proxies such as direct OS commands...

You can however, find a specific process using tasklist.exe with /fi parameter.

e.g: tasklist.exe /nh /fi "Imagename eq chrome.exe"

Note the mandatory double quotes.

Syntax & usage are available on MS Technet site.

Same example, filtering for "chrome.exe" in Java:

String findProcess = "chrome.exe";
String filenameFilter = "/nh /fi \"Imagename eq "+findProcess+"\"";
String tasksCmd = System.getenv("windir") +"/system32/tasklist.exe "+filenameFilter;

Process p = Runtime.getRuntime().exec(tasksCmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

ArrayList<String> procs = new ArrayList<String>();
String line = null;
while ((line = input.readLine()) != null)
procs.add(line);

input.close();

Boolean processFound = procs.stream().filter(row -> row.indexOf(findProcess) > -1).count() > 0;
// Head-up! If no processes were found - we still get:
// "INFO: No tasks are running which match the specified criteria."

Checking if a Process is Currently running by name in rust

You can use sysinfo crate, or particularly processes_by_name

You can get iterator to processes containing the name using the function

fn processes_by_name<'a>(&'a self, name: &'a str) -> Box<dyn Iterator<Item = &'a Process> + 'a>

You can use it like this

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new_all();
for process in s.processes_by_name("htop") {
//check here if this is your process
}

UPDATE: New version (0.23.0) also contains processes_by_exact_name

It returns an iterator to processes with the exact given name
You can use it like this

use sysinfo::{ProcessExt, System, SystemExt};

let s = System::new_all();
for process in s.processes_by_exact_name("htop") {
//Your code goes here
}

How to check if a Process is Running or Not

From the docs for QProcess ...

There are at least 3 ways to check if a QProcess instance is running.

QProcess.pid() : If its running, the pid will be > 0

QProcess.state() : Check it again the ProcessState enum to see if its QProcess::NotRunning

QProcess.atEnd() : Its not running if this is true

If any of these are not working as you would expect, then you will need to post a specific case of that example.

How to know if a process is running in Windows in C++, WinAPI?

Use the CreateToolhelp32Snapshot function to create a snapshot of the current process table, then use the Process32First and Process32Next functions to iterate the snapshot. You can get the name for each executable file by looking at the szExeName field of the PROCESSENTRY32 structure.

See the MSDN example for a sample of how to use these functions.

The advantage of this approach is that, unlike any EnumProcesses-based solution, it doesn't suffer from race conditions: with EnumProcesses it can happen that a process gets destroyed after you finished enumerating the processes but before you got around to opening the process (or reading our the process executable name).

check if process is running with only Path provided

You can filter by Path and select Id:

Get-Process | Where-Object {$_.Path -EQ "C:\Users\Administrator\Desktop\1\hello.exe"} | Select-Object Id

By the way:

Get-Process sadly does not even shows you the path to the process.

It does, if you ask it to (it returns an object like all those commands do, and it has a default selection of properties to list but that doesn't mean you can't access the remaining properties by specifically selecting them):

Get-Process explorer | Select-Object Path

(or use Select-Object * to see all available properties)

It appears you haven't yet understood how PowerShell objects work, so I'd recommend you to check out some tutorials about this topic, for instance this article about selecting and filtering and this one that goes more into detail about the filtering options.



Related Topics



Leave a reply



Submit