0

I have read the tutorial Linux Daemon Writing HOWTO and Creating a Daemon Process in C Language with an Example Program.

What I want to do is have a service that is available to be called via the terminal at any point. This is simply a binary application that exists in the system PATH.

But I want the memory to be conserved on subsequent calls. Upon bootup, I want myService to be called and initialized. I'll call this via myService init. Later on, I should be able to call myService doThis or myService doThat, but the memory should always be conserved.

How do I do this?

Kousha
  • 32,871
  • 51
  • 172
  • 296
  • By "conserved", do you mean "persisted" or "optimized for size"? – Frédéric Hamidi Apr 06 '15 at 18:37
  • persisted, so the variables retain their values. If variable `x` was set to `2`, it should still be `2` next time the service is called, until the service changes its value to another number, upon which it should still remain that new number. – Kousha Apr 06 '15 at 18:39
  • Well, that's pretty much how a daemon works -- it runs in the background, so its memory remains the same across "calls" (well, except if you want state to be persisted across daemon runs / reboots, but then this question gets even more broad). Technically you would have a "client" command that RPCs the daemon process in order to send your `doThis` and `doThat` calls. `init` could be performed by the daemon itself on startup. – Frédéric Hamidi Apr 06 '15 at 18:43
  • If the process doesn't exit, its static memory remains intact. Do you want server state to be saved and subsequently restored in case of a reboot or process restart? – jxh Apr 06 '15 at 19:11
  • No, I don't need to save the memory upon reboot. I just want it to be a deamon that is initialized every time the system reboots and that I can call it via terminal whenever I want. I don't know how to the "client" command that @FrédéricHamidi suggested. – Kousha Apr 06 '15 at 19:13
  • 1
    @Kousha, then see the related, possible duplicate [Interprocess communication with a Daemon](http://stackoverflow.com/q/6721195/464709). – Frédéric Hamidi Apr 06 '15 at 19:19
  • @FrédéricHamidi, so if I'm getting this correct, I write to a "file" and the deamon is monitoring this file for responses? – Kousha Apr 06 '15 at 19:22
  • 1
    @Kousha, technically you write to a "socket", but it is a kind of file, yes. That particular incarnation is used to send streams to the same machine, other types of sockets usually deal with the network. But the principle remains the same, you still read and write streams of bytes. As with files. Then again, [everything is a file](http://en.wikipedia.org/wiki/Everything_is_a_file), and [everything is a stream of bytes](http://yarchive.net/comp/linux/everything_is_file.html). – Frédéric Hamidi Apr 06 '15 at 19:24

0 Answers0