### Thread: check if file is running

1. #### check if file is running

greets

i am VERY new to python and have no prior programming experience.so please be patient if my queston seems trivial or impossible.ok,that said,i found a very cool script that i was able to make very minor modifications to,and make a exe out of with py2exe.basicly,what it does is start a game server,then monitors the server window for specific errors.

everything works great,but if the server were to lock or crash,then this script just ends.since its already watchin the server,is there a way i could have it try to restart the server and connect again if it locked/crashed?both the server.exe and the exe for this script are located locally in the same directory on a winxp machine.

Code:
#ServerCheck

from __future__ import division
import os, sys, os.path, string
import cPickle
import getpass
import telnetlib
import time

host = 'x.x.x.x'
port = xxxx
sendThis = 'xxxxxx'

os.system('start path\to\server.exe') #starts the server
time.sleep(5)

def main():
sc = ElectricEye()
sc.run()
class ElectricEye:
def __init__(self):
self.socket={}
pass
def run(self):
while 1:
tn = telnetlib.Telnet()
tn.open(host,port)
tn.write(' ')
while 1:
if len(data)>0:
strstart = "Setup_Start acct='"
strend = "',"
p = data.find(strstart)
if p>-1:
pe = data.find(strend,p+len(strstart))
if pe>-1:
col1=data.find(':')
if col1>-1:
col2=data.find(':',col1+1)
if col2>-1:
col3=data.find(':',col2+1)
if col3>-1:
sockNum=data[col2+1:col3]
acctname=data[p+len(strstart):pe]
self.socket[sockNum]=acctname
print 'memorizing accountname='+acctname+' sockNum='+sockNum
try:
strstart = ":ERROR:"
strend = ":Bad Msg" #00 Eat 4 Bytes"
p = data.find(strstart)
if p>-1:
pe = data.find(strend,p+len(strstart))
if pe>-1:
sockNum=data[p+len(strstart):pe]
acctname=self.socket[sockNum]
print '********************'
print 'ERROR by accountname='+acctname+' sockNum='+sockNum
#get uid
tn.write('account '+acctname+' show lastcharuid\n')
strstart = "'lastcharuid' for '"
strend = "' is '"
p = data.find(strstart)
if p>-1:
pe = data.find(strend,p+len(strstart))
if pe>-1:
acctname=data[p+len(strstart):pe]
val=data[pe+len(strend):]
quotepos = val.find("'")
val=val[:quotepos]
print acctname+' has been disconnected and blocked.'
tn.write('uid.'+val+'.sysmessage '+sendThis+'\n')

#time.sleep(3) #pause for 3 seconds.

tn.write('uid.'+val+'.kick\n')
tn.write ('b ElectricEye blocked account "'+acctname+'" for using an illegal client!\n')
#get last ip
tn.write('/account update\n')
tn.write('account '+acctname+' show lastip\n')
strstart = "'lastip' for '"
strend = "' is '"
p = data.find(strstart)
if p>-1:
pe = data.find(strend,p+len(strstart))
if pe>-1:
acctname=data[p+len(strstart):pe]
val=data[pe+len(strend):]
quotepos = val.find("'")
val=val[:quotepos]
print 'the IP '+acctname+' used was '+val
print '********************'
#change disconnect to remove 1 if you want to delete their character too.
#Disconnect won't work in older versions, but you can send a bad gump msg.
#22:19:170:Setup_Start acct='syrius', char='Syrius'
#22:19:ERROR:170:Bad Msg 00 Eat 4 bytes, prv=02
except KeyError:
pass

tn.close()

if __name__ == '__main__': main()
2. Hi there. I might have an answer for you. It's quite simple in a way.

Simple replace your main by something like this :
Code:
def main():
while True:
try:
sc = ElectricEye()
sc.run()
except:
file = open('somefile.txt','w') #You should change its name ;)
file.write("Exception occured.")
file.write("Type : " + sys.exc_info()[0])
file.write("Value : " + sys.exc_info()[1])
file.write("Traceback : " + sys.exc_info()[2])
file.close()
BEWARE though ! There HAS to be a simpler way : I'm internationally known for using the most complicated of ways to solve problems. Anyway, just try it : it'll put valuable info in text file about the error that occured and then just loop back and restart the server. Good luck !
3. Like your solusion Solar, kinda a built-in error log . Anyway as long as the server doesn't exit with an error, all you really need is the while loop! Which you can put inside or outside the main() function i.e.

Code:
if __name__ == '__main__':
while True: main()
or

Code:
def main():
while True:
sc = ElectricEye()
sc.run()
do exactly the same thing but one is internal to the function and the other isnt . Now that i think about it you can also stick the loop inside our class if you wanted to .

As for actually being able to check if the server class is running you could have a variable which stores the servers status as part of the class? Seems to be the best way to "check if file is running" IMO

The class (and i'm not saying this to have a go at you or anyone else for that matter) just has so many design flaws and there are countless ways to improve the script . Sorry to say it but your probably better of ripping the whole thing down and starting over

Take care guys,
Mark.
4. sorry it took so long for me to reply,but thank you for the help guys!and SoalrBears's error log is very cool!

but unfortunately like netytan said,the script is flawed.it seems the first loop for the telnet loops infinately.so therefore while the script monitors the server,it causes a error which makes it shut down when the server does

so it looks like i'll take netytans advise and try to start over.and no worries netytan,i am not offended.infact,i appreciate your honesty!

just wanted to say thx for the help guys...and keep it up!your efforts are appreciated!

cheers!
5. No problem B, and good luck with the project! If you post your code when your done i'd be very interested to see what you come up with .

A few little tips:

don't use the string modul; i have nothing against it but i see no reason to use it since you can do everything you can with the string module using the string type methods...

make sure whatever you do import gets used, and for a good reason! i.e. you're not using __future__ devision on there at all

Think carefully about your class design if your going to use OOP, the ElectricEye class (aswell as having a very cryptic name) could just as easily (and more efficently) been a function!

We'll hope this helps,

take care,
Mark.