Aller au contenu

Photo

Linux boot script question?


  • Veuillez vous connecter pour répondre
48 réponses à ce sujet

#26
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
I cant get it to fire and my linux friend wont be in until friday.Could you post what you made work without the nwnx2 Skildron? I can copy it and paste my module name in it.Then if it doenst work for me I will know to trouble shoot something else .

#27
Skildron

Skildron
  • Members
  • 231 messages
This is the nwservctl.sh script I use:

#!/bin/bash
#nwservctl.sh

#control command for nwn server, requires empty-expect

# **** BASIC CONFIGURATION ****

# Num of log directory to keep
LOG_HIST_NUM=9

# Seconds to wait for server loading
SECONDS_TO_WAIT=60

#Default Countdown in seconds
COUNTDOWN=60

# nwserver command: nwserver or nwnstartup.sh for NWNX2
#NWSERVER_COMMAND="nwnstartup.sh" #NWNX2
NWSERVER_COMMAND="nwserver -module Samaris" #Basic nwserver
FIRST_EXIT_PHRASE="Stopping server in"
SECOND_EXIT_PHRASE="seconds please logout..."

# output passfrase to wait for module loaded
#PASS_PHRASE="0x08203e79" # NWNX2 init if you use nwnstartup.sh
PASS_PHRASE="Module loaded" # Basic nwserver

# **** END BASIC CONFIGURATION ****

# **** ADVANCED CONFIGURATION ******
# ONLY FOR EXPERTS

# start server command
NWSERVER_START="empty -f -i ./in.fifo -o ./out.fifo ./$NWSERVER_COMMAND"

# wait for loaded command
WAIT_LOADED="empty -w -t $SECONDS_TO_WAIT -i ./out.fifo -o ./in.fifo $PASS_PHRASE" # FOR nwnstartup.sh

# return output from server command
# use a timeout until all commands are debugged
CATCH_OUTPUT="empty -r -t 30 -b 8192 -i ./out.fifo"

# first line argument
ARG1="$1"

# exit code
EXCODE=0

# *** CODE ***

#check if arguments exist
if [ "x$ARG1" = "x" ] || [ "x$ARG1" = "xusage" ] || [ "x$ARG1" = "x--help" ]; then
echo "Usage: `basename $0` start | restart [countdown [n]]" >&2
echo " `basename $0` stop [countdown [n]] | kill | status" >&2
echo " `basename $0` info [pid | pcpu | etime | pmem | vsz ] | --help" >&2
echo " `basename $0` <nwsever commands> if server is running" >&2
exit 1
fi

# check if nwserver is running exit if command is not
if [ "$(pidof nwserver)" ] ; then
if [ "x$ARG1" = "xstart" ] ; then
echo "`basename $0` - nwserver already running..."
exit 1
else
: # do nothing continue
fi
else
if [ "x$ARG1" = "xstart" ] ; then
: # do nothing continue
else
echo "`basename $0` - nwserver is not running..."
exit 1
fi
fi

case $ARG1 in
start)
# rotate logs
[ -d ./logs ] || mkdir -p ./logs
if [ -d "./logs/logs.$LOG_HIST_NUM" ]; then
rm -f -r "./logs/logs.$LOG_HIST_NUM"
fi
for ((ddest=$LOG_HIST_NUM; ddest >= 2 ; ddest--)); do
let "dsourc = $ddest - 1"
if [ -d "./logs/logs.$dsourc" ]; then
mv -f "./logs/logs.$dsourc" "./logs/logs.$ddest"
fi
done

#move log.0 in log.1
mv -f ./logs.0 ./logs/logs.1
mkdir ./logs.0
#end rotate logs

shift
NWSERVER_START="$NWSERVER_START $*"
$NWSERVER_START
echo "Starting nwserver please wait.."
sleep 5
$WAIT_LOADED

