How to restart (or reset) a running process in linux

Make your client exec /proc/self/exe when it receives that paticular message. You don't need to know where the executable actually resides in the file system. And you can reuse main()'s argv to construct a new argument vector.

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

int main(int argc, char **argv)
{
        char buf[32] = {};
        char *exec_argv[] = { argv[0], buf, 0 };
        int count = argc > 1 ? atoi(argv[1]) : 0;

        printf("Running: %s %d\n", argv[0], count);
        snprintf(buf, sizeof(buf), "%d", count+1);
        sleep(1);

        execv("/proc/self/exe", exec_argv);

        /* NOT REACHED */
        return 0;
}

This restart.c runs like this:

$ gcc restart.c 
$ ./a.out 3
Running: ./a.out 3
Running: ./a.out 4
Running: ./a.out 5

The normal way to do this is to let your program exit, and use a monitoring system to restart it. The init program offers such a monitoring system. There are many different init programs (SysVinit, BusyBox, Systemd, etc.), with completely different configuration mechanisms (always writing a configuration file, but the location and the syntax of the file differs), so look up the documentation of the one you're using. Configure init to launch your program at boot time or upon explicit request, and to restart it if it dies. There are also fancier monitoring programs but you don't sound like you need them. This approach has many advantages over having the program do the restart by itself: it's standard, so you can restart a bunch of services without having to care how they're made; it works even if the program dies due to a bug.

There's a standard mechanism to tell a process to exit: signals. Send your program a TERM signal. If your program needs to perform any cleanup, write a signal handler. That doesn't preclude having a program-specific command to make it shut down if you have an administrative channel to send it commands like this.


If the client application is a Linux service, it can be restarted with this command:

service <clientapp> restart

or forced to reload its configuration:

service <clientapp> reload
service <clientapp> force-reload

If, more likely, it's a custom application, it needs to have embedded in its code the feature to restart itself or reload its configuration upon reception of a signal or event. Failing to do so, as a last resort you can always kill the client app:

pkill -9 <clientapp>

and restart again, but it's ugly as it leaves the app in an undetermined state.