how to make a php crontab silent

2,952

Solution 1

use -q it means "quiet" and thus doesn't generate output

*    *  *   *   * php -q /home/public_html/folder/file.php

also, ask yourself why you want to run it every minute, that's a little excessive.

Solution 2

2>&1 redirects the everything written to stderr to stdout

>/dev/null redirects stdout to /dev/null

cron will always email you anything sent to stdout or stderr unless redirected elsewhere. Ideally, your PHP script should be rewritten to accept a --quiet flag that doesn't write a whole bunch of useless crap out to stdout, which you could then use in your cron job.

The lazy man's option is to put a wrapper script around your cron job that will stop it from emailing you unless your command exits non-zero.

Share:
2,952

Related videos on Youtube

roshkattu
Author by

roshkattu

Updated on September 17, 2022

Comments

  • roshkattu
    roshkattu almost 2 years

    I am using the YoutubeDL library in a project. My environment is based on WINDOWS with XAMPP as the webserver boundle (apache,php,mysql,etc). I am using the youtube-dl.exe file to download the video and then use ffmpeg.exe to convert the video to an MP3 audio file.

    At the moment, I have an issue related to programming: I want to show live a progressbar while the video is downloaded with the youtube-dl.exe file. This exe creates a log file, that is updated while the video is downloaded. So my approach on this was to create a PHP file, that opens, parses the log file and get's the progress percent, and sends it as a json encoded value to an AJAX function that is called every 100MS. Indeed, if the video is too large, there will be a very high ammount of data while polling the PHP file to get the progress state. And sometimes, the browser either crashes or freezes because of this ajax polling.

    My question is: is there any better approach to do this with PHP/AJAX? Rathar than poll the file every 100MS, or 50MS?

    • Musikdoktor
      Musikdoktor over 4 years
      Rosh can you please share how you managed to print on a file the download progress? im on windows and xampp too but i tried popen ytdl and then write a txt but for some reason i cant get anything inside the file.. Can you please share your approach?
  • rodjek
    rodjek over 13 years
    You're assuming that errors are being printed to stderr rather than stdout. This is not a safe assumption.
  • Brooke.
    Brooke. over 13 years
    I see. I also see that curl has --silient but can I curl an php script?
  • Istvan
    Istvan over 13 years
    @BandonRandon, you can curl URLs like http/https if this was your question.
  • Brooke.
    Brooke. over 13 years
    It's a crontab that checks the status of buses in real time. The file is checking to see how many minutes the bus is away and if it's close alerts the user.
  • Brooke.
    Brooke. over 13 years
    @l1x yes that was my question. That would make it run just like loading it in the browser correct?
  • Istvan
    Istvan over 13 years
    @BandonRandon yes, like yourdoma.in/folder/file.php
  • Brooke.
    Brooke. over 13 years
    Also, -q did the trick.
  • Brooke.
    Brooke. over 13 years
    Okay, so that's another route to go. The accept answer seemed to be a simple solution.
  • Tom O'Connor
    Tom O'Connor over 13 years
    If it needs to run once a minute, what's the problem with that? Surely the developer knows why it needs to run so frequently...
  • mhmpl
    mhmpl over 13 years
    @Tom generally cron may not be the right solution for things that need to run that often. But that's an opinion, and one that we enforce at work.
  • Brooke.
    Brooke. about 13 years
    @xenoterracide what are some other options to check a database and sent out alerts on a realtime basis?
  • mhmpl
    mhmpl about 13 years
    @bandnon 1 minute intervals aren't realtime anyways... but long running scripts that poll will do it, event driven programming might be good, when an alert is generated, send it.