5

I have two Linux systems communicating over sockets (Desktop and ARM-based development board).

I want to restart (or reset) my client application (running on a development board) when server sends a particular predefined message. I don't want to restart (reboot) Linux, I just want that client application restart itself automatically.

I am unable to understand how it should be done.

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.

6

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
2

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.

  • 1
    Unless the application is already in a bogus state, the -9 argument to pkill is not needed, not using it should allow the application to cleanly stop itself. – WhiteWinterWolf Jun 6 '15 at 11:04
  • @dr01 If application is not a service then how to restart it? – Saad Rafey Jun 6 '15 at 11:14
  • @SaadRafey: it's written in my answer, the capability to restart itself cleanly or to reload the configuration must be written in the code. – dr01 Jun 6 '15 at 11:59
  • I think this only works with OLD UBUTNU versions – noone Sep 10 '18 at 19:09
  • @noone Not at all, that's the standard SystemV command for service management. Now, it's true that most modern versions of distros use Systemd. – dr01 Sep 11 '18 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.