Skip to content

Commit fdfa3b8

Browse files
committed
Support for and script to make it easier to run with twistd.
1 parent 3a5018e commit fdfa3b8

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

relaybot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from twisted.internet.protocol import ReconnectingClientFactory
44
from twisted.python import log
55
from twisted.internet.endpoints import clientFromString
6+
from twisted.application import service
67
from signal import signal, SIGINT
78
import re
89

@@ -11,6 +12,7 @@
1112
log.startLogging(sys.stdout)
1213

1314
__version__ = "0.2"
15+
application = service.Application("RelayBot")
1416

1517
def main():
1618
host = "localhost"
@@ -38,7 +40,6 @@ def main():
3840
reactor.connectTCP(host, port, factory, timeout)
3941

4042
reactor.callWhenRunning(signal, SIGINT, handler)
41-
reactor.run()
4243

4344
class Communicator:
4445
def __init__(self):
@@ -157,5 +158,6 @@ class FLIPFactory(RelayFactory):
157158
def handler(signum, frame):
158159
reactor.stop()
159160

160-
if __name__ == "__main__":
161+
#Main if run as script, builtin for twistd.
162+
if __name__ in ["__main__", "__builtin__"]:
161163
main()

run.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
if [ "X`id -u`" = "X0" -a -z "$RUN_AS_USER" ]
3+
then
4+
echo "Do not run this script as root."
5+
exit 1
6+
fi
7+
8+
start() {
9+
twistd --python=relaybot.py --logfile=relaybot.log --pidfile=relaybot.pid
10+
}
11+
12+
stop() {
13+
kill `cat relaybot.pid`
14+
}
15+
16+
case "$1" in
17+
'start')
18+
start
19+
;;
20+
21+
'stop')
22+
stop
23+
;;
24+
25+
'restart')
26+
stop
27+
start
28+
;;
29+
30+
'status')
31+
tail -F relaybot.log
32+
;;
33+
34+
*)
35+
echo "Usage: $0 { start | stop | restart | status }"
36+
exit 1
37+
;;
38+
39+
esac
40+
41+
exit 0

0 commit comments

Comments
 (0)