$ ./server -p 8080 &> ./logs/server.log & echo $! > ./server.pid;
But is this the correct approach?
For userland it may be good enough (altough you may want to control the log and pid file from within your Nim application). Some OS distributions may even provide a "deamon" command / tool that run your application in that mode (kind of http://libslack.org/daemon/manpages/daemon.1.html or http://manpages.ubuntu.com/manpages/bionic/man1/daemon.1.html).
If instead you need to auto-start your server with the system then you would look at implementing a proper deamonization. For example by:
1) brewing your own (traditionally in C language implementing the "main" entry point and calling into your Nim entry routine -- this could require to disable your actual "main" proc from Nim, compile Nim app as some sort of library but pulling thegarbage collector in -- this a different subject altogether, which I'm not familiar with), if taking this path see: http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html, https://www.linux.org/docs/man7/daemon.html , or...
3) lastly but not least (I should have really started wih this one) and probably the most simple you want/need is to use a ready made Nim package, see: https://nimble.directory/search?query=daemon. The first entry there (implemented by Status / status-im company) seems to implement the double forking method (plus a Windows service bonus mode). Check that its licensing and the actual implementation (signal masking, file mode masking, session leader stuff, stdin/stdout handling via dup2() etc... -- compare it to the C implementation) meet your requirements.
P.S. check all the other packages from link in 3) as they may provide inpiration for a mix of functionalities (like extra signals on the second one for example) if implementing your own package.
Good luck,
Lucian
There are different init system and tools to run an application as a daemon without having to increase your application's complexity. Currently a lot of distributions ship systemd by default as it provides features like automated health check and restart and security sandboxing.
I wrote a little tool [1] that generates a template for Nim projects and ships an example .service file.
I wrote this nim program to elevate other programs to daemon status (and restart them if they crash):
https://github.com/treeform/guardmons/blob/master/daemon.nim
Wholeheartedly recommend not reinventing the daemon wheel - self-daemonizing programs and self log-rotating problems never do things quite the way one needs.
use systemd, if that's what on your system, or (I recommend) a djb-inspired system: daemontools, s6, nosh , etc - they are ultra simple and roc solid.