Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cronjob creates output file, but doesn't write to it

Tags:

php

cron

I've got these two lines in my crontab file:

0 * * * * /usr/bin/php /var/www/html/worker/confirmBets.php >> /var/www/html/worker/out.put
30 23 * * * mv /var/www/html/worker/out.put /var/www/html/worker/out.put-"$(date +"\%y-\%m-\%d")" >/dev/null 2>&1

While the second line works and renames the out.put file every night, the first line doesn't quite work. It does create the out.put file, but it does not write anything to it. I know that the PHP script works, as when I'm running the command (exactly that line, just without the 0 * * * *), it writes perfectly fine to the out.put file. When it's started from the cronjob though, it doesn't write to it (even though it creates it).

Funny fact: It did work several weeks ago, I just noticed it somehow doesn't work anymore. Unfortunately I don't know if I changed anything in the server setup in the meantime. What could cause that problem?

The syslog shows the following output:

Feb 25 07:00:01 vps485163 CRON[15161]: (root) CMD (root /usr/bin/php /var/www/html/worker/confirmBets.php >> /var/www/html/worker/out.put)
Feb 25 07:00:01 vps485163 CRON[15160]: (CRON) info (No MTA installed, discarding output)

What's meant by that?

like image 557
Maximilian Krause Avatar asked Dec 08 '25 17:12

Maximilian Krause


1 Answers

Found the solution. I just found out that you could directly redirect output from stderr, too, in a crontab. I edited my first line like so:

Feb 25 07:00:01 vps485163 CRON[15161]: (root) CMD (root /usr/bin/php /var/www/html/worker/confirmBets.php >> /var/www/html/worker/out.put) 2>&1

Notice the 2>&1 at the end which outputs stderr (2) where stdout is currently going (>> /var/www/html/worker/out.put). Through this, I could successfully log the PHP error in my output file. I messed up with a file path in a require(). That simple.

Even though that's just a guess, I think that's also where the error in my syslog came from: There's specified in the crontab file that output, which is not redirected, is sent via email to the cronjob owner. As there was output on stderr, which was not redirected, it tried to send it to me via mail even though I do not have a Mail Transfer Agent installed.

like image 76
Maximilian Krause Avatar answered Dec 11 '25 10:12

Maximilian Krause