0

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
7
  • 1
    There is a typo in your script as shown above. It appears a space between Content- and Type in the curl command. Check your original script. Apr 13, 2019 at 0:49
  • typo fixed. it's not the original cause.
    – Yeats Raw
    Apr 13, 2019 at 4:23
  • Update the system. Apr 13, 2019 at 4:30
  • In the second line your are using tmp/curl.tmp where a / is missing, so the logfile doesn't get deleted. Instead of the first two lines you could simply write OUTPUT=/tmp/apm.log; rm -f $OUTPUT /curl.tmp; touch $OUTPUT.
    – Freddy
    Apr 13, 2019 at 5:20
  • Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?
    – Freddy
    Apr 13, 2019 at 5:26

3 Answers 3

0

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

0

Try below things

[1] Give absolute path for curl

[2] Redirect stdout and error to logs and remove timeout from command By that way you can dig issue better.

[3] Use Shebang With your output I can say curl is not producing any output or having issue with command itself.

Hope it will help.

0

You're probably going to have to assign direct paths to your scripts. Also a shebang to assign the shell to bash would help.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .