I have recently completed the Wiki web development tutorial (http://golang.org/doc/articles/wiki/). I had tons of fun and I would like to experiment more with the net/http package.
However, I noticed that when I run the wiki from a console, the wiki takes over the console. If I close the console terminal or stop the process with CTRL+Z then the server stops.
How can I get the server to run in the background? I think the term for that is running in a daemon.
I’m running this on Ubuntu 12.04. Thanks for any help.
Simple / Usable things first
If you want a start script without much effort (i.e. dealing with the process, just having it managed by the system), you could create a
systemdservice. See Greg’s answer for a detailled description on how to do that.Afterwards you can start the service with
Previously I would have recommended trying
xinetdor something similar for finer granuarlity regarding resource and permission management but systemd already covers that.Using the shell
You could start your process like this:
The
&tells the shell to start the command in the background, keeping it in the job list.On some shells, the job is killed if the parent shell exits using the HANGUP signal.
To prevent this, you can launch your command using the
nohupcommand, which discards the HANGUP signal.However, this does not work, if the called process reconnects the HANGUP signal.
To be really sure, you need to remove the process from the shell’s joblist.
For two well known shells this can be achieved as follows:
bash:
zsh:
Killing your background job
Normally, the shell prints the PID of the process, which then can be killed using the
killcommand, to stop the server. If your shell does not print the PID, you can get it usingdirectly after execution. This prints the PID of the forked process.