2

I have a PHP script that I use to perform maintenance tasks on a website. I use a cron to execute it every X hours but I would also like to have it as an option if the site admin wants to run it manually. In order to make it admin friendly, at the end it should display the results in the browser by echoing various messages.

Question is, does it matter if the script echoes out the results when ran in a cron or will it cause errors? Should it only output to the browser when executed by the admin? Is there a way to tell when a script is executed in a browser and when from a cron?

Thanks.

bikey77
  • 6,384
  • 20
  • 60
  • 86
  • 1
    You can pass a parameter to a php file that you're executing via cron. for example.php?cron=1 when with cron and only example.php when via web. Then give an if statement inside php file to check that out. If youre echoing anything into the screen, then cron will store the response into different files on your ftp server. – Modestas Stankevičius Jun 06 '13 at 11:43
  • 1
    Append ` >& /dev/null ` onto your cron cmd – Lawrence Cherone Jun 06 '13 at 11:45

3 Answers3

1

Output of cron scripts is often mailed to the server admin. Since this could be annoying you normally do not want any output if no errors happened.

You can php_sapi_name() to check which type of interface was used ("cli" for cron jobs).

See the php docs.

Since other answers propose redirecting the output to /dev/null you should keep in mind that you will have no way to know if any errors happened. You cronjob could stop to work and you would not know for a long time...

Jens
  • 2,050
  • 1
  • 14
  • 30
0

If the cron job has any output on stderr or stdout, then cron will put send that output in an email to the cron job user, or the address given in the MAILTO variable last set in the cron file.

You should usually write your cron job/script so that it produces no output unless there's a problem. It's a cry wolf thing. If your cron job is constantly noisy, you won't pay attention when you need to.

So, there's a couple of likely approaches, that still give informative output when run from the command line:

  1. You can write informative error messages to stdout, and reserve stderr for things which really need to get attention. You can then have your cron job specification pipe stdout to /dev/null

  2. You can have command line options that switch output on or off. eg --verbose, or --quiet.

It is possible to test for the presence of a Terminal, but I think you'd be better off with one of the approaches above. The behaviour is then less surprising.

mc0e
  • 2,699
  • 28
  • 25
0

It won't be a problem.

The echo command will use php's standard output. If you execute the php script in the console, the echo shall print its' results in the console.

I believe that the crontab's standard output might depends of its' unix distribuition.

See does echo equal fputs( STDout )?. This question has some valuable information.

Community
  • 1
  • 1
hdvianna
  • 443
  • 5
  • 13