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); }

In reply to Neverwinter Nights Server Restart by MrCromeDome

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.