If the expression does not work in cron

I have the following code: (record.sh)

cd $(dirname $0)

dt=$(date '+%d/%m/%Y %H:%M:%S');
echo $dt;
read action < /home/nfs/sauger/web/pi/action.txt
echo $action;
if [[ $action == *"start"* ]]
then
  echo "start recording"
  ./gone.sh
  exit 1
elif [[ $action == *"stop"* ]]
then
 echo "stop recording"
  ./gone.sh
  exit 1
else 
#More stuff done here
fi

When I run this script manually, the output is as follows:

19/01/2016 19:07:11
start
start recording

If the same script is run via (root) cronjob, the output is as follows:

19/01/2016 19:07:01
start

As you can see, the file "action.txt" was read without problems (the "launch" is logged both times), so this should not be a problem of permissions or wrong paths. But when you perform the cronjob role, the if statement is not called. There is no record "start recording".

So my question is: why does the if statement work when I call the script manually, but not when it is done via cron?

+4
source share
1 answer

script bash; , /bin/sh.

shebang , , (/path/to/script, sh /path/to/script), . :

case $action in
  *start*)
    echo "start recording"
    ./gone.sh
    exit 1
    ;;
  *stop*)
    echo "stop recording"
    ./gone.sh
    exit 1
    ;;
esac
+6

Source: https://habr.com/ru/post/1625008/


All Articles