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
17 # When multiple arguments are given, only the error from the _last_
21 # |||||||||||||||||||| START CONFIGURATION SECTION ||||||||||||||||||||
22 # -------------------- --------------------
23 # Name of the executable
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
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 ||||||||||||||||||||
68 if [ "x$ARGV" = "x" ] ; then
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"
82 STATUS
="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
86 STATUS
="$EXEC (pid $PID?) not running"
90 STATUS
="$EXEC apparently not running (no pid file)"
102 if [ $RUNNING -eq 1 ]; then
103 echo "$0 $ARG: $EXEC (pid $PID) already running"
106 if eval "$START" ; then
107 echo "$0 $ARG: $EXEC started"
108 # Make sure it stayed up!
111 if [ $RUNNING -eq 0 ]; then
112 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
115 echo "$0 $ARG: $EXEC could not be started"
120 if [ $RUNNING -eq 0 ]; then
121 echo "$0 $ARG: $STATUS"
124 if kill -15 $PID ; then
125 echo "$0 $ARG: $EXEC stopped"
128 if kill -9 $PID ; then
129 echo "$0 $ARG: $EXEC stopped"
131 echo "$0 $ARG: $EXEC could not be stopped"
135 # Make sure it really died!
138 if [ $RUNNING -eq 1 ]; then
139 echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
147 if [ $RUNNING -eq 0 ]; then
148 echo "$0 $ARG: $STATUS"
151 if kill -1 $PID; then
153 echo "$EXEC (PID $PID) reloaded"
155 echo "Can't reload $EXEC"
161 if [ $RUNNING -eq 1 ]; then
171 echo "usage: $0 (start|stop|restart|status|help)"
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