View difference between Paste ID: 7gT3xAfb and UGTkBdWn
SHOW: | | - or go back to the newest paste.
1
#!/bin/bash
2
# rTorrent Stop/Start/Restart
3
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
4
FILE="$HOME/rtorrent/.session/rtorrent.lock"
5
SERVICE='rtorrent'
6-
RTPID=$(pidof $SERVICE)
6+
RTPID=$(pgrep -fx -u $LOGNAME $SERVICE)
7
# Sets default status can be askkill, autokill, or donotkill
8
KILLSTATUS='askkill'
9
KILLSIGNAL='-15'
10
RTLOG="/dev/null"
11
loglines=500
12
useerr=0
13
LOGFLAG=0
14
15
# write log file
16
log_header() {
17
RTLOG="$HOME/rt.log"
18
echo >> $RTLOG
19
date >> $RTLOG
20
echo "$0 $@" >> $RTLOG
21
if [ $(cat "$RTLOG" | wc -l) -gt $loglines ]
22
  then
23
    tail -$loglines  "$RTLOG" > $HOME/rt.temp 
24
    cat $HOME/rt.temp > $RTLOG
25
    rm -f $HOME/rt.temp
26
fi
27
}
28
29
# function to check if service is running
30
service_running(){
31-
if pidof $SERVICE > /dev/null
31+
if pgrep -fx -u $LOGNAME $SERVICE > /dev/null
32
  then
33
    return 0
34
  else
35
    return 1
36
fi
37
}
38
39
# function to ask user for y/n response
40
ask_user(){
41
if [ -t 1 ]
42
  then
43
    while true
44
      do
45
      read -p "Unable to close $SERVICE, close using kill -9? " answer
46
      case $answer in [Yy]* ) return 0 ;;
47
                      [Nn]* ) return 1 ;;
48
                          * ) echo "Enter y or n";;
49
      esac
50
    done
51
  else
52
    echo "Running in background -a auto response is n" >> $RTLOG
53
    return 1
54
fi
55
}
56
57
# function that starts service
58
start_service(){
59
  local seconds=0
60
  if [ $SERVICE = "rtorrent" ]
61
    then
62
      if [ -a $FILE ]
63
        then
64
          echo "removing $FILE" | tee -a "$RTLOG"
65
          rm -f $FILE
66
        else
67
          echo "No session lock file" | tee -a "$RTLOG"
68
      fi
69
  fi
70
71
  echo -n "Starting $SERVICE"
72
  screen -d -m -S $SERVICE $SERVICE
73
74
  while ! (service_running)
75
    do
76
      seconds=$(( $seconds + 1 ))
77
      echo -n "."
78
      sleep 1
79
      if [ $seconds = 10 ]
80
        then
81
          echo
82
          return 1
83
      fi
84
  done
85
86
  echo
87
  return 0
88
}
89
90
# function that stops service
91
stop_service(){
92
 local seconds=0
93
 echo -n "$SERVICE shutting down using kill $1"
94
 kill $1 $RTPID
95
96
 while ( service_running )
97
   do
98
     seconds=$(( $seconds + 1 ))
99
     echo -n " ."
100
     sleep 1
101
     if [ $seconds = 20 ]
102
       then 
103
         echo
104
         return 1           
105
     fi
106
   done
107
108
   echo
109
   return 0      
110
}
111
112
# reports any usage error
113
usage_error() {
114
echo "usage: [-h] [-a|-k|-d] [-l] [start | stop | restart]" | tee -a "$RTLOG"
115
if [ $1 = 1 ]
116
  then
117
    echo "only one of the options -a -k -d allowed" | tee -a "$RTLOG"
118
fi
119
120
if [ $1 = 2 ]
121
  then
122
    echo "incorrect option used" | tee -a "$RTLOG"
123
fi
124
125
if [ $1 = 3 ]
126
  then
127
    echo "incorrect argument used" | tee -a "$RTLOG"
128
fi
129
130
if [ $1 = 4 ]
131
  then
132
    echo "too many arguments, only 1 argument allowed" | tee -a "$RTLOG"
133
fi
134
135
136
exit 1 
137
}
138
139
# prints usage help to screen
140
usage_help() {
141
echo "usage: [-h] [-a|-k|-d] [-l] [start | stop | restart]"
142
echo "options:"
143
echo "  -h usage help display"
144
echo "  -l send results to log file"
145
echo "  -a if unable to close service ask user whether to use kill -9"
146
echo "  -k if unable to close service use kill -9"
147
echo "  -d if unable to close service do not use kill -9"
148
echo "arguments:"
149
echo "  start starts the service"
150
echo "  stop stops the service"
151
echo "  restart first stops and then starts the service"
152
153
exit 0
154
}
155
156
157
# Check for options
158
while getopts ":hlkad" optname
159
  do
