3

Trying to learn how to implement a cron job using php.

cron is formatted like so /etc/crontab

 * 13 * * * /usr/bin/php /var/www/path/to/file/database-bkup-test.php &>/tmp/mycroncommand.log

So this should run at 13 hour daily (1 pm)

I reviewed the status of the service with

sudo systemctl status crond.service -l

Found the following error:

(/usr/bin/php) ERROR (getpwnam() failed)

I have this set up using PHP, the script is basically this:

$today = date('Y-m-d');
$user = 'db_user';
$password = 'db_password'; 
$host = '127.0.0.1'; //this is actually replaced with an address to another remote server
$port='3306';
$database='database_name';

exec('mysqldump --user='.$user.' --password='.$password.' --host='.$host.' --port='.$port.' '.$database.' > /db-bkup/db-'.$today.'.sql');

the end result would be a database dump into a file:

db-2017-03-24.sql

Could anyone please assist me?

I have spent time reading and trying to implement this. I found this post to be very helpful

Since the job is failing, the whole thing is not even generating the log in the /tmp.

Update

Added a simple cron to echo 'Hello World';

*/2 * * * * root /scripts/test.php &>/tmp/mycrontest.log

This time I have a log generated with Permission Denied. I presume that is because I never applied /usr/bin/php

This is a step forwards from original error.

4
  • Have you tried it with other PHP scripts, to see if the problem is your script or something else? E.g. just do a script that prints "hello world" and see if that will run.
    – CMR
    Mar 24, 2017 at 14:10
  • @ConnWarwicker When you say print, do you mean echo? No I have not but I will try.
    – mcv
    Mar 24, 2017 at 14:13
  • If you add in the proper PHP path, does that get rid of the permission error on the simple script and run it properly? Also, which user account are you running these as?
    – CMR
    Mar 24, 2017 at 14:33
  • @ConnWarwicker I have to sudo to modify the files for crontab, so that would be root. I am including the PHP path now, I actually replaced the user with the path and I think that is the issue. Now I have the user and path there. Ugh...I think this will resolve the issue. :)
    – mcv
    Mar 24, 2017 at 14:58

1 Answer 1

6

Thanks to Conn Warwicker above for assisting in resolving the issue:

I omitted the user root from my cron job.

 * 13 * * * root /usr/bin/php /var/www/path/to/file/database-bkup-test.php &>/tmp/mycroncommand.log
3
  • 2
    Yes, you should specify the user running the job.
    – Yuri
    Dec 19, 2017 at 9:15
  • @Yuri wow really?!
    – oldboy
    Jul 2, 2018 at 4:26
  • this didnt solve my problem :/ same error
    – oldboy
    Jul 2, 2018 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .