close

Function Repository Resource:

ProcessRunningQ

Source Notebook

Check if a process, session or task is currently active

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ProcessRunningQ"][proc]

gives True if the process, task or session specified by proc is running, and False otherwise.

ResourceFunction["ProcessRunningQ"][patt]

gives True if there are any processes running on your computer system whose names match the string pattern patt.

ResourceFunction["ProcessRunningQ"][propval]

gives True if there are any processes for which property prop has value val.

ResourceFunction["ProcessRunningQ"][<|prop1val1,|>]

gives True if there are any processes for which property propi has value vali.

Details and Options

The value for proc can have any of the following forms:
pidan Integer corresponding to a process ID
ProcessObject[]a system process represented as a ProcessObject
TaskObject[]a background task
CloudObject[]a ScheduledTask deployed to the cloud
WebSessionObject[]the underlying process used by the WebSessionObject
ExternalSessionObject[]the underlying process used by the ExternalSessionObject
KernelObject[]the link for communicating with the given subkernel
LinkObject[]a LinkObject
InputStream[]an input stream
OutputStream[]an output stream
In ResourceFunction["ProcessRunningQ"][propval], properties that can be used include:
"PID"process ID
"PPID"parent process ID
"Memory"memory the process is using
"StartTime"date the process was started
"RealTime"time since the process started
"SystemTime"CPU time spent in kernel space
"UserTime"CPU time spent in user space
"Threads"number of threads
"User"the user the process belongs to
"Program"name of the program
"Path"full file path of the program
ResourceFunction["ProcessRunningQ"] returns False if proc is not a valid process specification.

Examples

Basic Examples (7) 

Check if a process is currently running using a process ID:

In[1]:=
ResourceFunction["ProcessRunningQ"][$ProcessID]
Out[1]=
Image

Check if a process is running by name (results may be platform-dependent):

In[2]:=
ResourceFunction["ProcessRunningQ"]["WolframKernel.exe"]
Out[2]=
Image
In[3]:=
ResourceFunction["ProcessRunningQ"]["NotARealProgram.exe"]
Out[3]=
Image
In[4]:=
ResourceFunction["ProcessRunningQ"]["BirdSay.exe"]
Out[4]=
Image

Check if a ProcessObject is running:

In[5]:=
process = StartProcess[$SystemShell]
Out[5]=
Image
In[6]:=
ResourceFunction["ProcessRunningQ"][process]
Out[6]=
Image

Close the process:

In[7]:=
KillProcess[process]

Verify that it’s no longer running:

In[8]:=
ResourceFunction["ProcessRunningQ"][process]
Out[8]=
Image

Check if a TaskObject is running:

In[9]:=
obj = SessionSubmit[ScheduledTask[1 + 1, {5}]]
Out[9]=
Image
In[10]:=
ResourceFunction["ProcessRunningQ"][obj]
Out[10]=
Image

Check again after it has finished:

In[11]:=
Pause[5]; ResourceFunction["ProcessRunningQ"][obj]
Out[11]=
Image

Check if a stream is open:

In[12]:=
str = StringToStream["abc efg hij"]
Out[12]=
Image
In[13]:=
ResourceFunction["ProcessRunningQ"][str]
Out[13]=
Image
In[14]:=
Close[str]
Out[14]=
Image
In[15]:=
ResourceFunction["ProcessRunningQ"][str]
Out[15]=
Image

Use string patterns:

In[16]:=
ResourceFunction["ProcessRunningQ"]["notepad" ~~ ___]
Out[16]=
Image
In[17]:=
ResourceFunction["ProcessRunningQ"][RegularExpression["notepad.*"]]
Out[17]=
Image

Use an association of properties:

In[18]:=
ResourceFunction[
 "ProcessRunningQ"][<|"Program" -> "Wolfram", "Memory" -> x_ /; x > Quantity[50, "MiB"]|>]
Out[18]=
Image
In[19]:=
ResourceFunction[
 "ProcessRunningQ"][<|"Program" -> "Wolfram", "Memory" -> x_ /; x > Quantity[50, "GiB"]|>]
Out[19]=
Image

Scope (7) 

Check if a scheduled task deployed to the cloud is currently following its assigned schedule:

In[20]:=
obj = CloudDeploy[ScheduledTask[1 + 1, "Hourly"]]
Out[20]=
Image
In[21]:=
ResourceFunction["ProcessRunningQ"][obj]
Out[21]=
Image

A task is not considered to be running if it is suspended:

In[22]:=
TaskSuspend[obj]
Out[22]=
Image
In[23]:=
ResourceFunction["ProcessRunningQ"][obj]
Out[23]=
Image

TaskResume the task:

In[24]:=
TaskResume[obj]
Out[24]=
Image
In[25]:=
ResourceFunction["ProcessRunningQ"][obj]
Out[25]=
Image
In[26]:=
TaskRemove[obj]
Out[26]=
Image

Check if a WebSessionObject is running:

