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/184.108.40.206 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
curlcommand. Check your original script.
/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.
/tmp/curl.tmpinstead of one?