if [ "$(pidof nwserver)" ]
then
echo "nwserver is running now"
else
echo "nwserver loading failed.."
EXCODE=1
fi
;;
stop|restart)
if [ "x$2" = "xcountdown" ]; then
if [ "$3" -gt 0 ]; then
COUNTDOWN="$3"
fi
while [ "$COUNTDOWN" -gt 0 ]; do
$0 "say $FIRST_EXIT_PHRASE $COUNTDOWN $SECOND_EXIT_PHRASE"
let "COUNTDOWN -= 10"
sleep 10
done
fi
echo "exit" | empty -s -o ./in.fifo
$CATCH_OUTPUT
if [ "x$1" = "xrestart" ]; then
echo "restarting in 10 seconds please wait..."
sleep 10
$0 "start"
fi
;;
info)
case "$2" in
pcpu|etime|pmem|vsz)
ps -p "$(pidof nwserver)" -o "$2="
;;
pid)
echo "$(pidof nwserver)"
;;
*)
ps -p "$(pidof nwserver)" -o pid,comm,etime,pmem,vsz,pcpu
;;
esac
;;
kill)
empty -k "$(pidof nwserver)"
;;
# with return output
status|help|listbans)
echo "$ARG1" | empty -s -o ./in.fifo
$CATCH_OUTPUT
;;
# no return output
playerpassword|dmpassword|adminpassword|oneparty|ilr|elc|difficulty|servername|autosaveinterval|pauseandplay|minlevel|maxlevel|maxclients|export|kick)
echo "$*" | empty -s -o ./in.fifo
#no output to catch
;;
#all other commands default send to nwserver
*)
echo "$*" | empty -s -o ./in.fifo
$CATCH_OUTPUT
;;
esac

exit $EXCODE


I start it manually from the directory it resides in with
./nwservctl.sh start
Works fine for me.

Greetings

Skildron

#28
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
Woot ! ! ! After my linux friend beat me vigirously with a bamboo stick:pinched::blink: for using my Vista note pad ,he then copied your posted script  again in linux and installed it.
It works great.


Thanks again Skildron 

#29
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
Hey Skil, since we're talking bout the script here, maybe you can follow this.....


So you have the wait_loaded line like so:

SECONDS_TO_WAIT=120
PASS_RESPONSE="playerpassword\\n" # Basic nwserver
PASS_PHRASE="0x08203e79" # NWNX2 init if you use nwnstartup.sh
WAIT_LOADED="empty -w -t $SECONDS_TO_WAIT -i out.fifo -o in.fifo $PASS_PHRASE $PASS_RESPONSE"


So it waits for the nwnx init phrase, but I was expecting the -t to delay the command by that number of seconds, but according to the man page it simply is the amount of time to wait for an appropriate response. Have a better idea on how to automate the removal of the password after more than a basic delay?


I've been mulling over something like the man page suggests....

A better way to send the password to the supervised program is to read
it from file:

empty -s [common options] <./password-file



#30
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
Double Post

Modifié par Calvinthesneak, 13 mars 2011 - 10:06 .


#31
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
And apparently getting a post in while they try to maintain site is a bad idea, it makes them triplicate.

Modifié par Calvinthesneak, 13 mars 2011 - 10:07 .


#32
Skildron

Skildron
  • Members
  • 231 messages
Hey there, Calvin!

I'm not sure what you are trying to do. Since I don't use nwnx2, I don't no how to start it, but AFAIKS there is no place in the startup process to offer a player password. After nwserver started, you can provide one with

./nwservctl.sh playerpassword password

But this will not log you into the server as player, but set the password you need to log into the server with the graphical multiplayer client.

Perhaps, you could explain a little more what you are trying to do, then I may be able to help.

Greetings
Skildron

#33
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
Running standalone server from linux box. What I'm trying to do is when it starts up via the script is let the server start and remove the password after about 5 minutes or so.

with or without nwnx the process for the bash script doesn't change much, empty expect simply looks for text to appear, either it waits for "module loaded" string or if you're running in nwnx, it just lists the memory loc for the nwnx INIT function which is that fixed string of "0x08203e79"

