Author Topic: Server restarter to rule them all!  (Read 2197 times)

0 Members and 1 Guest are viewing this topic.

Offline mar77a

  • Global Moderator
  • Veteran
  • *****
  • Posts: 1295
  • mad
    • random stuffs
Server restarter to rule them all!
« on: November 27, 2007, 03:19:07 pm »
Written in python, uses UDP to ping the server. Thanks to EnEsCe for the insight in the soldat client/lobby protocol. Just set up the config vars and run.

Setting it up:
ip: server's ip
port: server's join port
interval: min time in seconds between check
run_exec: command to execute if the server is dead

Code: [Select]
#!/usr/bin/python
import os
import time
import socket
import struct
import sys

#CONFIG
ip = "outcry.u13.net"
port = 99999
interval = 5
run_exec = "nohup ./soldatserver > /dev/null &"
#END

def check_alive(ip,port):
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.connect((ip,int(port)))
tv = struct.pack('ii', int(5), int((5-int(5))*1e6))
sock.setsockopt(socket.SOL_SOCKET,socket.SO_RCVTIMEO,tv)
ping_p = "i" + ""
sock.send(ping_p,0x100);
recv = sock.recvfrom(6,0)
buff = recv[0]
return len(buff)
except:
return 0

print "Keeping "+ip+":"+str(port)+" alive.\nOut:",
while 1:
if check_alive(ip,int(port)) > 0:
print "0"
else:
print "1_"
os.system(run_exec)
print "R"
time.sleep(interval)

Or download from: http://alb.u13.net/~mar77a/py/sc.py

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: Server restarter to rule them all!
« Reply #1 on: November 28, 2007, 11:10:33 am »
Well, I guess I can stop working on the ruby one of these I was making that was going to do the exact same thing...

details on the lobby ping can be found here: http://devs.soldat.pl/wiki/index.php/Lobby_ping

This mechanism exists because it is how soldat clients "ping" all of the servers in the lobby list

edit: wanna make it so that you can also run it like ./keepalive --ip=u13.net --port=23073 --run="./soldatserver > /dev/null 2>&1 &disown" ?

If you don't feel like parsing the args manually, there are libraries out there (for just about every major scripting/programming language) that will do it for you, especially handy for more complex formats like --option=value
« Last Edit: November 28, 2007, 11:14:18 am by FliesLikeABrick »

Offline {[MS]}Pyro

  • Major(1)
  • Posts: 20
Re: Server restarter to rule them all!
« Reply #2 on: November 29, 2007, 02:48:50 am »
Might sound silly, but how do you start it? In debian.
« Last Edit: November 29, 2007, 02:58:16 am by {[MS]}Pyro »


Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: Server restarter to rule them all!
« Reply #4 on: November 29, 2007, 04:39:02 am »
perl ./whateveryounamedit.pl


*buzzer sound*

chmod +x whateveryounamedit.py then ./whateveryounamedit.py

It is a python script, and it contains the interpreter definition at the beginning of the file with #!, so you don't need to explicitly call python to execute it.

Offline mar77a

  • Global Moderator
  • Veteran
  • *****
  • Posts: 1295
  • mad
    • random stuffs
Re: Server restarter to rule them all!
« Reply #5 on: November 29, 2007, 07:18:57 am »
Also, I suggest you use the script to actually start the server; read: run the script only and it'll take care of running the server.

1. Download and edit the config variables
2. chmod +x sc.py
2. nohup ./sc.py > /dev/null &

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: Server restarter to rule them all!
« Reply #6 on: November 29, 2007, 09:33:43 am »
Also, I suggest you use the script to actually start the server; read: run the script only and it'll take care of running the server.

1. Download and edit the config variables
2. chmod +x sc.py
2. nohup ./sc.py > /dev/null &

Why use it to start the server if it won't stop the server when it closes?

Offline mar77a

  • Global Moderator
  • Veteran
  • *****
  • Posts: 1295
  • mad
    • random stuffs
Re: Server restarter to rule them all!
« Reply #7 on: November 29, 2007, 09:41:48 am »
It will  :)

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: Server restarter to rule them all!
« Reply #8 on: November 29, 2007, 02:42:51 pm »
Aha, right.  I somehow overlooked the fact that you were using &, I thought you were using &disown

Offline Atomaffe

  • Major(1)
  • Posts: 1
Re: Server restarter to rule them all!
« Reply #9 on: January 17, 2008, 09:19:42 am »
Is this a script to restart the server automaticaly if it crash??

what i have to do?!

1. Download and edit the config variables
2. chmod +x sc.py
2. nohup ./sc.py > /dev/null &

could i also start it with ./sc.py -d ??
« Last Edit: January 17, 2008, 09:56:02 am by Atomaffe »

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: Server restarter to rule them all!
« Reply #10 on: January 17, 2008, 06:17:33 pm »
Is this a script to restart the server automaticaly if it crash??

what i have to do?!

1. Download and edit the config variables
2. chmod +x sc.py
2. nohup ./sc.py > /dev/null &

could i also start it with ./sc.py -d ??

yes, that is what it is.

you need to start it as mar77a said.  The way it is written now, using -d is not going to work