suganya
suganya

Reputation: 245

Invoking cron from php is not working

Cron is not working properly. i have created a file inside /etc/cron.d with following command

$ touch /etc/cron.d/php-crons
$ chown www-data /etc/cron.d/php-crons

i got error like (*system*php-crons) WRONG FILE OWNER (/etc/cron.d/php-crons) so i changed file owner as root

$ chown root /etc/cron.d/php-crons

even though cron is not working. my php file (cron.php) is as follows

 $fp = fopen('/etc/cron.d/php-crons', 'a');
 fwrite($fp, '10 * * * * root usr/bin/php PATH TO SCRIPT/email.php'.PHP_EOL);
 fclose($fp);

when i open the /etc/cron.d/php-crons there i can able to see the job.

 10 * * * * root usr/bin/php /var/www/PATH TO SCRIPT/email.php

In email.php i included

  #!/usr/bin/php
 mail ("[email protected]", "Cron Successful Public HTML!", "Hello World from cron.php!");

if i change (/etc/cron.d/php-crons)file owner as root and then run cron.php in browser , then i not able to write anything inside /etc/cron.d/php-crons and get warning as follows.

Warning: fopen(/etc/cron.d/php-crons): failed to open stream: Permission denied in /var/www/cron.php on line 2 Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/cron.php on line 3 Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/cron.php on line 4 . please someone guide me!!

Upvotes: 0

Views: 1503

Answers (2)

Doon
Doon

Reputation: 20232

which operating system are you using? Normally for something like this you want to use per user crontabs. so the commands are run as that user. /etc/cron.d is used for system crontabs. (/var/spool/cron/crontabs) is where user crontabs are stored, but they will need to be enabled. Normally by adding the user that is allowed to use them to /etc/cron.allow. Allowing the web process to make changes to files that run as root is really a bad idea. With the per user option it would only be able to touch things that it could normally touch anyways, so less of security risk.

Upvotes: 0

initall
initall

Reputation: 2385

There are several issues. As you noticed, your implementation of the cron daemon does not allow to be the files owned by non-root-users and you will not be allowed to make the file world-writable for the same reason of security.

Your cron job line, at least in the example, lists a relative path ("usr/bin/php") that should be absolute. That your cron.php file is owned by root does not mean it is run as root user. It is executed and run by PHP because it has the appropriate group and/or other permission bits. And you shouldn't change the permissions in a way it is run as root (e.g. with setuid-chmods).

What you probably should do is a cronjob line that acts as a wrapper to a PHP script. The PHP script reads all your jobs from a database and executes them, or even a text file similar to what you already created.

This way you can run the wrapper script as a non-root user. Most cron implementations allow you to name the user (e.g. www-data) on others you use the "su" command before your script (see man 5 crontab etc.).

Upvotes: 1

Related Questions