0

I have configured a cronjob to backup a mysql database and upload the file to Amazon S3. Here's the job:

57 14 * * * . $HOME/.profile; php /root/backup_scripts/mysql-backup/index.php

And here are the contents of index.php:

<?php

require 'vendor/autoload.php';

$s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'profile' => 'my-bucket',
]);

$file = tmpfile();
$filename = stream_get_meta_data($file)['uri'];
$username = getenv('MYSQL_USERNAME');
$password = getenv('MYSQL_PASSWORD');

shell_exec("mysqldump -u {$username} -p{$password} --events --all-databases --extended-insert=FALSE --complete-insert=TRUE > {$filename}");

$result = $s3->putObject([
    'Bucket' => 'my-bucket',
    'Key' => 'mysql.backup.' . time(),
    'Body' => $file,
]);

fclose($file);

?>

The upload works correctly, but I am receiving an email from Cron with the output of the command. The output is:

stdin: is not a tty

I've read some related questions on SO and understand that this is caused by a command expecting to be run with stdin as a terminal. When cron runs the command, there is no terminal, so the output occurs.

However, I haven't been able to find a solution for how to get rid of this. Is there any way to ensure this output does not occur? I'm also not clear on which command is actually generating the output. Is it the cron command itself, or the mysqldump being executed from within the PHP script?

flyingL123
  • 7,686
  • 11
  • 66
  • 135
  • Try to narrow down the source of error. Start with an empty PHP script, see if it complains. Then progressively add part by part until it fails. – randomir Oct 16 '17 at 19:10
  • Modify the command being run and add > /dev/null 2>&1 to it. That says to dump any and all output into the bit bucket and you won't be bothered again. Your complete command, with the change, will look like: `57 14 * * * . $HOME/.profile; php /root/backup_scripts/mysql-backup/index.php > /dev/null 2>&1` – Dave Oct 16 '17 at 19:23
  • Thanks @randomir. It looks like the output is a result of `. $HOME/.profile;` command. When this is removed, the output does not occur. I need this command though to load in environment variables. Any idea how to get around this? – flyingL123 Oct 16 '17 at 20:04
  • Thanks @Dave, unfortunately this won't work because I do actually want the output to be sent to me when there is 'real' output from an error in the script or something like that. I use the email to alert me of an issue. In this case it's producing output even though there isn't actually a problem. – flyingL123 Oct 16 '17 at 20:05
  • Maybe [this](https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use) will help. Btw, check your `HOME` is actually defined when run by `cron`. – randomir Oct 16 '17 at 20:09
  • (Using absolute paths is the safest way to go in `cron`.) – randomir Oct 16 '17 at 20:14

0 Answers0