views:

269

answers:

2

So I have a script that I debug with a bunch of echo statements. This is run every 3 minutes on my server by cron, and I sometimes leave the echo statements in there. They're not going to a browser, they're just going... anywhere?

This is a vague question I guess, but what happens when there's no end-user or output for an echo statement? Does it hog up memory? Does it just vanish? I'd appreciate any help in understanding this.

+8  A: 

Yes they are outputted but to noone in particular (See zombat's answer, it's mailed to the owner of the crontask). You can write the output of your script to a file via:

php myscript.php > /var/log/cronlog.log

(Assuming you're using linux since you said cron and not scheduled task)

Mike B
And are they held in memory or anything bad like that? Or is forgetting to comment out a few echos no big deal?
Alex Mcp
@Alex: They go into the STDOUT buffer, so yes, they are kept in memory. But it is periodically flushed out and has a maximum size. The mem leak wouldn't be your echo statements.
Eric
As stated by zombat, the output is mailed to the owner of the cron job. It's usually a system account, and thus sometimes hard to find if you have virtual addresses and such.
DGM
+6  A: 

The answer is yes, and the output is mailed to the account that is running the cron task. You can change this in the crontab file by setting a "MAILTO=accountname" option, like this example cron file:

MAILTO=root

# run a script every hour
01 * * * * root run-parts /etc/cron.hourly
#etc.

Any output from the above cron task would be mailed to the root user. As Mike B posted, you can also simply redirect the output elsewhere on the task line using the > operator:

01 * * * * php testscript.php > /var/log/logfile.log

in which case cron does not see it and does not send an email.

The bottom line is that if you leave some echo statements in a PHP script and set it as a cron job, then you will start getting emails from the cron daemon.

zombat
You can redirect to `/dev/null`, if you don't want to keep the output. It'll still get printed, but it'll immediately be discarded, thereby not taking up any memory.
troelskn
@troelskn - definitely a popular strategy.
zombat
So I found I was using 75% of my diskspace quota on that server, and looked and looked.... and found 3 gigs of mail in the maildir directory. So there you go!
Alex Mcp