# How to check if a pid is currently running?

**URL:** https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908
**Category:** General Usage
**Tags:** operating-system
**Created:** [February 22, 2022, 1:57pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908 "2022-02-22T13:57:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)
#### Post date: [February 22, 2022, 1:57pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/1 "2022-02-22T13:57:45Z")

</div>

I want to confirm that a specific program is running. The other program can write its pid to a file, so I can read that. Is there an existing API (not platform specific) in Julia to confirm that a certain pid (or process by some other query, maybe name or something) is running?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [February 22, 2022, 4:18pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/2 "2022-02-22T16:18:06Z")

</div>

Assuming you are using Linux, you can use [`kill -0 $PID`](https://unix.stackexchange.com/questions/169898/what-does-kill-0-do):

```
julia> run(`kill -0 43181`)
Process(`kill -0 43181`, ProcessExited(0))

julia> run(`kill -0 43182`)
kill: (43182): No such process
ERROR: failed process: Process(`kill -0 43182`, ProcessExited(1)) [1]

Stacktrace:
 [1] pipeline_error
   @ ./process.jl:531 [inlined]
 [2] run(::Cmd; wait::Bool)
   @ Base ./process.jl:446
 [3] run(::Cmd)
   @ Base ./process.jl:444
 [4] top-level scope
   @ REPL[2]:1

```

---

<div class="post-metadata">

### Author: ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)
#### Post date: [February 22, 2022, 4:19pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/3 "2022-02-22T16:19:21Z")

</div>

I’m not using Linux. Sorry, I should have specified I was asking if there was an API for it.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [February 22, 2022, 4:37pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/4 "2022-02-22T16:37:55Z")

</div>

You can `ccall` [`uv_kill`](http://docs.libuv.org/en/v1.x/guide/processes.html#sending-signals-to-processes):

```julia
ccall(:uv_kill, Cint, (Cint, Cint), getpid(), 0)

```

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [February 22, 2022, 4:38pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/5 "2022-02-22T16:38:34Z")

</div>

I don’t see one on the documentation, but you might try calling [uv\_kill](https://docs.libuv.org/en/v1.x/guide/processes.html#sending-signals-to-processes) directly with

```
julia> @ccall uv_kill(43181::Cint, 0::Cint)::Cint
0

julia> @ccall uv_kill(43182::Cint, 0::Cint)::Cint
-3

```

Documentation says it might work on Windows, worth a test!

Edit: Oops, sniped!

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [February 22, 2022, 4:45pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/6 "2022-02-22T16:45:37Z")

</div>

I think I have a solution, but first a caveat, it doesn’t seem robust what you want to do.

The max PID in at least Linux (where it’s configurable) is 32768 so lets say the process dies, there’s a possibility that some new process will have started with that PID. Might need to have been many, unless something close to the max number of PIDs already in use.

You can change (infor for LInux), to make less likely, see here:

[https://stackoverflow.com/questions/6294133/maximum-pid-in-linux](https://stackoverflow.com/questions/6294133/maximum-pid-in-linux)

What you can do with Linux/Unix kill (or uv\_kill, I see already suggested), you can also do with the underlying kill in C (and thus somehow with `ccall` from Julia). I do not know the pros and cons vs uv\_kill that is just an abstraction.

```julia
$ man 2 kill
       #include <sys/types.h>
       #include <signal.h>

       int kill(pid_t pid, int sig);
[..]
       If sig is 0, then no signal is sent, but existence and permission checks are still performed; this can be used to check for the existence of a process ID
       or process group ID that the caller is permitted to signal.

```

I don’t know which signals are portable according to C or POSIX. From way back I recall Windows described as POSIX compatible. From memory that support wasn’t really useful to call Windows sort of Unix compatible, or only just barely. That has since changed with WSL [2], but you want this to work in Windows without using any WSL.

[https://stackoverflow.com/questions/3043978/how-to-check-if-a-process-id-pid-exists](https://stackoverflow.com/questions/3043978/how-to-check-if-a-process-id-pid-exists)

---

<div class="post-metadata">

### Author: ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)
#### Post date: [February 22, 2022, 6:26pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/7 "2022-02-22T18:26:12Z")

</div>

That’s a really good point. I would need to add some other mechanism to ensure it’s the same process. I could have the writing process touch the file occasionally and then the reading process would verify the last modified date of the file is less than the touch period.

---

<div class="post-metadata">

### Author: ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)
#### Post date: [February 22, 2022, 6:27pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-pid-is-currently-running/76908/8 "2022-02-22T18:27:39Z")

</div>

> [@contradict](#):
>
> ```
> julia> @ccall uv_kill(43181::Cint, 0::Cint)::Cint
> 0
> 
> ```

I confirmed this works on windows. It returns 0 if found, -4040 if not.
