Cron not working in the new EC2 instance. If run manually then it's working

I have set up a new EC2 instance. Everything is working perfectly. Now this morning I have set the cron and below is an example of cron

*/5 * * * * /usr/bin/php 

I have tested so many times but my cron is not working. If I hit manually cron url then it’s working. I need to know if I have to set any settings in EC2 instance for running the cron.

Ubuntu version 22.04.3 LTS

Asked By: user9437856


Referring to a PHP script by its URL i.e. https://www... when passing it as an argument to a local interpreter i.e. /usr/bin/php will not work as the interpreter will not read the code in that PHP script but rather the output (HTML/TEXT/JS …etc.) by the web-server serving that script/page … That is if that used interpreter/executable can handle sending and receiving HTTP/S requests which /usr/bin/php might not be able to handle after all and thus neither the web-server will respond nor the interpreter will read anything.

If you want to run the script on the server remotely, use e.g. /bin/curl "https://www...." >/dev/null 2>1 instead.

… therefor, your crontab line should look something like this:

*/5 * * * * /bin/curl "" >/dev/null 2>1

… that should run the target script/page on the target server by simply asking the web-server to serve that page like a web-browser would do … Make sure the URL doesn’t contain any % characters as those will break the line in crontab unless escaped with .

>/dev/null will redirect the output stdout of the command to /dev/null thus discarding it and 2>1 will redirect the error(s) from stderr to stdout and thus discarding them as well … If you don’t do that, you’ll get a lot of error logs from CRON, but your command should run every time nonetheless … It’s a good measure though or you can redirect (appending with >>) both to a custom log file instead of /dev/null if you wish or either like output with >>/path/to/outlog and error with 2>>/path/to/errlog.

Answered By: Raffa
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.