What is the right way to write/configure application under Linux, that runs all the time and serves external requests (TCP, database, filesystem, any kind of them).
I specifically do not call this daemon, because it may mean something I do not want it to in Linux environment.
I already read multiple topics, including:
best way to write a linux daemon
Best practice to run Linux service as a different user
but none of them gives full comparison about which approach to use.
I see following options:
But which of them is the way to go. Or if they all can be used, what constitutes daemon in Linux?
I am looking for an equivalent of running application as a service under windows (and any .exe can be automatically made for runs as a service with use of sc).
My requirements are as following:
I am the author of the application, but would prefer not to alter it to handle daemonization.
My guess would be to write custom init.d script which in turn would call daemon() function from /etc/init.d/functions. Am I right?
RHEL7 uses systemd as its init system, which will take care of most of your requirements. You should write a systemd unit file for your daemon (called a service in systemd parlance). It can then:
systemctl enable yourservice
.User
key in your unit file.service start
: Yes, or through systemctl start
.Restart
key in your unit file (for example, on-failure
or always
).journalctl
and/or written to syslog, as needed.Your application doesn't need to (and shouldn't) daemonize itself when run under a modern init system. This goes not only for systemd but also for upstart, as well as supervisors like runit, daemontools, supervisord and most everything else. Daemonizing is a bit finicky and easy to get wrong. Just write your application like you normally would, and let an init system do its thing.