r15253@catbus: nickm | 2007-09-21 15:03:51 -0400
[tor.git] / contrib / torctl.in
blob4136bd9434e16e4e592fb2f98ba0f9df1710d91c
1 #!/bin/sh
3 # TOR control script designed to allow an easy command line interface
4 # to controlling The Onion Router
6 # The exit codes returned are:
7 # 0 - operation completed successfully. For "status", tor running.
8 # 1 - For "status", tor not running.
9 # 2 - Command not supported
10 # 3 - Could not be started or reloaded
11 # 4 - Could not be stopped
12 # 5 -
13 # 6 -
14 # 7 -
15 # 8 -
17 # When multiple arguments are given, only the error from the _last_
18 # one is reported.
21 # |||||||||||||||||||| START CONFIGURATION SECTION ||||||||||||||||||||
22 # -------------------- --------------------
23 # Name of the executable
24 EXEC=tor
26 # the path to your binary, including options if necessary
27 TORBIN="@BINDIR@/$EXEC"
29 # the path to the configuration file
30 TORCONF="@CONFDIR@/torrc"
32 # the path to your PID file
33 PIDFILE="@LOCALSTATEDIR@/run/tor/tor.pid"
35 # The path to the log file
36 LOGFILE="@LOCALSTATEDIR@/log/tor/tor.log"
38 # The path to the datadirectory
39 TORDATA="@LOCALSTATEDIR@/lib/tor"
41 TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE\" --runasdaemon 1"
42 TORARGS="$TORARGS --datadirectory $TORDATA"
44 # If user and group names are set in the environment, then use them;
45 # otherwise run as the invoking user (or whatever user the config
46 # file says)... unless the invoking user is root. The idea here is to
47 # let an unprivileged user run tor for her own use using this script,
48 # while still providing for it to be used as a system daemon.
49 if [ "x`id -u`" = "x0" ]; then
50 TORUSER=@TORUSER@
51 TORGROUP=@TORGROUP@
54 if [ "x$TORUSER" != "x" ]; then
55 TORARGS="$TORARGS --user $TORUSER"
57 if [ "x$TORGROUP" != "x" ]; then
58 TORARGS="$TORARGS --group $TORGROUP"
61 # We no longer wrap the Tor daemon startup in an su when running as
62 # root, because it's too painful to make the use of su portable.
63 # Just let the daemon set the UID and GID.
64 START="$TORBIN -f $TORCONF $TORARGS"
67 # -------------------- --------------------
68 # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
70 ERROR=0
71 ARGV="$@"
72 if [ "x$ARGV" = "x" ] ; then
73 ARGS="help"
76 checkIfRunning ( ) {
77 # check for pidfile
78 PID=unknown
79 if [ -f $PIDFILE ] ; then
80 PID=`/bin/cat $PIDFILE`
81 if [ "x$PID" != "x" ] ; then
82 if kill -0 $PID 2>/dev/null ; then
83 STATUS="$EXEC (pid $PID) running"
84 RUNNING=1
85 else
86 STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
87 RUNNING=0
89 else
90 STATUS="$EXEC (pid $PID?) not running"
91 RUNNING=0
93 else
94 STATUS="$EXEC apparently not running (no pid file)"
95 RUNNING=0
97 return
100 for ARG in $@ $ARGS
102 checkIfRunning
104 case $ARG in
105 start)
106 if [ $RUNNING -eq 1 ]; then
107 echo "$0 $ARG: $EXEC (pid $PID) already running"
108 continue
110 if eval "$START" ; then
111 echo "$0 $ARG: $EXEC started"
112 # Make sure it stayed up!
113 /bin/sleep 1
114 checkIfRunning
115 if [ $RUNNING -eq 0 ]; then
116 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
118 else
119 echo "$0 $ARG: $EXEC could not be started"
120 ERROR=3
123 stop)
124 if [ $RUNNING -eq 0 ]; then
125 echo "$0 $ARG: $STATUS"
126 continue
128 if kill -15 $PID ; then
129 echo "$0 $ARG: $EXEC stopped"
130 else
131 /bin/sleep 1
132 if kill -9 $PID ; then
133 echo "$0 $ARG: $EXEC stopped"
134 else
135 echo "$0 $ARG: $EXEC could not be stopped"
136 ERROR=4
139 # Make sure it really died!
140 /bin/sleep 1
141 checkIfRunning
142 if [ $RUNNING -eq 1 ]; then
143 echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
144 ERROR=4
147 restart)
148 $0 stop start
150 reload)
151 if [ $RUNNING -eq 0 ]; then
152 echo "$0 $ARG: $STATUS"
153 continue
155 if kill -1 $PID; then
156 /bin/sleep 1
157 echo "$EXEC (PID $PID) reloaded"
158 else
159 echo "Can't reload $EXEC"
160 ERROR=3
163 status)
164 echo $STATUS
165 if [ $RUNNING -eq 1 ]; then
166 ERROR=0
167 else
168 ERROR=1
171 log)
172 cat $LOGFILE
174 help)
175 echo "usage: $0 (start|stop|restart|status|help)"
176 /bin/cat <<EOF
178 start - start $EXEC
179 stop - stop $EXEC
180 restart - stop and restart $EXEC if running or start if not running
181 reload - cause the running process to reinitialize itself
182 status - tell whether $EXEC is running or not
183 log - display the contents of the log file
184 help - this text
187 ERROR=0
190 $0 help
191 ERROR=2
194 esac
196 done
198 exit $ERROR