-4

I have a cron running and there are some warning emails i get. How to stop those warning emails?

I am getting these warnings in my email. PHP Warning: session_start(): Cannot send session cookie - headers already sent

Can someone help me to solve this warning or help out in stopping those warning emails being sent.

Techsavy
  • 33
  • 6

2 Answers2

3

Cron is quite dumb and sends everything that a process prints to STDOUT or STDERR to the email address configured in the MAILTO environment variable (or, if not set, to the user owning the cron). There are a few ways to fix this issue:

  1. The right way: Fix your PHP warnings. Note that there is actually no good reason for calling session_start in a cron. This is preferable to simply piping the entire output into oblivion (/dev/null) since now you can use cron's mail feature for actual error reporting: just have your cron keep quiet when it runs successfully and have it print something to stdout or stderr when an actual error occurs.
  2. The easy way: Redirect your output into a log file (or /dev/null if you don't care about the output):

    * * * * * user ./your/command.php >/path/to/logfile 2>&1
    
helmbert
  • 35,797
  • 13
  • 82
  • 95
  • Can you please explain point 1 in detail? – Whip Jan 22 '18 at 04:44
  • @VeeK Please elaborate. What's unclear about the first item? – helmbert Jan 22 '18 at 15:26
  • "since now you can use cron's mail feature for actual error reporting" What does that mean and how does that work? Could you post an example? – Whip Jan 23 '18 at 11:47
  • When your crontab contains a `MAILTO=` assignment, all output of that script will be sent to that mail address. Make sure that your script does not output anything on success, and any errors messages otherwise, and you've got yourself a quick-and-easy alerting whenever one of your cron jobs is failing. See the [`crontab(5)` man page](http://man7.org/linux/man-pages/man5/crontab.5.html) for more information. – helmbert Jan 23 '18 at 18:30
1

You can redirect standard error to standard output and then redirect it all to /dev/null so you won't get any notices from your cron jobs.

# sample crontab
0 * * * * /usr/bin/php /path/to/phpfile.php >/dev/null 2>&1

The above tells all output to go to /dev/null including anything on the STDERR descriptor which is where PHP errors come out.

Travis D
  • 356
  • 2
  • 7