standard way to communicate with a running process via shell script in linux

Go To StackoverFlow.com

3

Is there a standard linux/unix pattern for communicating with long running process?

For example, I have few hundred process, written in c++, and running on various machines and I would like to send them a command like reload configuration, start, stop etc via shell scripts.

2012-04-05 03:04
by Jimm


1

Since you also care about remote processes, and assuming you can modify the source code of all your programs, you could consider some way to communicate with them:

  • defining your own small textual protocol, and have each process listening on some socket or some named pipe. You probably would need some multiplexing syscall like poll

  • use existing libraries and tools like MPI, Corba, or perhaps D-Bus or ONC/RPC/XDR

  • change the configuration files of your application, and have signal conventions, e.g. catch SIGHUP to reload the configuration, and SIGTERM to properly terminate it (but there is no way to send a signal remotely; you'll need e.g. to ssh some kill command).

2012-04-05 04:53
by Basile Starynkevitch
Option #3 (signals) has the drawback that from the time you learn the PID to the time you issue the kill $PID the system could have reassigned that PID to a different process. Not likely, perhaps, but quite possible for a) you to learn your process' PID, b) your process to die, c) a new process to start and the d) OS to choose the PID you thought belonged to your process. making e) sending kill to that PID the wrong thing to do - Jesse Chisholm 2015-11-12 22:34


6

Signals.

2012-04-05 03:06
by Ignacio Vazquez-Abrams
The OP wants to be able to communicate with remote processes too, most likely in a master-slave model. Signals won't help in that case isn't it - Gangadhar 2012-04-05 04:18
Sure they will. kill works fine over ssh - Ignacio Vazquez-Abrams 2012-04-05 04:22
Thanks for that. I didn't know about it. Looks like this link explains that too - http://stackoverflow.com/questions/1512132/how-to-send-sigint-to-a-remote-process-over-ss - Gangadhar 2012-04-05 04:27
Signals has the drawback that from the time you learn the PID to the time you issue the kill $PID the system could have reassigned that PID to a different process. Not likely, perhaps, but quite possible for a) you to learn your process' PID, b) your process to die, c) a new process to start and the d) OS to choose the PID you thought belonged to your process. making e) sending kill to that PID the wrong thing to do - Jesse Chisholm 2015-11-12 22:35


2

If you're trying to trigger simple actions like the start/stop/reload configuration as you've described, the most common method is to use signals.

From your shell script you can use the kill command to send a specific signal to a specific process. Within your process you would implement one or more signal handlers. The signal handler(s) are registered to receive one or more signals by using the signal() function, or the sigaction() function.

Conventionally SIGHUP is used to trigger a reload of configuration. SIGSTOP and SIGCONT may be appropriate for pausing and resuming.

man 7 signal will show you a complete list of available signals to choose from.


If you need to trigger more complex actions you can create a named pipe. Have your process create the pipe and, from your shell script, just echo commands to it.

2012-04-05 03:35
by Andrew Edgecombe
Named Pipe is safer. Signals has the drawback that from the time you learn the PID to the time you issue the kill $PID the system could have reassigned that PID to a different process. Not likely, perhaps, but quite possible for a) you to learn your process' PID, b) your process to die, c) a new process to start and the d) OS to choose the PID you thought belonged to your process. making e) sending kill to that PID the wrong thing to do - Jesse Chisholm 2015-11-12 22:35
Ads