Skip to content

Add daemonization for node - #39

Merged
rustaceanrob merged 2 commits into
kernel-node:masterfrom
rustaceanrob:26-3-9-daemonize
Mar 11, 2026
Merged

Add daemonization for node#39
rustaceanrob merged 2 commits into
kernel-node:masterfrom
rustaceanrob:26-3-9-daemonize

Conversation

@rustaceanrob

@rustaceanrob rustaceanrob commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Closes #25

From commit one, references in commit:

libc is brought in here to do a couple of calls. First, a fork is used to create a child process. Then, if the process is the parent, the program exits immediately. If the process is the child, a new session is made where the child is the group leader, and gives that session an ID. Following the creation of a new session, the environment is moved to the working directory, where the stdout, stderr, and pid file are written. All stdin are dropped by writting to /dev/null. The umask set allows for the process owner to read/write, and the group to read.

This was otherwise straightforward, however the logging appears to go to stderr instead of stdout for some reason. I am unsure if this is a quark of the logging library or a personal misunderstanding. In any case, both stdout and stderr are written to the same file.

Tested this with both killing from the pid file and graceful shutdown.

@rustaceanrob
rustaceanrob force-pushed the 26-3-9-daemonize branch 3 times, most recently from 5b2974e to 0709d57 Compare March 10, 2026 11:13
`libc` is brought in here to do a couple of calls. First, a `fork` is
used to create a child process. Then, if the process is the parent, the
program exits immediately. If the process is the child, a new session is
made where the child is the group leader, and gives that session an ID.
Following the creation of a new session, the environment is moved to the
working directory, were the `stdout`, `stderr`, and `pid` file are written.
All `stdin` are dropped by writting to `/dev/null`. The `umask` set
allows for the process owner to read/write, and the group to read.

This was otherwise straightforward, however the logging appears to go to
`stderr` instead of `stdout` for some reason. I am unsure if this is a
quark of the logging library or a personal misunderstanding. In any
case, both `stdout` and `stderr` are written to the same file.

ref: https://man7.org/linux/man-pages/man2/fork.2.html
ref: https://man7.org/linux/man-pages/man2/setsid.2.html
ref: https://unix.stackexchange.com/questions/286373/does-the-ubuntu-linux-16-04-daemon-function-execute-a-double-fork
ref: https://thelinuxcode.com/umask-in-linux-default-permissions-bitwise-math-and-real-examples-you-can-trust/
@rustaceanrob
rustaceanrob merged commit 2fbaaa6 into kernel-node:master Mar 11, 2026
1 check passed
@rustaceanrob
rustaceanrob deleted the 26-3-9-daemonize branch March 11, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to run as daemon

1 participant