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