In[27]:=
session = StartWebSession["Chrome"]
Out[27]=
Image
In[28]:=
ResourceFunction["ProcessRunningQ"][session]
Out[28]=
Image

Close the session using DeleteObject:

In[29]:=
DeleteObject[session]

Verify that it’s no longer running:

In[30]:=
ResourceFunction["ProcessRunningQ"][session]
Out[30]=
Image

Check if an ExternalSessionObject is running:

In[31]:=
session = StartExternalSession["Python"]
Out[31]=
Image
In[32]:=
ResourceFunction["ProcessRunningQ"][session]
Out[32]=
Image

Close the session using DeleteObject:

In[33]:=
DeleteObject[session]

Verify that it’s no longer running:

In[34]:=
ResourceFunction["ProcessRunningQ"][session]
Out[34]=
Image

Check that parallel subkernels are still running:

In[35]:=
kernels = LaunchKernels[2]
Out[35]=
Image
In[36]:=
ResourceFunction["ProcessRunningQ"] /@ kernels
Out[36]=
Image

Close one of the kernels:

In[37]:=
CloseKernels[1]
Out[37]=
Image

Verify that one of the kernels is no longer running:

In[38]:=
ResourceFunction["ProcessRunningQ"] /@ kernels
Out[38]=
Image

Check that a LinkObject is still running (this example requires the ability to launch new kernels with LinkLaunch on the current machine):

In[39]:=
link = LinkLaunch[First[$CommandLine] <> " -wstp"]
Out[39]=
Image
In[40]:=
ResourceFunction["ProcessRunningQ"][link]
Out[40]=
Image

Get the $ProcessID of the linked kernel:

In[41]:=
LinkRead[link]
Out[41]=
Image
In[42]:=
LinkWrite[link, EnterTextPacket["$ProcessID"]];
LinkRead[link]
Out[24]=
Image
In[43]:=
resp = LinkRead[link]
Out[43]=
Image
In[44]:=
pid = ToExpression[First[resp]]
Out[44]=
Image

Check that it’s running by using the PID:

In[45]:=
ResourceFunction["ProcessRunningQ"][pid]
Out[45]=
Image

Close the link using LinkClose:

In[46]:=
LinkClose[link]

Verify that it’s no longer running:

In[47]:=
ResourceFunction["ProcessRunningQ"][link]
Out[47]=
Image

Check using the PID:

In[48]:=
ResourceFunction["ProcessRunningQ"][pid]
Out[48]=
Image

Processes do not need to be started by the current kernel:

In[49]:=
proc = RandomChoice[SystemProcesses[]]
Out[49]=
Image
In[50]:=
ResourceFunction["ProcessRunningQ"][proc]
Out[50]=
Image

Use the PID of a random system process:

In[51]:=
pid = RandomChoice[SystemProcesses[]]["PID"]
Out[51]=
Image
In[52]:=
ResourceFunction["ProcessRunningQ"][pid]
Out[52]=
Image

Properties and Relations (2) 

When patt is a string pattern, ProcessRunningQ[patt] is equivalent to MatchQ[SystemProcesses[patt],{__ProcessObject}]:

In[53]:=
ResourceFunction["ProcessRunningQ"]["BirdSay." ~~ __]
Out[53]=
Image
In[54]:=
SystemProcesses["BirdSay." ~~ __]
Out[54]=
Image

The same is true for rules:

In[55]:=
ResourceFunction["ProcessRunningQ"][
 "Path" -> FileNameJoin[{$InstallationDirectory, "SystemFiles", "Java"}]]
Out[55]=
Image
In[56]:=
SystemProcesses[
 "Path" -> FileNameJoin[{$InstallationDirectory, "SystemFiles", "Java"}]]
Out[56]=
Image

Possible Issues (6) 

A LinkObject may still be reported as running if it appears in Links[] even if the connected process has been terminated:

In[57]:=
link = LinkLaunch[First[$CommandLine] <> " -wstp"]
Out[57]=
Image

Get the PID:

In[58]:=
LinkRead[link]
Out[58]=
Image
In[59]:=
LinkWrite[link, EnterTextPacket["$ProcessID"]];
LinkRead[link]
Out[42]=
Image
In[60]:=
resp = LinkRead[link]
Out[60]=
Image
In[61]:=
pid = ToExpression[First[resp]]
Out[61]=
Image

Get the corresponding ProcessObject:

In[62]:=
proc = First[SystemProcesses["PID" -> pid]]
Out[62]=
Image

Terminate the process with KillProcess instead of LinkClose:

In[63]:=
KillProcess[proc]

The LinkObject is still active:

In[64]:=
ResourceFunction["ProcessRunningQ"][link]
Out[64]=
Image
In[65]:=
LinkReadyQ[link]
Out[65]=
Image
In[66]:=
MemberQ[Links[], link]
Out[66]=
Image

However, the process itself is not:

In[67]:=
ResourceFunction["ProcessRunningQ"][pid]
Out[67]=
Image

Version History

  • 1.0.0 – 14 August 2019

Related Resources

Author Notes

TODO: ChannelListener, SocketObject

License Information