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:
Linux daemonize
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?
In Unix and Linux, a daemon is a program that runs in the background without requiring any user interaction. The file name of a software daemon usually ends in the letter d.
A daemon (also known as background processes) is a Linux or UNIX program that runs in the background. Almost all daemons have names that end with the letter "d". For example, httpd the daemon that handles the Apache server, or, sshd which handles SSH remote access connections. Linux often start daemons at boot time.
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.
If I understand you correctly, especially with respect to RHEL 7 requirement, Systemd Unit is your friend. If it is something you tried or discarded for any reason, please elaborate further.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With