2

I am developing an application with different behavior depending on the arguments :

  • "-config" starts a Gtk window to change options, start and close the daemon.
  • "-daemon" starts a background process that does something every X minutes.

I already know how to use fork/system/exec etc...

But I would like to know the main logic of such application to :

  • restart or refresh the daemon when configuration change.
  • keep only one instance of the daemon.

I have read that killing the daemon to restart it is not a clean way to do.
How other applications do ? (ubuntuone, weather forecast, rss feed working with notification area)

Thanks for your help.

PS : I don't want to create a system-wide daemon, just a user application with a background process.

3
  • 1
    I don't see anything Ubuntu specific here. Might be a better fit on StackOverflow. Sep 11, 2012 at 10:12
  • It is not explicitly nor exclusively Ubuntu, so you might be right. But my question was "How other applications do?". On an Ubuntu forum, I thought it would be understandable... Sep 11, 2012 at 10:45
  • @aking1012 Questions generalizable to GNU/Linux are historically considered on topic. Unless consensus has changed.
    – Jjed
    Sep 11, 2012 at 14:55

2 Answers 2

3

Consider offloading notification onto the system!

The general way things to communicate with a daemon in modern GNU/Linux desktop is D-Bus. To grossly simplify what it does: D-Bus allows you to call functions from a daemon "service" process. You could easily just have a refreshConfig() function that reloads configuration from file on call.

That said, the specific thing you're trying to do has a solution more germane to Ubuntu and GTK+. With GSettings, you create a configuration schema that defines key/value pairs you're interested in storing. After doing so, you can put a line something like this in your daemon...

mySettings->connect("changed::setting", onSettingChanged);

And GObject will wake up your daemon and call onSettingChanged() when any configuration changes. That function can call a new instance of itself and exit(0) if you wish. Although I can't think of a code logic reason why you'd need to do that.

Here is an excellent guide for using GSettings with Python. It should be easily translatable to C++.

1
  • I already use Gtk in my application, but I did not know that Gtk provides method to use dconf. I will think about your post. Thank you. Sep 11, 2012 at 18:07
2

You are touching here on an ocean of information. The problem is called inter-process communication (IPC) and there are tons of materials on that, depending on your programming language, need for complexity etc. It ranges from communicating over files (presence or absence of a lock file is a simple variant of this), through named pipes to Sys V IPC including semaphores and shared memory.

In your case, I would look into named pipes.

3
  • Thank for your help. Your answer made me think about detecting changes on the configuration file. I also remember that I can catch signals to properly kill my daemon. I just still wonder how to detect if the daemon is started : a lock file looks messy (what if the daemon crashes ?), what about checking the ps command ? Sep 11, 2012 at 10:29
  • 1
    Often, the lock file contains the PID of the daemon you started. You check (i) for the existence of the lock file (2) for the existence of a process with the PID contained in the file and (3) whether that process has the same name as your daemon. This is not 100% foolproof, however. (1) and (2) alone are not sufficient, after a reboot it can be that another process has the same PID as the one stored in the stale lock file. Hopefully if, in addition, the full path and name of your process is the same, you can be reasonably sure that it is correct.
    – January
    Sep 11, 2012 at 10:38
  • How my God. So genius ! I never thought about the 3rd step. I understand now why so many applications use it. Thanks for the complete explanation. Sep 11, 2012 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .