سؤال

with all the kind replies to my last question I have been able to create a small script to check temperatures on my NAS but now I am struggling on sending it to the background. When I try to start my script with

sh temp_check.sh start

I get an error pointing to this line

$0 background &

and I can't figure out why :/ This is my whole script

#!/ffp/bin/sh
#
#check CPU and HDD temperature and shutdown if too hot

#Settings
cpu_high=60
hdd_high=50

NAME="temp_check"
PIDFILE="/var/run/$NAME.pid"

Background() {
   echo $$ >$PIDFILE
   exec >/dev/null 2>&1
   trap "rm -f $PIDFILE ; exit 0" INT TERM EXIT

   while true; do
       STATE=$(hdparm -C /dev/sda1 | grep "drive state" | awk '{print $4}')
       if [ "$STATE" = "active/idle" ]; then
           hdd_temp=$(/usr/local/zy-pkgs/bin/smartctl -A -d marvell /dev/sda | grep 194 | cut -d: -f3 | awk '{print $10}')
           cpu_temp=$(i2cget -y 0x0 0x0a 0x07)
           printf -v cpu_res "%d" "$cpu_temp"
           if [[ $cpu_res -lt $cpu_high && $hdd_temp -lt $hdd_high ]]; then
           #echo "CPU und HDD Temperaturen in Ordnung"
           sleep 30
           else
           halt
           fi
       else
           cpu_temp=$(i2cget -y 0x0 0x0a 0x07)
           printf -v cpu_res "%d" "$cpu_temp"
           if [ $cpu_res -lt $cpu_high ]; then
               #echo "CPU Temperatur in Ordnung - HDD im Standby Modus"
               sleep 30
           else
               halt
           fi
       fi
   done
   }

   case $1 in
       start)
           [ -f $PIDFILE ] && [ -f /proc/` cat $PIDFILE `/cmdline ] && echo "already running.       Aborting." && exit 1
           $0 background &
           echo "Starting $NAME..."
       ;;
       stop)
           kill -9 ` cat $PIDFILE ` >/dev/null 2>&1
           echo "Stopping $NAME ` cat $PIDFILE ` "
       ;;
       status)
           [ -f $PIDFILE ] && [ -f /proc/` cat $PIDFILE `/cmdline ] && echo "running as ` cat $PIDFILE ` " && exit 0
           echo "not running"
       ;;
       background)
           Background
       ;;
       *)
           echo "use $0 [ start | stop | status ]"
       ;;
   esac

Any help is appreciated

Cheers Moritz

هل كانت مفيدة؟

المحلول

One possible error is, that the script is not executable. If this is the case, you can either fix the line to

/bin/sh $0 background &

or make the script executable with

chmod a+x temp_check.sh

نصائح أخرى

maybe it helps, if you change your Background() function to something like

Background()
{ (
...
) &
}

and just call Background in your "start". That's much simpler.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top