curl bash script works fine in shell but not work in cronjob Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Come Celebrate our 10 Year Anniversary!Bash script works from prompt but not from cronbash shell command runs fine at command prompt, but not in my .sh script?Why doesn't this command work as a cronjob, while it works perfectly when typed in a shell?expect script works fine standalone not from bash scriptExecuting script remotely with “curl | bash” feedbackUbuntu 10.04 cronjob with curl dosen't workCURL & SSL failing on a specific sitebash scripts work fine when I run it from command line but not from cronshell script not working in cronjobCentOS 7: curl and wget not working

Can the Great Weapon Master feat's damage bonus and accuracy penalty apply to attacks from the Spiritual Weapon spell?

How come Sam didn't become Lord of Horn Hill?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Illegal assignment from sObject to Id

Maximum summed subsequences with non-adjacent items

Chinese Seal on silk painting - what does it mean?

How do living politicians protect their readily obtainable signatures from misuse?

What is the difference between globalisation and imperialism?

What would you call this weird metallic apparatus that allows you to lift people?

Why do we bend a book to keep it straight?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Performance gap between vector<bool> and array

Using audio cues to encourage good posture

Should I follow up with an employee I believe overracted to a mistake I made?

What was the first language to use conditional keywords?

Sum letters are not two different

How were pictures turned from film to a big picture in a picture frame before digital scanning?

As a beginner, should I get a Squier Strat with a SSS config or a HSS?

Do any jurisdictions seriously consider reclassifying social media websites as publishers?

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Morning, Afternoon, Night Kanji

Why is the AVR GCC compiler using a full `CALL` even though I have set the `-mshort-calls` flag?

How does the math work when buying airline miles?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Come Celebrate our 10 Year Anniversary!Bash script works from prompt but not from cronbash shell command runs fine at command prompt, but not in my .sh script?Why doesn't this command work as a cronjob, while it works perfectly when typed in a shell?expect script works fine standalone not from bash scriptExecuting script remotely with “curl | bash” feedbackUbuntu 10.04 cronjob with curl dosen't workCURL & SSL failing on a specific sitebash scripts work fine when I run it from command line but not from cronshell script not working in cronjobCentOS 7: curl and wget not working



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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









share|improve this question









New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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.

    – Michael Hampton
    Apr 13 at 0:49











  • typo fixed. it's not the original cause.

    – Yeats Raw
    Apr 13 at 4:23











  • Update the system.

    – Michael Hampton
    Apr 13 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 at 5:20












  • Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?

    – Freddy
    Apr 13 at 5:26


















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









share|improve this question









New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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.

    – Michael Hampton
    Apr 13 at 0:49











  • typo fixed. it's not the original cause.

    – Yeats Raw
    Apr 13 at 4:23











  • Update the system.

    – Michael Hampton
    Apr 13 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 at 5:20












  • Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?

    – Freddy
    Apr 13 at 5:26














0












0








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









share|improve this question









New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






bash cron curl






share|improve this question









New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 13 at 4:23







Yeats Raw













New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 12 at 23:39









Yeats RawYeats Raw

11




11




New contributor




Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Yeats Raw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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.

    – Michael Hampton
    Apr 13 at 0:49











  • typo fixed. it's not the original cause.

    – Yeats Raw
    Apr 13 at 4:23











  • Update the system.

    – Michael Hampton
    Apr 13 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 at 5:20












  • Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?

    – Freddy
    Apr 13 at 5:26













  • 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.

    – Michael Hampton
    Apr 13 at 0:49











  • typo fixed. it's not the original cause.

    – Yeats Raw
    Apr 13 at 4:23











  • Update the system.

    – Michael Hampton
    Apr 13 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 at 5:20












  • Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?

    – Freddy
    Apr 13 at 5:26








1




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.

– Michael Hampton
Apr 13 at 0:49





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.

– Michael Hampton
Apr 13 at 0:49













typo fixed. it's not the original cause.

– Yeats Raw
Apr 13 at 4:23





typo fixed. it's not the original cause.

– Yeats Raw
Apr 13 at 4:23













Update the system.

– Michael Hampton
Apr 13 at 4:30





Update the system.

– Michael Hampton
Apr 13 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 at 5:20






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 at 5:20














Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?

– Freddy
Apr 13 at 5:26






Why do you use two files /tmp/apm.log and /tmp/curl.tmp instead of one?

– Freddy
Apr 13 at 5:26











1 Answer
1






active

oldest

votes


















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






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "2"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Yeats Raw is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f962873%2fcurl-bash-script-works-fine-in-shell-but-not-work-in-cronjob%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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






    share|improve this answer



























      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






      share|improve this answer

























        0












        0








        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






        share|improve this 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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 15 at 3:37









        badbukabadbuka

        11




        11




















            Yeats Raw is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Yeats Raw is a new contributor. Be nice, and check out our Code of Conduct.












            Yeats Raw is a new contributor. Be nice, and check out our Code of Conduct.











            Yeats Raw is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Server Fault!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f962873%2fcurl-bash-script-works-fine-in-shell-but-not-work-in-cronjob%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company