Php – Cron Error getpwnam() failed for DB back up

centos7cronPHP

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.

Best Answer

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