This probably isn't as cool as some of the other things here, but to the Unix and Perl dummies I work with, this is a blessing. What is it, you ask? A script to keep a game (read: Neverwinter Nights) running.
Not that I'm particularly fond of this game, mind you, but it's something to pass the time. And with the regularity that the server part of the game crashes, it's been really hard to pass time playing it, until now.
For those of you who are interested in the nitty gritty, this script works for Linux only and requires that the user be root (don't gripe, I know. . .) or that the user be granted permission to use /sbin/pidof (this can be accomplished by using sudo or something similar). The script looks at a file created by nwserver that contains the PID of the server process. The list of PIDs (well, the last in the list) from the system is checked against the PID in the file, and if there is no match, the server is assumed to be crashed, and we attempt to start it again.
If you run this as a non-root user, you also may wish to have screen installed, otherwise you will have to have a terminal session constantly open to the Linux machine to ensure that the restart script keeps running. The script MUST be installed in the same directory as the nwserver file.
The script is fairly well commented, and any things you might wish to change are clearly commented. Chances are, you will only have to/want to change the time to sleep and the path to your NWN installation.
The script, as presented here, runs nwserver with no options. You may wish to change the following line:
my $server = $path . "nwserver";
to something like this:
my $server = $path . "nwserver -quiet -module Chapter2 -maxclients 16";
Alternatively, you can replace nwserver with a home-rolled shell script that starts nwserver with the options that you use (this is what I do).
If you really want to be cool, you can redirect the output of this script to a file, so you have a nifty log of when your server crashed and when it was restarted:
./nwrestart > nwrestart.log &
As always, I'd love constructive comments and criticisms on how to do things better. Can't learn unless you rip me apart.
Thanks, and have a great localtime() =)
MrCromeDome
#!/usr/bin/perl use strict; use warnings; # Path to your Neverwinter Nights installation: my $path = "/usr/local/games/nwn/"; # Don't change these two lines! my $pid_file = $path . ".nwnpid"; my $server = $path . "nwserver"; # Time to wait (in seconds) before checking to see if the server is st +ill alive: my $delay = 300; while(1) { # Get the PID of the server my $pid = `cat $pid_file`; chomp($pid); # Get the list of PIDs from Unix my $pid_list = `/sbin/pidof nwserver`; chomp($pid_list); # Get the lowest PID in the list $pid_list = substr($pid_list,rindex($pid_list," ") + 1,length($pid_l +ist) - rindex($pid_list, " ")); # If two aren't equal, restart server if($pid ne $pid_list) { my $now = localtime(); print "Restarting server. . . "; system("$server > /dev/null"); print "done (", $now, ")\n"; } # Go to sleep and try again later sleep($delay); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Neverwinter Nights Server Restart
by insensate (Hermit) on Jul 15, 2002 at 21:31 UTC | |
by gav^ (Curate) on Jul 15, 2002 at 21:37 UTC | |
by MrCromeDome (Deacon) on Jul 15, 2002 at 22:01 UTC | |
Re: Neverwinter Nights Server Restart
by coreolyn (Parson) on Jul 15, 2002 at 21:36 UTC | |
by MrCromeDome (Deacon) on Jul 15, 2002 at 22:08 UTC | |
by Anonymous Monk on Dec 17, 2002 at 16:38 UTC | |
by coreolyn (Parson) on Jul 15, 2002 at 21:44 UTC |