fix service command for cron jobs

the cron environment usually does not contain $PATH. This fixes cases
where users do not use full paths in their crontabs.

fixes https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/issues/120#issuecomment-364028147
This commit is contained in:
Stuart Cardall 2018-02-08 09:44:58 +00:00
parent 0ebdfec103
commit c3624d2d89
No known key found for this signature in database
GPG key ID: AEB857F1C891D0C6

View file

@ -140,15 +140,24 @@ update_paths() {
service_cmd() { service_cmd() {
# arch linux does not have a 'service' command # arch linux does not have a 'service' command
local svc= svc_list="service systemctl rc-service" local x= svc= svc_list="service systemctl rc-service"
for svc in $svc_list; do for x in $svc_list; do
svc=$(which $svc 2>/dev/null) svc=$(which $svc 2>/dev/null)
if [ -n "$svc" ]; then if [ -n "$svc" ]; then
echo $svc case "$x" in
exit 0 service) svc="$svc nginx reload";;
systemctl) svc="$svc reload nginx.service";;
rc-service) svc="$svc nginx reload";;
esac
break
else
# centos does not have 'which' by default
svc="/usr/sbin/service nginx reload"
fi fi
done done
echo $svc
} }
sanitize_path() { sanitize_path() {
@ -259,8 +268,7 @@ get_options() {
main() { main() {
local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master
local file=globalblacklist.conf remote_dir=conf.d url= output= update= status= tmp= retval= local file=globalblacklist.conf remote_dir=conf.d url= output= update= status= tmp= retval=
# default to service (centos does not have 'which' by default) local reload_service=$(service_cmd)
local service=${service_cmd:-"service"}
# require root # require root
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
@ -307,7 +315,7 @@ main() {
# re-read nginx configuration # re-read nginx configuration
if [ $retval = 0 ]; then if [ $retval = 0 ]; then
$service nginx reload 2>&1 >/dev/null $reload_service 2>&1 >/dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
status="${BOLDGREEN}[OK]${RESET}" status="${BOLDGREEN}[OK]${RESET}"
print_message "\nReloading NGINX configuration...$status\n" print_message "\nReloading NGINX configuration...$status\n"