简体   繁体   中英

How to restart a running process in Linux?

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
#]

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM