Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a process is running or not on Windows?

I am trying to create a python script which I will later run as a service. Now I want to run a particular part of the code only when iTunes is running. I understand from some research that polling the entire command list and then searching for the application for that list is expensive.

I found out that processes on UNIX-based operating systems create a lock file to notify that a program is currently running, at which point we can use os.stat(location_of_file) to check if the file exists to determine if a program is running or not.

Is there a similar lock file created on Windows?

If not what are the various ways in Python by which we can determine if a process is running or not?

I am using python 2.7 and iTunes COM interface.

like image 779
nightf0x Avatar asked Oct 16 '11 20:10

nightf0x


People also ask

How do you check whether a process is running or not in Windows?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.

How do I list processes in Windows?

Just tap on Start, type cmd.exe and open the Command Prompt from the results to get started. Simply typing tasklist and hitting the Enter-key displays a list of all running processes on the system. Each process is listed with its name, process ID, session name and number, and memory usage.

How do you check if a process is running in Windows using Java?

If you want to check the work of java application, run 'ps' command with '-ef' options, that will show you not only the command, time and PID of all the running processes, but also the full listing, which contains necessary information about the file that is being executed and program parameters.

What does tasklist command do?

Tasklist is a tool that displays a list of the processes that are running on either a local or remote machine.


2 Answers

You can not rely on lock files in Linux or Windows. I would just bite the bullet and iterate through all the running programs. I really do not believe it will be as "expensive" as you think. psutil is an excellent cross-platform python module cable of enumerating all the running programs on a system.

import psutil     "someProgram" in (p.name() for p in psutil.process_iter()) 
like image 166
Mark Avatar answered Sep 24 '22 02:09

Mark


Although @zeller said it already here is an example how to use tasklist. As I was just looking for vanilla python alternatives...

import subprocess  def process_exists(process_name):     call = 'TASKLIST', '/FI', 'imagename eq %s' % process_name     # use buildin check_output right away     output = subprocess.check_output(call).decode()     # check in last line for process name     last_line = output.strip().split('\r\n')[-1]     # because Fail message could be translated     return last_line.lower().startswith(process_name.lower()) 

and now you can do:

>>> process_exists('eclipse.exe') True  >>> process_exists('AJKGVSJGSCSeclipse.exe') False 

To avoid calling this multiple times and have an overview of all the processes this way you could do something like:

# get info dict about all running processes import subprocess output = subprocess.check_output(('TASKLIST', '/FO', 'CSV')).decode() # get rid of extra " and split into lines output = output.replace('"', '').split('\r\n') keys = output[0].split(',') proc_list = [i.split(',') for i in output[1:] if i] # make dict with proc names as keys and dicts with the extra nfo as values proc_dict = dict((i[0], dict(zip(keys[1:], i[1:]))) for i in proc_list) for name, values in sorted(proc_dict.items(), key=lambda x: x[0].lower()):     print('%s: %s' % (name, values)) 
like image 24
ewerybody Avatar answered Sep 20 '22 02:09

ewerybody