I have a server that I want to add to systemd so that it launch automatically and restarts on crash.
Does the server have to be a daemon?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
No, the process does not have to be a daemon. Processes running in the foreground can be handled by a systemd service of the 'simple' type.
The Debian Wiki even mentions that running in the foreground is preferred, and that most services use the 'simple' type.
Processes that run as daemons usually start by forking off a child process. To handle such processes with systemd, you need the 'forking' type of service.
There are a few other types as well, such as 'oneshot'. See the systemd.service man page for more details.
Yes. Processes launched by service managers such as systemd run in dæmon context.
But be aware of what being a dæmon actually means. To quote Bach:
Processes in the UNIX system are either user processes, daemon processes, or kernel processes. Most processes on typical systems are user processes, associated with users at a terminal. Daemon processes are not associated with any users, but do system-wide functions, such as […].— Maurice J. Bach (1986). The Design of the UNIX Operating System. Prentice Hall. ISBN 01320177571. page 238.
It is nothing to do with whether a program forks or not; and everything to do with whether the process is associated with a user login session. If your program that you want to manage as a service does a system-wide function and isn't supposed to be associated with user login sessions, then it can be run in dæmon context. You can wrap it in a systemd service unit, and use systemd to manage it. (Or you can do similar with other service management toolsets.)
(systemd has the concept of per-user services, which have a somewhat complex relationship with user login sessions, X servers, per-user runtime directories, per-user Desktop Bus brokers, and PAM; which is the source of quite a few questions on this WWW site. The aforegiven relates to system-wide services in systemd, however, which is presumably what you are thinking of.)
For best results, of course, the program should not fork to "dæmonize". "dæmonization" is the idea that user processes can spawn dæmon processes. This hasn't really been the case since the 1980s, for reasons beyond the scope of this answer, and the idea is flawed. In any case, it is totally superfluous for anything run under systemd, daemontools, the nosh toolset, perp, s6, runit, and other such service management mechanisms. Processes spawned by such service managers are already in dæmon context.