Tuesday, January 30, 2024

[SOLVED] What are the different ways to enter the cron job command?

Issue

Can anyone please explain me about the different ways of entering cron job command in cPanel?

Some people write the following command:

/usr/local/bin/php /home/username/public_html/cron/index.php

Some write like this:

php -q /home/username/public_html/cron/index.php

And some others write the website url in the command:

curl https://www.example.com/cron/index.php

Now I'm getting confused. I don't know which one to use over the other.


Solution

In general, something like this would be the preferred method:

/usr/local/bin/php /home/username/public_html/cron/index.php

Always use the full path when you can, it will eliminate a lot of issues related to what user is running what shell, and what your PATH is, etc.

This is basically the same thing:

php -q /home/username/public_html/cron/index.php

However, note that this assumes you have php in your PATH -- which may not be true, hence why it's better to always specify the full path. The -q simply means don't output headers, which shouldn't exist for a cron script anyway.

Don't do this:

curl https://www.example.com/cron/index.php

This put completely unnecessary network requirements on the process, such as DNS and HTTP.

Also, and more importantly, don't put your cron scripts in your document root. They should never be accessible from the public web. Anybody in the world can just hit this URL whenever they want, and if that script launches a sensitive or time-crucial process, then you're gonna have a bad time.

Instead, put your cron scripts outside the document root, so they can't possibly be launched from a remote browser. Then invoke them from the CLI:

/usr/local/bin/php /home/username/scripts/whatever.php

You could also use a shebang inside the PHP script, to specifically launch it in the right environment:

#!/usr/local/bin/php
<?php
    // whatever

Then chmod +x the script and then you can just run it by itself:

/path/to/scripts/whatever.php


Answered By - Alex Howansky
Answer Checked By - Robin (WPSolving Admin)