Tasklist.Exe Equivalent in Linux

Tasklist.exe equivalent in linux

ps -ef will get you all of the tasks running

man ps will get you all of the options

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class psef
{

public static void main(String args[]) throws Exception
{

try
{
String line;
Process p = Runtime.getRuntime().exec("ps -ef");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
input.close();
}
catch (Exception err)
{
err.printStackTrace();
}
}
}

Linux equivalent of taskkill

kill -9 pid_of_process

or

killall -KILL myStupidProcess

How can I determine whether a specific file is open in Windows?

Use Process Explorer from the Sysinternals Suite, the Find Handle or DLL function will let you search for the process with that file open.

Most efficient way to scan the windows process list?

If you go with tasklist, it might actually be faster to just run the command once and get back all of the results, rather than running it for each of your executable names. There is some overhead for exec'ing a process, and getting the output. (When you get back the results, you'll have to loop through them in code, but this might be faster. Normally there won't be more than 100 processes running at once, so it won't be too bad.) You should write a test and check to see if that's true.

In C#, Process.GetProcesses() is the best way to go.

In Java, there isn't really an equivalent class/method. Getting a process list is pretty OS-specific, so the Java designers must have decided not to integrate/abstract this capability into the base Java classes. You'll probably have to Runtime.getRuntime().exec("tasklist.exe") to get the results on Windows, or exec("ps") on Unix/Linux.

What is the Windows equivalent of pidof from Linux?

You can use Toolhelp APIs to enumerate processes, get their full path and compare it with the required process name. You need to walk the modules list for each process. The first module in the list is the process executable itself. Here's a sample code:

int main( int argc, char* argv[] )
{

if( argc > 1 )
{
printf( "\nGetting PID of: %s\n", argv[1] );
HANDLE hProcSnapshot = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( INVALID_HANDLE_VALUE != hProcSnapshot )
{
PROCESSENTRY32 procEntry = {0};
procEntry.dwSize = sizeof(PROCESSENTRY32);
if( ::Process32First( hProcSnapshot, &procEntry ) )
{
do
{
HANDLE hModSnapshot = ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, procEntry.th32ProcessID );
if( INVALID_HANDLE_VALUE != hModSnapshot )
{
MODULEENTRY32 modEntry = {0};
modEntry.dwSize = sizeof( MODULEENTRY32 );
if( Module32First( hModSnapshot, &modEntry ) )
{
if( 0 == stricmp( argv[1], modEntry.szExePath ) )
{
printf( "\nPID: %ld\n", procEntry.th32ProcessID );
::CloseHandle( hModSnapshot );
break;
}
}
::CloseHandle( hModSnapshot );
}
}
while( ::Process32Next( hProcSnapshot, &procEntry ) );
}
::CloseHandle( hProcSnapshot );
}
}
return 0;
}

How do you list all processes on the command line in Windows?

Working with cmd.exe:

tasklist

If you have Powershell:

get-process

Via WMI:

wmic process

(you can query remote machines as well with /node:ComputerOrIP, and there are a LOT more ways to customize this command: link)

How to get a list of current open windows/process with Java?

Finally, with Java 9+ it is possible with ProcessHandle:

public static void main(String[] args) {
ProcessHandle.allProcesses()
.forEach(process -> System.out.println(processDetails(process)));
}

private static String processDetails(ProcessHandle process) {
return String.format("%8d %8s %10s %26s %-40s",
process.pid(),
text(process.parent().map(ProcessHandle::pid)),
text(process.info().user()),
text(process.info().startInstant()),
text(process.info().commandLine()));
}

private static String text(Optional<?> optional) {
return optional.map(Object::toString).orElse("-");
}

Output:

    1        -       root   2017-11-19T18:01:13.100Z /sbin/init
...
639 1325 www-data 2018-12-04T06:35:58.680Z /usr/sbin/apache2 -k start
...
23082 11054 huguesm 2018-12-04T10:24:22.100Z /.../java ProcessListDemo

Get PID from tasklist command

Just use a for /F loop to capture the CSV output of the tasklist command and to extract the right token.

In Command Prompt:

@for /F "tokens=2 delims=," %P in ('tasklist /SVC /FI "Services eq .Service02" /FO CSV /NH') do @echo %~P

In a batch file:

@echo off
for /F "tokens=2 delims=," %%P in ('
tasklist /SVC /FI "Services eq .Service02" /FO CSV /NH
') do echo %%~P

The ~-modifier removes the surrounding quotation marks from the PID value.



Related Topics



Leave a reply



Submit