Forum Moderators: coopster

Message Too Old, No Replies

Forcing script to ONLY run by cron

         

csdude55

5:48 am on Mar 1, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a script that I run manually through the browser on the first of each month, just after midnight. I don't think it matters, but I use this script to process credit cards.

I'd like to change that to run via cron, and then make it so that it can ONLY run via cron (ie, if I try to run it through the browser it will throw an error). I'm running Linux and WHM/cPanel.

What's the best way to do this? I considered adding this to the PHP script:

if (php_sapi_name() !== 'cli') {
header("Location: https://www.example.com/404.php");
exit;
}

but haven't tested it so I'm not sure if that works. It would give me the option of giving myself a password-encrypted backdoor, though, so I could run it via browser if necessary; something like:

if (php_sapi_name() !== 'cli' && $_GET['backdoor'] !== 'foo') {
header("Location: https://www.example.com/404.php");
exit;
}


I think that an easier option might be to just upload the script to a directory that's not accessible via browser (like /run/script.php), but I haven't tested it either so I don't know for sure that I'll be allowed to access MySQL and mail() that way.

Any thoughts or suggestions?

Wayder

11:11 pm on Mar 15, 2023 (gmt 0)

10+ Year Member Top Contributors Of The Month



I run most of my scripts from directories that are not accessible via the browser and I run some of them by cron as well. No issues with accessing MySQL or sending emails at all.

phranque

11:36 pm on Mar 15, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I think that an easier option might be to just upload the script to a directory that's not accessible via browser

this is what you should do.

you should also change ownership of the script so it matches the group and user that runs cron jobs on your system and then set the permissions to limit execution to owners only.