1

I have a bash script named testScript.sh that looks like this :

#!/bin/bash
curl -X GET https://www.example.com -o ~/Desktop/testFile.json
curl -X POST -d ~/Desktop/testFile.json http://www.example2.com

i want to run this script with crontab so i edited crontab file with crontab -e command like that :

* * * * * ~/Desktop/testScript.sh

The weird part is that when i run the script manually like ./testScript.sh with pi user, both curl commands execute just fine. When the script runs from crontab i see the testFile created so the first curl command execute, but the curl Post is not executed. I have already did my research and most of the people say it's environment variables but i don't seem to understand any of the answers.

EDIT* I followed @roaima suggestion to make a log file. Logs : ....Couldn't read data from file testFile.json", this make an empty Warning : post ....... the curl post then is giving me a 400 bad request because there is not content to post. 1) the testFile.json is created with the first curl command, i see it locally on my machine, i don't know why it cannot read it. 2) the script still runs fine if i run it like that./testScript.sh

7
  • How do you know that the second command is not executed? Could it have run but failed? Modify your crontab entry to capture stdout and stderr by appending >> output.log 2>&1. Then change your script so the first line reads #!/bin/bash -x. Wait one minute and then look in the output.log for in your HOME directory. Sep 27, 2018 at 7:35
  • Are you getting any errors emailed to you by the cron daemon relating to this?
    – Kusalananda
    Sep 27, 2018 at 7:35
  • I have a raspberry pi that it's not mine so i dont know the email @Kusalananda
    – Thanos M
    Sep 27, 2018 at 7:44
  • log file : "Couldn't read data from file testFile.json", this make an empty Warning : post ....... the curl post then is giving me a 400 bad request because there is not content to post. Two things to point out : 1) the testFile.json is created i don't know why it cannot read it , 2) the script runs fine if i run it like that ./testScript.sh @roaima
    – Thanos M
    Sep 27, 2018 at 7:53
  • 2
    If you want curl to send the contents of a file, and not just the filename, you would need -d @filename but tilde ~ will not be expanded so use -d @$HOME/Desktop/testFile.json.
    – meuh
    Sep 27, 2018 at 8:38

1 Answer 1

1

The answer to my problem was given out by @meuh

If you want curl to send the contents of a file, and not just the filename, you would need -d @filename but tilde ~ will not be expanded so use -d @$HOME/Desktop/testFile.json

You must log in to answer this question.

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