Advertisement

Background Processes

Started by October 28, 2003 06:21 AM
8 comments, last by Evil Bill 21 years ago
I was looking at a thread in this forum earlier (i've forgotten which one now), and it said that a daemon is a program that waits for special events and then runs another program. How would i run e.g. a MUD server in the background? I'd like to be able to start it when the machine boots, and just leave it running (to shut it down i'd connect to the MUD server, and issue a command for it to quit). Would i write a daemon for this, or is there another method? If i do write a daemon, whats this "double fork" i keep hearing about? Apparently i need to call fork() twice, to make sure i get away from the parent process. Eh? This is under RedHat 9 if it makes any difference. Edit: This was the topic: Linux Daemon Cheers, Steve [edited by - Evil Bill on October 28, 2003 8:57:10 AM]
Member of the Unban Mindwipe Society (UMWS)
I''ve been thinking about it for a while now...
What if i just write a program that fork()s once at startup, and returns in the parent fork, and the child fork continues to run, and then if i execute that program at startup? Would that work? I''ll test this when i get home anyway...
Member of the Unban Mindwipe Society (UMWS)
Advertisement
It's apparent that you need to read up more on the fork function. The fork function returns not once, but twice. Once for the child and once for the parent. So if you have like a webserver, you have a parent process that sits there and waits for an accept. If it gets a connection, it forks a child and lets the child take care of the connection and it goes back to listening. just so you know, fork clones the process and starts a new process of its own, so its just like starting a new program. Sounds like you want something like:

pid_t child;while(1){ Accept(...); if((child = fork()) == 0) // This is the child {   Do stuff in the new child process here } This is back to the parent}


For a mud, you'll probably also want to look up TCP protocol, how to do non-blocking IO (look up select and poll functions), and about a million other things.

EDIT: Oh yeah, and to make something run in the background, you could just add it to your cron, or you could run your executable with the & sign after it. Like a.out &.

[edited by - xg0blin on October 28, 2003 9:07:55 AM]
Yeah, i know about sockets, i''ve written several servers under windows.
I know about fork() too, but my explanation is crap

Can i just run a program from the init script using & ? (I totally forgot about using & )

Cheers for the reply,
Steve
Member of the Unban Mindwipe Society (UMWS)
quote: Original post by Evil Bill
Can i just run a program from the init script using & ? (I totally forgot about using & )

Appending & to a command will run it in the background, but it will stop running if you log out. There is a command that will keep it running after you log out, but I can''t remember it at the moment. I thought it was ''nd'', but I don''t think that''s it. Mayne ''nohup''?
quote:
[steve@server steve]$ nohup --help
Usage: /usr/bin/nohup COMMAND [ARG]...
or: /usr/bin/nohup OPTION
Run COMMAND, ignoring hangup signals.

--help display this help and exit
--version output version information and exit

Report bugs to .
[steve@server steve]$


That looks like it, but it doesn't seem to work.

I typed:
quote:
[steve@server MUD]$ nohup `./MUDServer -port 1337`&
[1] 13805
[steve@server MUD]$

But the server exited when i logged off.


Edit:
Do you know if it would work if i added the line to my rc.local file? I don't particularly want to restart my server just to see if it works (my server's in more or less constant use)

Cheers,
Steve

[edited by - Evil Bill on October 28, 2003 4:47:52 PM]
Member of the Unban Mindwipe Society (UMWS)
Advertisement
Ignore all that.
I''d probably help if i ran it as root, huh?
It works perfectly now, thanks! :D
Member of the Unban Mindwipe Society (UMWS)
You''ll probably want to start your server using init scripts at certain run levels. These scripts reside in places like /etc/rc5.d, etc. It is different for each distro. Then you can do things like "/etc/rc5.d/myMudd -stop" to turn it off.

RandomTask
Also take a look at daemon(3)

or

int daemon(int nochdir, int noclose);

that is.
Cool, thanks
Member of the Unban Mindwipe Society (UMWS)

This topic is closed to new replies.

Advertisement