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 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
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 ||||||||||||||||||||
72 if [ "x$ARGV" = "x" ] ; then
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"
86 STATUS
="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
90 STATUS
="$EXEC (pid $PID?) not running"
94 STATUS
="$EXEC apparently not running (no pid file)"
106 if [ $RUNNING -eq 1 ]; then
107 echo "$0 $ARG: $EXEC (pid $PID) already running"
110 if eval "$START" ; then
111 echo "$0 $ARG: $EXEC started"
112 # Make sure it stayed up!
115 if [ $RUNNING -eq 0 ]; then
116 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
119 echo "$0 $ARG: $EXEC could not be started"
124 if [ $RUNNING -eq 0 ]; then
125 echo "$0 $ARG: $STATUS"
128 if kill -15 $PID ; then
129 echo "$0 $ARG: $EXEC stopped"
132 if kill -9 $PID ; then
133 echo "$0 $ARG: $EXEC stopped"
135 echo "$0 $ARG: $EXEC could not be stopped"
139 # Make sure it really died!
142 if [ $RUNNING -eq 1 ]; then
143 echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
151 if [ $RUNNING -eq 0 ]; then
152 echo "$0 $ARG: $STATUS"
155 if kill -1 $PID; then
157 echo "$EXEC (PID $PID) reloaded"
159 echo "Can't reload $EXEC"
165 if [ $RUNNING -eq 1 ]; then
175 echo "usage: $0 (start|stop|restart|status|help)"
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