-3

I'm implementing Watchdog (the app starts watchdog) and can't figure out how to restart a process using a process' path. Edited: How to get path of the process by pid and restart it. Any help or ideas will be appreciated.

Ann
  • 25
  • 9
  • 1
    [exec](https://linux.die.net/man/3/exec) ? – KamilCuk Dec 10 '18 at 08:44
  • If I guess correctly what you are maybe trying to do, then your watchdog should probably be the one which starts the process in the first place, so it will have the path and arguments already. – hyde Dec 10 '18 at 09:05
  • No, the app starts the Watchdog. Watchdog receives pid and remembering the path of the app to restart it if it failed. – Ann Dec 10 '18 at 09:09
  • 1
    Why do you want to get the path out of pid? What if the process dies before the watchdog has a chance to retreive the path? Make the process send its own path to the watchdog (and command line arguments too). – n. m. could be an AI Dec 10 '18 at 09:17
  • The `pipe trick` : open a pipe & fork a child. If the pipe is closed, the other side is dead. Or, even simpler : just fork& block on wait(). – joop Dec 10 '18 at 10:14

2 Answers2

0

You can run your exe directly by using system Library in C. System library executes any command that you can execute in Command Prompt or Bash. So use the batch commands to kill and then rerun the program. To rerun you will need to use the full path.

Windows

#include <stdlib.h>

int main(){
  system("taskkill /IM executablename");
  int status = system("executablename");
}

Linux

 int main(){
      system("killall -v executablename");
      int status = system("fullexecutablenameandpath");
    }
Code Name Jack
  • 2,856
  • 24
  • 40
  • That does not restart the process... Also it does not use the process' path. – hyde Dec 10 '18 at 09:00
  • I have added details to kill and restart. Your question is not clear. – Code Name Jack Dec 10 '18 at 09:04
  • 1
    I'm not the asker, just wanted to explain the downvote. I think the question is too unclear to be answerable. It might be asking for a shell script even. – hyde Dec 10 '18 at 09:08
  • Thank you for the clarification. I have edited to make it more related. The question was confusing. I have updated the answer so that it helps others who come looking. – Code Name Jack Dec 10 '18 at 09:10
  • It's not about shell script, but c. – Ann Dec 10 '18 at 09:11
  • As said any command that works on the command line, can be passed to system("command"); e.g. system("kill -HUP 1"); I can't verify as I don't have any accessible linux system now. – Code Name Jack Dec 10 '18 at 09:17
0

This seems like a XY problem. Don't start an app that then starts a watchdog which might then restart the app.

Write a watchdog (or simply use the already existing one form systemd) to start, watch and restart the app. The path and arguements for the app are then simply passed as arguments to the watchdog.

This way the whole thing will actually work.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42