Search code examples
bashcron

Why does the script launched through Crontab behave differently?


I have .sh script in UbuntuServer (VM):

response=$(curl -H 'Accept: application/vnd.twitchtv.v5+json' -H 'Authorization: OAuth 9f65dd6onr07vhpdqblbiix5rl0tch' -X GET 'https://api.twitch.tv/kraken/streams/127060528' | jq -r '.stream');
now=$(date +"%Y-%m-%d %T");
echo "$now" >> log.txt;
echo "$response" >> log.txt;
if [[ "$response" == "null" ]]
then
  echo "ZERO"
else
  streamlink -o "dump/stream_$now.mp3" twitch.tv/mahetirecords/clip/FreezingEncouragingCougarSuperVinlin audio
  echo "STREAM"
fi

When I run the script through the bash, the THEN-way is triggered. When I run the script through crontab, the ELSE-way is triggered. WHY?

Crontab -e:

* * * * * /home/chesterlife/twitch-interceptor/script.sh

If the stream is offline, then "$response" return null. This is text-null because var=""; if [ "$var" == "$response" ]; then echo "true"; else echo "false"; fi return false

Any ideas?


Solution

  • By default crontab executes using sh shell. You can change it to bash using the information from the link below:

    https://unix.stackexchange.com/questions/94456/how-to-change-cron-shell-sh-to-bash

    Also read this Stack Overflow question about the difference between [ and [[ in Bash

    While [ is POSIX, [[ is only supported by some shells including bash.