0

I'm sure this is fairly easy for someone to answer.

I have a dedicated server that had been setup by someone else previous to me and I need to set a new cron job. The cron will run a PHP file.

So far I have done the following:

  • sudo crontab -u root -e
    To edit the crontab. The last line before I added my line of code was a PHP script cron so I used that as example and changed the file path to my new file. See the two examples below, also I changed the cron to run every minute:

Old Example

 */5 * * * * /usr/bin/php /home/emailer/htdocs/admin/cron.php

New Code I added

 */1 * * * * /usr/bin/php /home/non-cms-websites/crons/cron-notifications/index.php

The script I have added just sends a test email to my email account use PHP Mailer, just a basic script at the moment but will have more functionality when I get it working.

Do I need to restart/reload the cron so the new one I added runs?

The server is running Linux Ubuntu 10.04 64 bit if that makes a difference.

Any help greatly appreciated, thank you.

Kane Mitchell
  • 370
  • 2
  • 4
  • 12
  • skipping the idea to run something like this as `root` it should work fine - you are sure your cron dont work? or maybe you have bad php file and the reason you arent recieving email is in the php script not cron – violator667 Jan 08 '15 at 11:10
  • I am able to run the PHP file directly through a browser at the moment and the email sends fine so I don't believe its the file. Just can't seem to work out if it's possible to debug. Thanks for your input. – Kane Mitchell Jan 08 '15 at 11:15
  • 2
    try to `/usr/bin/php /home/non-cms-websites/crons/cron-notifications/index.php` in your terminal and look for errors – violator667 Jan 08 '15 at 11:18
  • Is your PHP script dependent on any environment variables to work correctly? They may not be set when it runs from cron. – Kenster Jan 08 '15 at 11:53

4 Answers4

0

Check the path to index.php file is correct or not. Also run crontab -l command to see the list of crons.

Needhi Agrawal
  • 1,326
  • 8
  • 14
  • Yeah I have checked it again and it's that the path seems fine. I just don't understand how I can debug it to try and see what is going wrong. – Kane Mitchell Jan 08 '15 at 11:17
  • You run the crontab -l – Needhi Agrawal Jan 08 '15 at 11:21
  • running "sudo crontab -u root -l" and I can see the new cron job I created is in the list though but no other information. – Kane Mitchell Jan 08 '15 at 11:25
  • Instead of /usr/bin/php just use php and then see. – Needhi Agrawal Jan 08 '15 at 11:28
  • Still no luck with that Needhi, I used "/usr/bin/php" because the previous developer who set up the server had ran a PHP cron like that so it made sense to do the same. I really don't know what else to do. Any ideas on any debugging techniques? – Kane Mitchell Jan 08 '15 at 11:38
  • You can check by accessing your file from browser will work or there is syntax error or something. – Needhi Agrawal Jan 08 '15 at 11:40
  • Also from the terminal you can directly run the file to check what's the problem by running the command:/usr/bin/php /home/non-cms-websites/crons/cron-notifications/index.php – Needhi Agrawal Jan 08 '15 at 11:43
  • THANK YOU Needhi! I ran the script as above and I saw a fatal error in the PHP that wasn't showing in the browser. I also just found that the error emails were going into my spam so I missed it completely. Thank you all for the help. – Kane Mitchell Jan 08 '15 at 11:51
0

According to this link you need to restart: http://wiki.qnap.com/wiki/Add_items_to_crontab

According to this link you don't need to restart but it seems on some comments that it might vary depending on linux distribution. Restarting cron after changing crontab file?

The command to restart may differ depening on linux distribution. This works on my Debian machine and should work on Ubuntu as well.

sudo /etc/init.d/cron restart

or

sudo service cron restart
Community
  • 1
  • 1
user1766169
  • 1,932
  • 3
  • 22
  • 44
0

I suppose restarting is not necessary if you are using Vixie Cron. probably the php file path or script has some issue. Please see the below link it might help. i have quoted a small excerpt.

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

"With lots of daemons, (e.g. httpd and syslogd) they need to be restarted after the config files have been changed so that the program has a chance to reload them. Vixie Cron will automatically reload the files after they have been edited with the crontab command. Some cron versions reload the files every minute, and some require restarting, but Vixie Cron just loads the files if they have changed."

0

You mentioned that you have checked it, but not clear if you meant the path to php or actually ran it on the cli - as you did state it works thru the browser, and the path to .php script is correct. Sanity check the php executable is where is should be

/usr/bin/php -v

While I run completely differnet version of everything, being 2020, my php exec path is /bin/php.

And no funny business with permissions.

Ensure you have php-cli installed:

dpkg --get-selections | grep -i php

Check the cron logs (usually /var/log/cron ) Check for PHP errors. Ensure php.ini actually has error logs for cli: e.g.

; Log errors to specified file. PHP's default behavior is to leave this value ; http://php.net/error-log error_log = /var/log/httpd/php_errors.log ; Log errors to syslog. ;error_log = syslog

Or even easier, temporarily enable error display for the script to debug (assuming is it not already explicitly disabled). Top of the script after your

error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', 1);

Run it directly off the cli using the command string you are configuring in cron (again - sanity check!). Let cron do its thing then check the logs.

The cron log will at very least tell you if it is being called at all. The PHP error log (or syslog as per the config) will give you the expected basics, but also some easy to miss faults, like exceeding mem limits or a configured max_execution_time.

Running via the webserver does not imply it's all good on the cli.

Coopz
  • 41
  • 3