Either way they are both strings which signal that it should provide a response. That response it provides immediately on recieving the keyphrase. Right now I set it to reply with PASS_RESPONSE="playerpassword\\n".

So basically this just sends the playerpassword command to the nwserver and clears the password. I spose what I really will have to do is make the sleep between the server load and the password removal longer, or just leave it the way I had it running.

#34
Skildron

Skildron
  • Members
  • 231 messages
You will probably not have any grace time - as soon as empty finds the pass phrase in the out.fifo, it sends the pass response to the in.fifo. The -t switch only tells empty the amount of time to wait for the pass phrase before giving up with a time out - otherwise, empty would wait forever if the server program does not send the expected pass phrase.

But, anyhow, what you want to do should work the way you try. I suspect the \\n closing you playerpassword statement to be superfluous. There is no instance of echo or printf involved that would be able to evaluate the newline command.

If that does not work in any way, you could modify the script around line 100 in the following way:

$WAIT_LOADED
if [ "$(pidof nwserver)" ]
then
echo "nwserver is running now"
sleep 300
echo "playerpassword" | empty -s -o ./in.fifo
else
echo "nwserver loading failed.."
EXCODE=1
fi


That would, of course, add 5 to the time the script stays active after the success message.

HTH
Greetings
Skildron

#35
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
Hmmm I did it a little different Skil.

Changed the WAIT_LOADED command to:

[quote]WAIT_LOADED="empty -s -o in.fifo $PASS_RESPONSE" [/quote]

then I went and changed the text in the same area as you, but a bit differently.

[quote]
shift
NWSERVER_START="$NWSERVER_START $*"
$NWSERVER_START
echo "Starting nwserver please wait.."
sleep 5


if [ "$(pidof nwserver)" ]
then
echo "nwserver is running now"
else
echo "nwserver loading failed.."
EXCODE=1
fi
echo "waiting 3 mins for password before removing"
sleep 180
echo "removing password"
$WAIT_LOADED
;;
stop|restart)

Modifié par Calvinthesneak, 13 mars 2011 - 11:11 .


#36
Skildron

Skildron
  • Members
  • 231 messages
Just out of curiosity: Why would you want to remove the player password for your server? And if you absolutely don't need a password, why don't you remove it in the nwnplayer.ini file?

Greetings
Skildron

#37
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
You want to start it up so players can't log in while it's loading. Then wish players to freely log in once it's loaded and had a chance to run initialization routines and such in the module.

#38
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
Hey guys ...did you notice. The script works great for just reloading nwserver program unless the computer itself reboots .I have to manually delete the in.fifo and out.fifo files from my directory after the computer reboots for the script to work again.I was wondering should I somehow have these files deleted on system start up?

#39
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
You shouldn't ever need to create or delete the fifo files. I made that mistake the very first time. It should run all on it's own, soon as the script terminates the files should self delete. Even if they didn't delete, the fifos should be empty, and just resume feeding buffer commands.

#40
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
empty -f will not work with precreated fifo files so nwserver never starts. I could not test with nwnx2 because I don't have it, but with plain nwserver, everything works and you can communicate with nwserver via the nwservctl.sh script. Bear in mind, though, that lots of server commands are set up in a way so that they don't give output on the console. I don't know wether nwserver would create output on those commands, but the script only catches output for status, help and listbans. All other commands stay silent, even if they normally would give output.



Yes script works great until PC crashes leaving the in.fifo and out.fifo in the directory since nwserver didnt shut down via the nwservctl.sh which deletes these.What are my choices ?delete these files sytem startup?

#41
Skildron

Skildron
  • Members
  • 231 messages
Deleting the files on startup if they exist would be a good choice - I personally would add some commands at the very beginning of nwservctl.sh to that effect, e.g.

test -f ./in.fifo && rm -f in.fifo
test -f ./out.fifo && rm -f out.fifo


That should take care of your problems. Another question is why your server crashes. My own linux server only reboots if I update the kernel - but I don't run nwserver on this machine, so I don't know if it would crash the system. Linux usually is quite stable, except if your hardware is faulty/unstable. That's a condition Linux abhors.

