diff --git a/relaybot.py b/relaybot.py index d319677..4bd3404 100644 --- a/relaybot.py +++ b/relaybot.py @@ -3,6 +3,7 @@ from twisted.internet import reactor, protocol from twisted.internet.protocol import ReconnectingClientFactory from twisted.python import log from twisted.internet.endpoints import clientFromString +from twisted.application import service from signal import signal, SIGINT import re @@ -11,6 +12,7 @@ import sys log.startLogging(sys.stdout) __version__ = "0.2" +application = service.Application("RelayBot") def main(): host = "localhost" @@ -38,7 +40,6 @@ def main(): reactor.connectTCP(host, port, factory, timeout) reactor.callWhenRunning(signal, SIGINT, handler) - reactor.run() class Communicator: def __init__(self): @@ -157,5 +158,6 @@ class FLIPFactory(RelayFactory): def handler(signum, frame): reactor.stop() -if __name__ == "__main__": +#Main if run as script, builtin for twistd. +if __name__ in ["__main__", "__builtin__"]: main() diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..780ab16 --- /dev/null +++ b/run.sh @@ -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