issue with bash when running in crontab

Viewed 35

Hope somebody can point me in the right direction in solving this problem. I have this bash with if statement.

checkPID=(MySQL queries to count columns)

if [[ $checkPID -eq 1 ]]
then
echo "PID already exist, running update queries instead"

(MySQL update queries here)

else

echo "PID does not exist, running insert queries"

(MySQL insert queries here)

fi

When I run this bash script on the command line everything works as expected, but when I automate this via crontab it doesn't follow the if condition regardless of the value of the checkPID variable.

1 Answers

Your crontab line uses dash (/bin/sh) instead of bash.

*/10 * * * * sh /home/servo/scripts/slavereport.sh –
change to:
*/10 * * * * bash /home/servo/scripts/slavereport.sh –

Since you are using bash operations, remember to put #!/bin/bash as the first line in the script.

Fixing either of these should also resolve the issue. It isn't required to do both.

Related