Leo Jiang
Leo Jiang

Reputation: 26085

How can I only allow cron to be run on a PHP file during a certain time

I have a cron job that runs several times a day at full hours (:00). How can I only allow the PHP scripts to run during this time? I don't want someone else to be able to run my script. Here's what I thought of:

if (date('i', time()) > 2 || date('i', time()) < 58) {
  die;
}

Are there better, more secure ways?

Upvotes: 0

Views: 661

Answers (2)

ghbarratt
ghbarratt

Reputation: 11711

The cron jobs are usually run as the root user. You could make your script executable and readable by root only:

sudo chown root script_for_root_only.php
sudo chmod 744 script_for_root_only.php

You can also alter the command in the crontab to run the script as a special user if you do not want to use root..

I think we can assume that you do not have the script in a web accessible location. If you did then move it.

Upvotes: 0

jeroen
jeroen

Reputation: 91734

If you place your php script outside of your web directory, only you / cron will be able to run it and nobody else.

There are different ways to run a php script from cron, like for example adding something like this as the first line of your php script:

#!/usr/local/bin/php      /* depends on your server and configuration */

Upvotes: 4

Related Questions