160
    case $optname in
161
      "k" )[[ -n $OPTFLAG ]]  && useerr=1 || KILLSTATUS='autokill' && OPTFLAG=1 ;;
162
      "a" )[[ -n $OPTFLAG ]]  && useerr=1 || KILLSTATUS='askkill' && OPTFLAG=1 ;;
163
      "d" )[[ -n $OPTFLAG ]]  && useerr=1 || KILLSTATUS='donotkill' && OPTFLAG=1 ;;
164
      "l" ) LOGFLAG=1 ;;
165
      "h" ) usage_help ;;
166
        * ) useerr=2 ;;
167
    esac
168
  done
169
170
if [ $LOGFLAG = 1 ]
171
  then
172
    log_header "$@"
173
fi
174
175
if [ $useerr -gt 0 ] 
176
  then
177
    usage_error $useerr
178
fi
179
180
shift $(( $OPTIND - 1 ))
181
182
# Check correct arguments used
183
if [ $# = 1 ] 
184
  then
185
    if ! [[ $1 = "stop" || $1 = "start" || $1 = "restart"  ]] 
186
      then
187
        useerr=3
188
        usage_error $useerr
189
    fi
190
fi
191
192
# Check if there is more than 1 argument
193
if [ $# -gt 1 ]
194
  then
195
    useerr=4
196
    usage_error $useerr
197
fi
198
199
# Check if service is running
200
if [ $# = 0 ]
201
  then
202
    if ( service_running )
203
      then
204
        echo "$SERVICE is running"
205
      else
206
        echo "$SERVICE is NOT running"
207
    fi
208
fi
209
210
# Stop Service
211
if [[ $1 = "stop" || $1 = "restart" ]]
212
  then
213
    if ! ( service_running )
214
      then
215
        echo "$SERVICE was not running"
216
      else
217
        if ! ( stop_service $KILLSIGNAL ) 
218
          then 
219
            case $KILLSTATUS in
220
              "autokill" )
221
                KILLSIGNAL='-9'
222
                stop_service $KILLSIGNAL ;;
223
              "askkill" )
224
                if ( ask_user )
225
                  then
226
                    KILLSIGNAL='-9'
227
                    stop_service $KILLSIGNAL
228
                fi ;;
229
            esac
230
        fi
231
232
        if ( service_running )
233
          then
234
            echo "WARNING unable to close $SERVICE using $KILLSIGNAL" | tee -a "$RTLOG"
235
            exit 1
236
          else
237
            echo "$SERVICE has been closed using kill $KILLSIGNAL" | tee -a "$RTLOG"
238
        fi
239
    fi     
240
fi
241
242
# Start Service
243
if [[ $1 = "start" || $1 = "restart" ]]
244
  then
245
    if ( service_running )
246
      then
247
        echo "$SERVICE was already running"
248
      else
249
        if ( start_service )
250
          then
251
            echo "$SERVICE has been started" | tee -a "$RTLOG"
252
          else
253
            echo "WARNING: Unable to start $SERVICE" | tee -a "$RTLOG"
254
        fi
255
    fi
256
fi 
257
258
echo "There were no problems encountered" >> "$RTLOG"