2

I put "restart" in quotes because the following question on SO illustrates what I'm trying to do:

https://stackoverflow.com/questions/27957091/restarting-a-process-in-the-same-linux-terminal

I'm using mono to run some C# code and for cross-platform compatibility issues I'd rather not go the obvious route and just have a shell script or systemd handle restarting the process.

On windows what happens is a new window spawns, and the old one dies. In Linux I just get invalid handle exceptions being thrown. My ideal case would be to just re-use the same terminal.

I guess I'd have to have the terminal detach from the process that's about to end, attach to the new one, and then have the old process die?

Is what I'm asking even possible?

nathanvy
  • 53
  • 3
  • You `exec` an executable to replace one process with another. – mikeserv Jan 15 '15 at 05:46
  • I've tried that, but my process just dies and drops me back to the shell. – nathanvy Jan 15 '15 at 07:32
  • You could either just `exec` yourself, or... if you really want to start a new process, spawn a new process (fork and have one instance `exec` into the new process and the other clean up and die). In both cases you have to be careful to close all open files before `exec` because otherwise the file handles are lost (well, the file descriptors were there but the new instance would have to know about them). – orion Jan 15 '15 at 08:59
  • The canonical way to do that in C# is to use Process.Start() which spawns a new terminal window in Windows. Unfortunately this causes invalid handle exceptions in linux, and I'm not really sure how to rectify that. – nathanvy Jan 15 '15 at 09:12
  • @nathanvy As orion suggested, use the lower-level `execv()` system call. – Barmar Jan 15 '15 at 17:57
  • Yes, execv is exactly what I tried. – nathanvy Jan 15 '15 at 22:53
  • Can you `trap` a signal (e.g. 1, `SIGHUP`) within the process and use that to die and spawn a replacement? In POSIX shell (e.g. `bash`), that's merely `trap "$0; exit" SIGHUP` (plus any cleanup for backgrounded processes, which must be before the `exit`). – Adam Katz Feb 23 '15 at 17:19

0 Answers0