Advertisement

running in background

Started by February 27, 2005 08:00 AM
5 comments, last by GameDev.net 19 years, 7 months ago
Im writing a simple testing game server, and I would like to know how can i detach from terminal and go to background. right now Im developing in linux, but I would like to use some portable method in case i need to port to windows.
I dunno for sure how to do it in background, i.e by detaching yaself from the the terminal (you run in background with &, but I guess you know that), however, I know one program that does it : wget. With the -b option, it simply starts running in the background even if you close your session, the terminal, or simply unlog (is that an english term???) from the server, so, if i was you, Id look at its source code.
Latest Stable Wget

Although I wouldnt know for sure of a way to do it portable, aka, that would work on win32, maybe a solution would be to run it as a service... (but obviously, this would be a win32-only case, not an all-platform solution)
Advertisement
The simplest method is to use 'screen' to get a new console, run your server, then detach with Ctrl+D:

# screen
# ./test_server
# ^A^D

You can get back to the console later if you like with:

# screen -r


This is obviously not portable, but writing a portable service will probably take some work.
Use nohup to do it manually, or handle SIGHUP (there's a few more details to it, clicky) to do it programmatically.
Although you'll have to handle this in an entierly differnt way under Windows. Perhaps there's some portable daemon library around?
For a simple way, look at wget sources, likely, for Linux, check out Line 463 (method fork_to_background()) of utils.c
For win32, check out Line 137 of mswindows.c

Both are found under folder wget-1.9/src

Good luck
There is the daemon() function. I once used it to write a clone of nohup that does not create an output file.
Advertisement
Yup, the daemon function is probably the easiest way.
http://www.hmug.org/man/3/daemon.html

This topic is closed to new replies.

Advertisement