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]=

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

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

Check if a ProcessObject is running:

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

Close the process:

In[7]:=
KillProcess[process]

Verify that it’s no longer running:

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

Check if a TaskObject is running:

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

Check again after it has finished:

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

Check if a stream is open:

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

Use string patterns:

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

Use an association of properties:

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

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]=
In[21]:=
ResourceFunction["ProcessRunningQ"][obj]
Out[21]=

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

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

TaskResume the task:

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

Check if a WebSessionObject is running:

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

Close the session using DeleteObject:

In[29]:=
DeleteObject[session]

Verify that it’s no longer running:

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

Check if an ExternalSessionObject is running:

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

Close the session using DeleteObject:

In[33]:=
DeleteObject[session]

Verify that it’s no longer running:

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

Check that parallel subkernels are still running:

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

Close one of the kernels:

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

Verify that one of the kernels is no longer running:

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

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]=
In[40]:=
ResourceFunction["ProcessRunningQ"][link]
Out[40]=

Get the $ProcessID of the linked kernel:

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

Check that it’s running by using the PID:

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

Close the link using LinkClose:

In[46]:=
LinkClose[link]

Verify that it’s no longer running:

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

Check using the PID:

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

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

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

Use the PID of a random system process:

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

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]=
In[54]:=
SystemProcesses["BirdSay." ~~ __]
Out[54]=

The same is true for rules:

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

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]=

Get the PID:

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

Get the corresponding ProcessObject:

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

Terminate the process with KillProcess instead of LinkClose:

In[63]:=
KillProcess[proc]

The LinkObject is still active:

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

However, the process itself is not:

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

Version History

  • 1.0.0 – 14 August 2019

Related Resources

Author Notes

TODO: ChannelListener, SocketObject

License Information