Update: Commenters have pointed out a few things:
- This post is incomplete/incorrect. What I’m doing now is having the
daemonfunction call a script that looks like this:
#!/bin/bash exec 1>&- exec 2>&- exec 3>&- nohup myPropApp & 2>&1 > thelog.txt
That code was from another website who’s URL I lost, and I posted the solution below based on another, alternate method that I hadn’t tried but sounded simpler.
- There are other options, like
setsid(1), and the bash builtin
disown(which I had prematurely rejected as ksh-only).
Back when I was using Debian, one of the nicer things about it was their helper tool for startup scripts:
start-stop-daemon. Particularly, it’s ability to daemonize any process with the
-b flag. You notice how handy things like that end up being when you’ve got an in-house or otherwise proprietary app that can’t daemonize itself properly (e.g. Java-based services).
Somehow I’ve managed to get away with not having to write a script that daemonizes a normally-foreground process on an RH-based distribution yet, mainly because I’ve been using Debian almost exclusively for servers, and have only worked for tiny startups, where luxuries like init scripts are the last thing on anyones’ minds.
Everyone is familiar with the
nohup & trick, but that still leaves it associated to a terminal, so after you log out, your terminal/ssh session will just hang because stdin is still open. As it turns out, you can close your standard in from bash first by redirecting your standard input from nil (e.g.
someapp <&-), and that will let it just work.
Very sweet for writing initscripts.