Support for and script to make it easier to run with twistd.

This commit is contained in:
Steve Dougherty 2012-03-02 22:16:43 -05:00
parent 3a5018ec1a
commit fdfa3b8a9d
2 changed files with 45 additions and 2 deletions

View File

@ -3,6 +3,7 @@ from twisted.internet import reactor, protocol
from twisted.internet.protocol import ReconnectingClientFactory from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python import log from twisted.python import log
from twisted.internet.endpoints import clientFromString from twisted.internet.endpoints import clientFromString
from twisted.application import service
from signal import signal, SIGINT from signal import signal, SIGINT
import re import re
@ -11,6 +12,7 @@ import sys
log.startLogging(sys.stdout) log.startLogging(sys.stdout)
__version__ = "0.2" __version__ = "0.2"
application = service.Application("RelayBot")
def main(): def main():
host = "localhost" host = "localhost"
@ -38,7 +40,6 @@ def main():
reactor.connectTCP(host, port, factory, timeout) reactor.connectTCP(host, port, factory, timeout)
reactor.callWhenRunning(signal, SIGINT, handler) reactor.callWhenRunning(signal, SIGINT, handler)
reactor.run()
class Communicator: class Communicator:
def __init__(self): def __init__(self):
@ -157,5 +158,6 @@ class FLIPFactory(RelayFactory):
def handler(signum, frame): def handler(signum, frame):
reactor.stop() reactor.stop()
if __name__ == "__main__": #Main if run as script, builtin for twistd.
if __name__ in ["__main__", "__builtin__"]:
main() main()

41
run.sh Executable file
View File

@ -0,0 +1,41 @@
#!/bin/bash
if [ "X`id -u`" = "X0" -a -z "$RUN_AS_USER" ]
then
echo "Do not run this script as root."
exit 1
fi
start() {
twistd --python=relaybot.py --logfile=relaybot.log --pidfile=relaybot.pid
}
stop() {
kill `cat relaybot.pid`
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
tail -F relaybot.log
;;
*)
echo "Usage: $0 { start | stop | restart | status }"
exit 1
;;
esac
exit 0