Linux boot script question?
#26
Posté 08 mars 2011 - 04:12
#27
Posté 08 mars 2011 - 11:05
#!/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 startWorks fine for me.
Greetings
Skildron
#28
Posté 11 mars 2011 - 07:13
It works great.
Thanks again Skildron
#29
Posté 13 mars 2011 - 04:56
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
Posté 13 mars 2011 - 05:01
Modifié par Calvinthesneak, 13 mars 2011 - 10:06 .
#31
Posté 13 mars 2011 - 05:37
Modifié par Calvinthesneak, 13 mars 2011 - 10:07 .
#32
Posté 13 mars 2011 - 10:23
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
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../nwservctl.sh playerpassword password
Perhaps, you could explain a little more what you are trying to do, then I may be able to help.
Greetings
Skildron
#33
Posté 13 mars 2011 - 10:40
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
Posté 13 mars 2011 - 03:19
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
Posté 13 mars 2011 - 11:10
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
Posté 13 mars 2011 - 11:31
Greetings
Skildron
#37
Posté 14 mars 2011 - 02:31
#38
Posté 16 mars 2011 - 08:34
#39
Posté 17 mars 2011 - 12:05
#40
Posté 07 avril 2011 - 03:27
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
Posté 07 avril 2011 - 09:39
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
Posté 07 avril 2011 - 10:34
@reboot /home/dan/test -f ./in.fifo && rm -f in.fifo
@reboot /home/dan/test -f ./out.fifo && rm -f out.fifo
#43
Posté 08 avril 2011 - 10:51
@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
Posté 10 avril 2011 - 09:13
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
Posté 11 avril 2011 - 07:08
Greetings
Skildron
#46
Posté 11 avril 2011 - 05:50
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
Posté 11 avril 2011 - 09:35
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
Posté 11 avril 2011 - 09:52
@reboot ~/rm -f in.fifo
@reboot ~/rm -f out.fifo
#49
Posté 30 mai 2011 - 05:30
Modifié par Knight_Shield, 30 mai 2011 - 05:31 .





Retour en haut







