0

Hi I need to restart a running C process from Linux prompt. I googled it and some sites suggested SIGHUP which is not working in my case. Any other suggestions/pointers?

I have following code snippet

#include <stdio.h>
#include <unistd.h>

int main() {
    fprintf(stderr,"%s","Entering main function\n");
    while(1) {
       sleep (1);
}
fprintf(stderr,"%s","Exiting main function\n");
return;
}

Linux output

#] ./simple &
[1] 489440
#] Entering main function
#] ps aux | grep simple
user 489440  0.0  0.0   3924   360 pts/135  S    13:25   0:00 ./simple
user 489710  0.0  0.0 105312   804 pts/135  S+   13:25   0:00 grep simple
#] kill -1 489440
[1]    Hangup                        ./simple
#] ps aux | grep simple
user 490181  0.0  0.0 105312   800 pts/135  S+   13:25   0:00 grep simple
#]
Hipster1206
  • 411
  • 6
  • 13
  • possible duplicate of [How to restart C daemon program in Linux after receiving SIGHUP signal](http://stackoverflow.com/questions/2955847/how-to-restart-c-daemon-program-in-linux-after-receiving-sighup-signal) – Foon Aug 03 '15 at 20:44

2 Answers2

1

If you want to use signals to re-start your process, you can in theory create your own signal handler for SIGHUP and and have the signal handler exec your program+arguments again. Although; I suspect you may want to really read up on signal handlers and the semantics around them before undertaking this.

See: man signal and man execvp

Kjetil Joergensen
  • 1,595
  • 11
  • 10
0

Restart a process is to stop it and start it again. So you may just kill the process and start it again. Also, you may easily write a script to kill and start a process (by its name or by its pid).