I'm having trouble in finding the reason why my BASH script behaves differently if run alone or inside cron
. I have this snippet:
#!/bin/bash
RESPONSE=$(curl -fs -XPOST -H "Content-type: application/json" -d '{"id" : 4}' https://myserver.com)
echo $RESPONSE
if [ -z "$RESPONSE" ]; then
echo "empty response"
return 0
fi
COMMAND=$(echo $RESPONSE | python -c "import sys, json; print json.load(sys.stdin)['command']")
if [ -z "$COMMAND" ]; then
echo "empty command"
elif [ "$COMMAND" = "SYS_INFO" ];
then
#business logic
fi
that prints two different responses in the two environments:
$RESPONSE Running from console:
{"id":"1f78d8d0-e754-4a23-a2f0-448fbeb42995", "key":"\n4RHDFAnTull1Z+aHGbO1zXcAGghuaEUz0w8sT7dlpc80jG6ZaWnbDox4G0f8sKY\ng0WZ80zWf8ftNgX3nes9MWYEq00nM5jJWCSavmGSKCKjoGD2XqBod8W0Z5w/KAHTSitGVMFgMjda91+xozw8uMlzR/t3Y8FP2k/NHj\n"}
$RESPONSE Running from :
{"id":"1f78d8d0-e754-4a23-a2f0-448fbeb42995", "key":"
4RHDFAnTull1Z+aHGbO1zXcAGghuaEUz0w8sT7dlpc80jG6ZaWnbDox4G0f8sKYj
g0WZ80zWf8ftNgX3nes9MWYEq00nM5jJWCSavmGSKCKjoGD2XqBod8W0Z5w/KAHTSitGVMFgMjda91+xozw8uMlzR/t3Y8FP2k/NHj
"}
Please notice the \n
that the server returns into key
field that are present when running from console and NOT present (actually, they are encoded as newline) when running from crontab
What I've tried:
- adding source ~/.bashrc as suggested here
- changing value of PATH evaluating the differences of the two environments as suggested here
However, nothing seems to work.