How to check if a process started in the background still running?

AhmetB - Google

It looks like if you create a subprocess via exec.Cmd and Start() it, the Cmd.Process field is populated right away, however Cmd.ProcessState field remains nil until the process exits.

   // ProcessState contains information about an exited process,
   // available after a call to Wait or Run.
   ProcessState *os.ProcessState

So it looks like I can't actually check the status of a process I Start()ed while it's still running?

It makes no sense to me ProcessState is set when the process exits. There's an ProcessState.Exited() method which will always return true in this case.


So I tried to go this route instead: cmd.Process.Pid field exists right after I cmd.Start(), however it looks like os.Process doesn't expose any mechanisms to check if the process is running.

os.FindProcess says:

On Unix systems, FindProcess always succeeds and returns a Process for the given pid, regardless of whether the process exists.

which isn't useful –and it seems like there's no way to go from os.Process to an os.ProcessState unless you .Wait() which defeats the whole purpose (I want to know if the process is running or not before it has exited).

torek

I think you have two reasonable options here:

  • Spin off a goroutine that waits for the process to exit. When the wait is done, you know the process exited. (Positive: pretty easy to code correctly; negative: you dedicate an OS thread to waiting.)

  • Use syscall.Wait4() on the published Pid. A Wait4 with syscall.WNOHANG set returns immediately, filling in the status.

It might be nice if there were an exported os or cmd function that did the Wait4 for you and filled in the ProcessState. You could supply WNOHANG or not, as you see fit. But there isn't.


The point of ProcessState.Exited() is to distinguish between all the various possibilities, including:

  • process exited normally (with a status byte)
  • process died due to receiving an unhandled signal

See the stringer for ProcessState. Note that there are more possibilities than these two ... only there seems to be no way to get the others into a ProcessState. The only calls to syscall.Wait seem to be:

  • syscall/exec_unix.go: after a failed exec, to collect zombies before returning an error; and
  • os/exec_unix.go: after a call to p.blockUntilWaitable().

If it were not for the blockUntilWaitable, the exec_unix.go implementation variant for wait() could call syscall.Wait4 with syscall.WNOHANG, but blockUntilWaitable itself ensures that this is pointless (and the goal of this particular wait is to wait for exit anyway).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if a process started with Process.Start is still running?

How to tell if a process started with CreateProcess is still running?

How to send to background a running process started with sudo?

How do I check if a running process is a background process?

How to check if a forked process is still running from the c program

How to check if a process is still running using Python on Linux?

Check if app is running, NOT as background process

How to check if a timer is still running or not?

a process started with sudo and put in background still exist after ssh disconnect

How is a process defined as running in background?

How to check if apscheduler is running in the background

How to check if application is running on background?

Process Still running in background after python script converted to exe with pytoexeconverter

How to check if process is running in linux

How to check the process is already running or not

How to detect if a Node spawned process is still running?

how to exit Powershell when a process is still running?

How to check if a new process started in windows?

Should I use Thread.Sleep() to check if a process is still running?

how to check if Azure Function is still running or not

How to check ntpd is still running and synced

How to check if Laravel command is still running?

Checking if process still running?

How to move a running process to background (UNIX)

How to know if a running background process cored

How to pipe commands to a process running in the background?

How to use nohup on a process already running in background

Check running status of a background process launched from same bash script

How to check whether code is running in background or not?