Bash – curl bash script works fine in shell but not work in cronjob

bashcroncurl

I have a script using curl to do the job and set into crontabs.
Things were fine on redhat 7.5+ or CentOS 7.

But in redhat 7.2, the script turned into a junk and never output a thing from the curl results.

OUTPUT=/tmp/apm.log; touch $OUTPUT && truncate -s 0 $OUTPUT
[[ -f tmp/curl.tmp ]] && rm -f /tmp/curl.tmp
curl -s --connect-timeout 2 -XGET "http://www.google.com" -H Content-Type:application/json -d @/tmp/apm.json -o /tmp/curl.tmp
cat /tmp/curl.tmp >> $OUTPUT
echo >> $OUTPUT

Result: the script runs normally under shell, but does not run in cronjob. The /tmp/curl.tmp is generated but the $OUTPUT /tmp/apm.log is size 0.
After trial tests overnight, I found a potential solution.

OUTPUT=/tmp/apm.log; touch $OUTPUT && truncate -s 0 $OUTPUT
[[ -f tmp/curl.tmp ]] && rm -f /tmp/curl.tmp
curl -s --connect-timeout 2 -XGET "http://www.google.com" -H Content- Type:application/json -d @/tmp/apm.json -o /tmp/curl.tmp

while :
do
[ -f /tmp/curl.tmp ] && break
sleep 1
done**

cat /tmp/curl.tmp >> $OUTPUT
echo >> $OUTPUT

I added a while loop after curl to wait for the determination of the output file, following steps are not processed until the file is found. The script then can run normally as expected in cronjob.

It appears that the cron environment does not wait for the curl to finish it's task. So my question is, how can that be fixed ?

Red Hat Enterprise Linux Server 7.2 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.2:GA:server
Kernel: Linux 3.10.0-327.el7.x86_64
Architecture: x86-64

curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.16.2.3 Basic ECC 
zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 
pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL lib

Best Answer

I think it is environment issue, crontab uses /bin/sh as a default shell, sh does not understand double brackets, it is also possiblle that in newer versions of centos /bin/sh replaced by symlink to /bin/bash

Try adding this line in the begining your crontab file: SHELL=/bin/bash

Related Topic