There is a running exe file on the golang on the remote computer.
At the time of the update, I need to send a command to this process from another program (on the same computer), "close", after which it closes and copies the new exe and then it starts. How can I transfer the value to the program and then open the file?

  • Use syscall and any of the IPC system methods, a named event for example (CreateEvent). - Abyx
  • Very messy. IPC in Go is implemented through the network: net / rpc or net / rpc / jsonrpc . The remaining methods depend on the platform and most likely it is better to learn about them from the C programmers. - Ivan Black

1 answer 1

As an option through the signals .

When you start up, you force your program to create a .pid file, where to record the PID of its process. Sign it with signals (you can filter it with the 2nd parameter to Notify) and neatly end the program when you receive a signal, deleting the .pid file.

 ch := make(chan<- os.Signal) signal.Notify(ch) go func(){ <- ch // вставь свой механизм завершения // например server.stop() os.Exit(0) } 

From the "other" program (let's call the updateer), read the PID from a file, find the process by PID os.FindProcess, send a signal to your program os.Process.Signal, wait for the completion of os.Process.Wait, copy the new file, launch it. Look at os and signal, but "Signal sends a signal to the Process. Sending Interrupt on Windows is not implemented.", So tune in to which SIGUSR2 thread.

Option 2: "server". The “server” program starts, spawns the Worker child via syscall.ForkExec (separate exe's) and “falls asleep” waiting for alerts through the same signal from the updater (the updater at this point overwrites the Workera exe's.) it sends a signal to this descendant, it successfully completes, after which a new syscall.ForkExec is already called for a new binary, we fall asleep in a circle, and the completion of such a server can be configured with another signal, for example SIGUSR1.

The difference between the approaches is that in the second case you can take out all the logic of downloading, restarting and even versioning into the “server” itself, and the third party will only need to send a signal.

You didn’t specify what a “other” program is on the same computer, is it yours, or is it really a thread of 1C with its limitations. And so you can even write the third prog on golang, which will be the only thing that will poke into the "server" on the infe from the pid file of the "server" and end.