Greetings
Skildron

#42
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
My other linux server rarely crashed ,this one is a virtual machine.Only crashes when Im not around to delete the fifo files lol.Hows this look ,I added this to my cron tab.....

@reboot /home/dan/test -f ./in.fifo && rm -f in.fifo
@reboot /home/dan/test -f ./out.fifo && rm -f out.fifo

#43
Skildron

Skildron
  • Members
  • 231 messages
This probably won't work, unless you really have a test program in /home/dan. I think you meant to do something like this:

@reboot /usr/bin/test -f /home/dan/in.fifo && rm -f home/dan/in.fifo


Replace /home/dan with the path to your nwserver installation.

Greetings
Skildron

#44
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
I'm still somewhat confused here.

I can run any nwserver console command via the ./nwservctl.sh (nwserver command).

ie ./nwservctl.sh information

I don't know about changing directories.... but I tend to start a new terminal window if I need to do something else. I'll test a bit later on......

#45
Skildron

Skildron
  • Members
  • 231 messages
Yes, of course, if you are already in your nwserver directory, then you can do what you need to do with ./nwservctl.sh, but knight_shield wants crond to remove stale fifo files on reboot. As crond does not know about your nwn dirs and usually not even about system paths if they are not given in the crontab, you have to enter the command with full pathnames to work properly.

Greetings
Skildron

#46
Calvinthesneak

Calvinthesneak
  • Members
  • 656 messages
The odd part is I cannot have the shell script run if I create my own files using mkfifo. The process automatically makes it's own fifo files which it deletes as soon as it is terminated.

I suppose the root of my confusion is how he can get the process to run with creating his own. Crontab shouldn't have to do anything like that. I simply use crontab to lauch a process monitor to see if the process is hanging at 100% CPU usage every 5 minutes. I always provide full pathing in linux by habit, more secure and robust to do so.

Since the script is a forked process, even if it terminates abnormally, it should still be deleting the fifo files. Reading a bit more carefully it seems the crux of the problem is a reboot of the virtual machine. Maybe his idea is right in that case, still seems odd. I've had great performance from the script.

#47
Skildron

Skildron
  • Members
  • 231 messages

Calvinthesneak wrote...

The odd part is I cannot have the shell script run if I create my own files using mkfifo. The process automatically makes it's own fifo files which it deletes as soon as it is terminated.


Yes, that's right, empty refuses to work if the fifos it shall use are already present - it wants to create the fifos on its own.

I suppose the root of my confusion is how he can get the process to run with creating his own. Crontab shouldn't have to do anything like that. I simply use crontab to lauch a process monitor to see if the process is hanging at 100% CPU usage every 5 minutes. I always provide full pathing in linux by habit, more secure and robust to do so.


On a hard server crash that leads to a cold reboot, empty has no chance to remove its fifo files because no process is terminated normally - in fact, no process is terminated at all. That leaves the hard disc in an unclean state, too.

Since the script is a forked process, even if it terminates abnormally, it should still be deleting the fifo files. Reading a bit more carefully it seems the crux of the problem is a reboot of the virtual machine. Maybe his idea is right in that case, still seems odd. I've had great performance from the script.


Yes, the script is a streak of genius, and if the script terminates abnormally, its child processes like empty would of course be terminated by init. But if the server (the actual machine, not the nwserver executable) hangs? If I understood correctly, that's why knight shield wants crontab to remove the stale fifo files - so that on the following startup, nwservctl.sh runs properly and is not prevented from doing so by preexisting fifo files.

Greetings
Skildron

#48
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
Ok I did it ..... here is what I put in crontab

@reboot ~/rm -f in.fifo
@reboot ~/rm -f out.fifo

#49
Knight_Shield

Knight_Shield
  • Members
  • 444 messages
Just a note ..I changed pass phrase to ( Running... )because it was taking to long for(  Module loaded )after computer reboot for script to work properly.My mod has grown.

Modifié par Knight_Shield, 30 mai 2011 - 05:31 .