0

my script refuses to work under cron, but works fine while executing manually

#!/bin/bash
LOGFILE=/opt/xxx/scripts/rc.log
fUpMail() {
echo -e "Hello!\n\n$1 xx\n\nBest regards,\n\nCheck LLC 2k18" | mailx -s "$1 Rates were not imported" [email protected] 
}
curDate=`date +%Y-%m-%d`
#postgres expression output being assigned to a variable
rateQ=`PGPASSWORD=xxxxxx psql -t -h xxx.xxx.228.134 -p 5433 -d axx2 -U axxx2bo << EOF
SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'
EOF`
#same for demodb
rateDemo=`PGPASSWORD=xxx psql -t -h xx.xxx.42.14 -p 5432 -d axxxo -U acxxxxbo << EOF
SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'
EOF`
#logging
printf "\n`date +%H:%M:%S` $curDate $rateQ $rateDemo\n" >> $LOGFILE

#check if rate value is not null
if [[ $(($rateQ)) != 0 ]] && [[ $(($rateDemo)) != 0 ]];
then
#posting a commentary into jira
curl -u xxx-support-bot:Rzq-xxx-xxx-gch -X POST --data '{"body": "'"$rateQ"' LIVE rates for '"$curDate"' were imported automatically'"\n"''"$rateDemo"' DEMO rates for '"$curDate"' were imported automatically"}' -H "Content-type: application/json" https://jira.in.xxx.com:443/rest/api/2/issue/xxxxxx-1024/comment >> $LOGFILE
else
#if rates were not imported
if [[ $(($rateQ)) == 0 ]];
then
echo "looks like LIVE rates for $curDate were not imported, please check manually!"
#sending a letter
fUpMail 'LIVE'
fi
if [[ $(($rateDemo)) == 0 ]];
then
echo "looks like DEMO rates for $curDate were not imported, please check manually!"
fUpMail 'DEMO'
fi
fi

cron sends following message:

/opt/xxx/scripts/ratecheck.sh: line 25: Timing is on.
  6543

Time: 4.555 ms: syntax error: invalid arithmetic operator (error token is ".
  6543

Time: 4.555 ms")

line 25 is

if [[ $(($rateQ)) != 0 ]] && [[ $(($rateDemo)) != 0 ]];

Could please someone help explaining what's wrong here?

3
  • I can't tell from what's here, but it looks like there's unexpected content (possibly including carriage return characters) in $rateQ and/or $rateDemo. Try adding set -x at the beginning of the script so it'll print the equivalent of the commands as it executes them. Also, are you executing it with the sh command (which will override the shebang line), and do you have the PATH set so it can find all the executables it needs (by default, cron jobs run with a minimal PATH)? Commented Nov 30, 2018 at 1:26
  • Try putting your curl command on a single line. And what's with the $(($rateDemo)) anyway? You're not doing any math, so why not just use if [[ $rateDemo == 0 ]];? Commented Nov 30, 2018 at 3:42
  • @GordonDavisson cron job is next: SHELL=/bin/bash 20 22 * * 1-5 /opt/alphacapitallive/scripts/ratecheck.sh shouldn't it be enough? manually executed script with -x option + rateQ=' 6387' ... + rateDemo=' 6382' I have concerns about these spaces, but why does it work fine manually then? :( @miken32 curl command is a single line in fact :) These brackets are a relic in fact. Formerly these $rate variables were of a string type. And brackets were used to convert it. Thanks for mentioning, got rid of them. Commented Nov 30, 2018 at 15:15

1 Answer 1

1

You're getting more than a plain number back from psql and this is interfering with the type conversion you're doing. I think you can remove the extra output like this:

rateQ=$(PGPASSWORD=xxxxxx psql -t -h xxx.xxx.228.134 -p 5433 -d axx2 -U axxx2bo -q -c "SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'")

rateDemo=$(PGPASSWORD=xxx psql -t -h xx.xxx.42.14 -p 5432 -d axxxo -U acxxxxbo -q -c "SELECT COUNT(id) FROM quote WHERE f_date = '$curDate'")

Note the addition of the -q flag:

-q --quiet

Specifies that psql should do its work quietly. By default, it prints welcome messages and various informational output. If this option is used, none of this happens. This is useful with the -c option. This is equivalent to setting the variable QUIET to on.

https://www.postgresql.org/docs/9.0/app-psql.html

I also replaced your old-fashioned backticks with $() and put the SQL query into an argument.

If that doesn’t silence the additional output, you may also need to edit ~/.psqlrc for the user running the cron job and ensure there is no \timing line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.