0

I have written a c++ program which has a infinite loop. I want to run this program as a daemon (or service) in linux.

I want to communicate with this running daemon from outside of program (for example console or another program).

I read about signal handling in c++ and apparently there are some predefined signals. Is there any way to send your own signal to the running program?

Ali Mirzaei
  • 1,496
  • 2
  • 16
  • 27
  • `SIGUSR1` and `SIGUSR2` were meant for such purpose. You can't Define your own arbitrary signal numbers. – πάντα ῥεῖ Aug 29 '15 at 09:37
  • For a perfectly matching answer you should probably define what exactly you mean by *communicate*. – Daniel Jour Aug 29 '15 at 11:37
  • @Daniel I want to send some messages to program and handle them. For example I have a configuration file and I want that the running program load it every time it changed. – Ali Mirzaei Aug 29 '15 at 12:43

2 Answers2

2

Signals are most probably not what you really want to communicate with a demon process, unless you want to terminate it in a specific manner.

Also no, you can't define your own arbitrary signal numbers, as the operating system needs to know how they are sent to the process. As mentioned in my comment there are the SIGUSR1 and SIGUSR2 were intended for user defined signalling purposes.

The easiest way to let an external process communicate with a demon process, is to give it a configuration file, and let the demon watch for changes using the inotify() interface.

This technique is also used by many system demons already.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

You can use kill(pid, signal) from one process to send signal to another. Sending SIGKILL will violently and instantly terminate your process.

Signals are limited to what they express - and you can find that out by accessing page 7 of signal manual. Some signals can be ignored/handled/blocked while others cannot.

If you want true custom inter-process communication you should use pipes or even sockets (bad practice). This way, you would have to define your own protocol and you can do a lot more than with signals.

Here's a tutorial on how to use named pipes to send data to running processes: http://www.linuxjournal.com/content/using-named-pipes-fifos-bash.

Catalin
  • 474
  • 4
  • 19