Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Neverwinter Nights Server Restart

by MrCromeDome (Deacon)
on Jul 15, 2002 at 20:54 UTC ( [id://181916]=CUFP: print w/replies, xml ) Need Help??

While I debated earlier over where to post this script, I ended up posting it in CUFP as opposed to the other places I discussed. Ironic, no?

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
    Instead of using pidof and having a root requirement, why not just parse the output of a
    ps aux | grep nwserver
    (the pid is in the second column of the output, the options to ps may be different on your linux flavor) but that may help a bit.
    -Jason
      Or you could look at Proc::ProcessTable which makes this easy (and a bit more portable)...

      gav^

      Excellent suggestion. ++insensate. I hate having root requirements for something like this, and it seems I missed one of the more obvious ways to avoid it this time ;) Thanks for pointing it out to me.

      MrCromeDome

Re: Neverwinter Nights Server Restart
by coreolyn (Parson) on Jul 15, 2002 at 21:36 UTC

    Execellent... another NWN Perl dude *g*

    I noticed a TCL script floating around that hits the backgrounded server with via Expect I've been looking for time to wrap a Perl interface around that (TCL::Expect), but this will do in the mean time.

    Hate to sound dumb but I'm not understanding the comparison of:

    # 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)

    NOTE: To those who may know a better way, this script or one resembling it will probably get wide circulation and would be highly appreciated by many.

    coreolyn -- There be ton's of Perl work waiting in NWN :)
      At the risk of going a bit too far OT here:

      The above script has already been submitted to the BioWare community. I hope it's making the rounds already.

      The next project involves a web based server administration interface in Perl. Like you said. . . lots of Perl work waiting in NWN ;) Until the content creation tools (and server stability issues) get resolved, I'm going to stick to writing Perl utilities to help manage the game better.

      Thanks for your interest =) I'd be interested in chatting more about this with you. /msg me again sometime =)

      MrCromeDome

        *************************************** [root@never nwn]# more nwstart-ebro.pl #!/usr/bin/perl $nwpath="/usr/local/nwn"; #if [ -z $1 ]; then # echo "you did not give me a module to launch." # echo "usage: $0 MODULENAME" # exit #ifi #MODULE=$1 #while(1){} $pid_file = $nwpath . "/.nwnpid"; unless(open (PIDS, "<$pid_file")){ print "Informational: no pidfile found at $pid_file\n"; system("cd $nwpath"); system("./nwserver -module Yourmod -maxclients 60 -min +level 1 -maxlevel 20 -pauseandplay 0 -pvp 2 -servervault 0 -elc 0 -ilr 0 -gam +etype role play -oneparty 0 -difficulty 3 -autosaveinterval 0 -playerpassword you +rpass -dmp assword fryme -servername yourserver -publicserver 0 -reloadwhenempty +0 -qui et &"); } while (<PIDS>) { chomp; $line=$_; print "pid line is: $line\n"; if ($line eq "0"){ print "nwn may have failed to start up earlier, trying + again...\ n"; } if ($line ne "0") { $arg=$ARGV[0]; if ($arg eq "-f"){ #`rm -f $pid_file`; system("cd $nwpath"); system("./nwserver -module Yourmod -maxclients + 60 -minlevel 1 -maxlevel 20 -pauseandplay 0 -pvp 2 -servervault 0 -elc 0 -ilr 0 -gam +etype role play -oneparty 0 -difficulty 3 -autosaveinterval 0 -playerpassword you +rpass -dmp assword fryme -servername yourserver -publicserver 0 -reloadwhenempty +0 -qui et &"); }else{ print "nwn is already running! PID=$line\n"; } } else{ `cd $nwpath`; $output=`./nwserver -module Yourmod -maxclients 60 -mi +nlevel 1 -maxlevel 20 -pauseandplay 0 -pvp 2 -servervault 0 -elc 0 -ilr 0 -gam +etype role play -oneparty 0 -difficulty 3 -autosaveinterval 0 -playerpassword you +rpass -dmp assword fryme -servername yourserver -publicserver 0 -reloadwhenempty +0 -qui et &`; print "$output\n"; } } close PIDS; *************************************** [root@never nwn]# more nwkeepon-ebro.pl #!/usr/bin/perl # http://www.perlmonks.org/index.pl?node_id=181916 # 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. # This calls nwstart.sh, but that script, too, seems a # bit faulty, as does nwstop.pl; they both give errors # unless the server is in just the right state. use strict; use warnings; # Path to your Neverwinter Nights installation: my $path = "/usr/local/nwn/"; # Don't change these two lines! my $pid_file = $path . ".nwnpid"; my $startpid = "nwstart"; my $server = $path . "./nwstart-ebro.pl -f &"; # Time to wait (in seconds) before checking to see if the server is st +ill alive: my $delay = 30; #my $pid = `cat $pid_file`; #my $pid_list = ""; #my $start_list = ""; while(1) { my $status=open (PIDS, "<$pid_file"); if (!$status){ print "no pid file found, server was previously stoppe +d graceful ly, no action required\n"; } else{ # Get the list of PIDs from Unix # check to see if the start script is already running... my $start_list = ` ps -ewf | grep nwstart | grep -v grep `; #print "start list is $start_list\n"; if ($start_list=~$startpid){ print "Start script is currently running, no action required\n +"; } else{ # Get the PID of the server my $pid = `cat $pid_file`; chomp($pid); # Get the list of PIDs from Unix #my $pid_list = `ps -ef | grep nwserver | grep -v grep | cut -d" " +-f6`; my $pid_list=`ps -eo "%c %p" | grep $pid | cut -d" " -f2-`; chomp($pid_list); # print "debug:pidlist: $pid_list\n"; # Get the lowest PID in the list $pid_list = substr($pid_list,rindex($pid_list," ") + 1,length($pid_ +list) - rindex($pid_list, " ")); # print "debug:pidlist:formatted: $pid_list\n"; # If two aren't equal, restart server if(($pid ne $pid_list) ) { if ($pid eq "0"){ print "PID is set to zero please check server and star +t manually \n"; }else{ print "pids do not match: pid: $pid pidlist: $pid_list\n"; my $now = localtime(); print "Restarting server. . . "; system("$server > /dev/null"); print "done (", $now, ")\n"; }}else{ print "server alive, going to sleep for $delay seconds\n"; print "pid: $pid pidlist: $pid_list\n"; } } } close PIDS; # Go to sleep and try again later sleep($delay); } ****************************************** root@never nwn]# more nwstop-ebro.pl #!/usr/bin/perl # Path to your Neverwinter Nights installation: $path = "/usr/local/nwn/"; $pid_file = $path . ".nwnpid"; open (PIDS, "$pid_file") or die "nwn is not running or there is no pid +file at $p id_file\n"; while (<PIDS>) { chomp; if ($_ ne "0") { print "Killing pid $_\n"; system("kill", "-9", "$_"); } } #$pid = `cat $pid_file`; #chomp($pid); #print "my pid was $pid\n"; #print "my pid_file was $pid_file\n"; #system("kill", "-9", "$pid"); system("rm", "$pid_file");

      ack nevervmind.. just looked at the output of $pid and $pidlist (never worked with pidof).. I get it.

      coreolyn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://181916]
Approved by coreolyn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found