change Bytes to B to fix ticket 1195.
[tor.git] / contrib / torctl.in
blob4cc137da46d6c724ed352adad1a40642cfaedecf
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 name is set in the environment, then use it;
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@
53 if [ "x$TORUSER" != "x" ]; then
54 TORARGS="$TORARGS --user $TORUSER"
57 # We no longer wrap the Tor daemon startup in an su when running as
58 # root, because it's too painful to make the use of su portable.
59 # Just let the daemon set the UID and GID.
60 START="$TORBIN -f $TORCONF $TORARGS"
63 # -------------------- --------------------
64 # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
66 ERROR=0
67 ARGV="$@"
68 if [ "x$ARGV" = "x" ] ; then
69 ARGS="help"
72 checkIfRunning ( ) {
73 # check for pidfile
74 PID=unknown
75 if [ -f $PIDFILE ] ; then
76 PID=`/bin/cat $PIDFILE`
77 if [ "x$PID" != "x" ] ; then
78 if kill -0 $PID 2>/dev/null ; then
79 STATUS="$EXEC (pid $PID) running"
80 RUNNING=1
81 else
82 STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
83 RUNNING=0
85 else
86 STATUS="$EXEC (pid $PID?) not running"
87 RUNNING=0
89 else
90 STATUS="$EXEC apparently not running (no pid file)"
91 RUNNING=0
93 return
96 for ARG in $@ $ARGS
98 checkIfRunning
100 case $ARG in
101 start)
102 if [ $RUNNING -eq 1 ]; then
103 echo "$0 $ARG: $EXEC (pid $PID) already running"
104 continue
106 if eval "$START" ; then
107 echo "$0 $ARG: $EXEC started"
108 # Make sure it stayed up!
109 /bin/sleep 1
110 checkIfRunning
111 if [ $RUNNING -eq 0 ]; then
112 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
114 else
115 echo "$0 $ARG: $EXEC could not be started"
116 ERROR=3
119 stop)
120 if [ $RUNNING -eq 0 ]; then
121 echo "$0 $ARG: $STATUS"
122 continue
124 if kill -15 $PID ; then
125 echo "$0 $ARG: $EXEC stopped"
126 else
127 /bin/sleep 1
128 if kill -9 $PID ; then
129 echo "$0 $ARG: $EXEC stopped"
130 else
131 echo "$0 $ARG: $EXEC could not be stopped"
132 ERROR=4
135 # Make sure it really died!
136 /bin/sleep 1
137 checkIfRunning
138 if [ $RUNNING -eq 1 ]; then
139 echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
140 ERROR=4
143 restart)
144 $0 stop start
146 reload)
147 if [ $RUNNING -eq 0 ]; then
148 echo "$0 $ARG: $STATUS"
149 continue
151 if kill -1 $PID; then
152 /bin/sleep 1
153 echo "$EXEC (PID $PID) reloaded"
154 else
155 echo "Can't reload $EXEC"
156 ERROR=3
159 status)
160 echo $STATUS
161 if [ $RUNNING -eq 1 ]; then
162 ERROR=0
163 else
164 ERROR=1
167 log)
168 cat $LOGFILE
170 help)
171 echo "usage: $0 (start|stop|restart|status|help)"
172 /bin/cat <<EOF
174 start - start $EXEC
175 stop - stop $EXEC
176 restart - stop and restart $EXEC if running or start if not running
177 reload - cause the running process to reinitialize itself
178 status - tell whether $EXEC is running or not
179 log - display the contents of the log file
180 help - this text
183 ERROR=0
186 $0 help
187 ERROR=2
190 esac
192 done
194 exit $ERROR