The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

what is /dev/null 2>&1 ?

Discussion in 'General Discussion' started by cpane1, Nov 29, 2007.

  1. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    Hi,

    I have a problem where I have set up a cron to mail me when my mysql is down...
    I now receive emails every 20 minutes but the problem is that it mails me blank emails, that means even if the mysql is not down I receice BLANK emails. What cron should I set up so that I will receive emails only if mysql is down ??
    my cron:

    */20 * * * * /usr/bin/php /home/username/public_html/admin/mysqlstat.php

    Also as my subject line suggests what is what is /dev/null 2>&1 ? does that relates to my query above ??? Please help...
     
  2. darren.nolan

    darren.nolan Well-Known Member

    Joined:
    Oct 4, 2007
    Messages:
    259
    Likes Received:
    0
    Trophy Points:
    16
    I believe cPanel will send you an email every 5 minutes if one of your monitored services is down - Under WHM -> Service Manager -> Ensure the box to monitor MySQL is selected.

    With your cron - change it to;

    */20 * * * * /usr/bin/php -q /home/username/public_html/admin/mysqlstat.php

    -q is quiet or not header mode (might explain why you are getting blank emails?) - and have your mysqlstat.php file generate an email if the service is down.

    From LinuxHelp.net http://www.linuxhelp.net/guides/cron/
    >> /dev/null 2>&1 part means to send any standard output to /dev/null (the linux trash can) and to redirect standard error (2) to the same place as the standard output (1). Basically it runs the command without any output to a terminal etc.

    Hope this helps :)
     
  3. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    Hiya,

    Thanks for your response on this, but I am using this cron for a client ..hosted on the server.

    I simply want to make the cron email him only if there is a failure or errors with mysql server... I do not want the BLANK emails every 20 minutes...

    Please help.... :(
     
  4. rbalaji

    rbalaji Member

    Joined:
    Nov 27, 2007
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    no worries,
    You can set it up directly in crontab file. Open your crontab file and assign the cron mail address for him .
    The format looks like..
    -----------------
    ]crontab -e <username>
    MAILTO=username@domain.com
    */20 * * * * /usr/bin/php -q /home/username/public_html/admin/mysqlstat.php
    ----------------


    Thanks:)
     
    #4 rbalaji, Nov 30, 2007
    Last edited: Nov 30, 2007
  5. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    Hi rbalaji,

    Thanks, but I have already set that up and I am receiving this BLANK emails even if mysql is not down or have any errors with it.........

    One thing to note that the cron does send emails when there is a mysql failure...

    so in short here how it goes.

    cron emails when....

    there is a failure with mysql
    there is no mysql failure....BLANK email <---

    I do not want cron to email me when is no mysql failure.......



    Hope I am clear this time....

    Please help..
     
  6. darren.nolan

    darren.nolan Well-Known Member

    Joined:
    Oct 4, 2007
    Messages:
    259
    Likes Received:
    0
    Trophy Points:
    16
    I stress the -q after /php
     
  7. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    I tried the above to no avail...Still receiving the blank emails....

    please help....

    I don't think this works with cron..... is it ?
     
  8. darren.nolan

    darren.nolan Well-Known Member

    Joined:
    Oct 4, 2007
    Messages:
    259
    Likes Received:
    0
    Trophy Points:
    16
    What is your PHP script?
     
  9. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    Hi,

    attached is the script......

    It used to work but now suddenly its not.....

    cron set as:

    */20 * * * * /usr/bin/php -q /home/username/public_html/admin/mysqlstat.php

    Any other alternative besides php -q ? like wget, get .....
     

    Attached Files:

  10. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    One method I used for a long time was placing the script in public_html (like Drupal does with its cron script) then call lynx to request that "page".

    Another alternative is to make your PHP script a PHP shell script. That'd mean placing a hashbang at the top of the file as you would with a Perl script, except calling the PHP binary instead. If you don't want to echo output, use the -q parameter in there as well.

    In my own scripts though, when I want an email about something, I put in my own calls to mail() rather than relying on some implicit functionality of cron.
     
  11. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    Can you please let me know any such call (using mail() function) that can be added into the script that I have provided.....


    That would be much appreciated....

    :)
     
  12. brianoz

    brianoz Well-Known Member

    Joined:
    Mar 13, 2004
    Messages:
    1,146
    Likes Received:
    6
    Trophy Points:
    38
    Location:
    Melbourne, Australia
    cPanel Access Level:
    Root Administrator
    Google is your friend :) or php.net/mail, or phpfreaks, or sitepoint ...
     
  13. darren.nolan

    darren.nolan Well-Known Member

    Joined:
    Oct 4, 2007
    Messages:
    259
    Likes Received:
    0
    Trophy Points:
    16
    mmm -sitepoint.
     
  14. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    php.net/insert function name here is definitely a useful resource for PHP scripting, especially the user-contributed comments.
     
    #14 cPanelDavidG, Dec 5, 2007
    Last edited: Dec 5, 2007
  15. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6

    Thanks for all your help...... but all seems to be in vain :( as the client want that script and no other way round.. he says it USED to WORK.. and now its not....
    I don't understand whats changed on the server ......

    also does cron really does what the client really looking for ??? this is a BIG question.....

    guys..come one help me in this......


    :( :(
     
  16. brianoz

    brianoz Well-Known Member

    Joined:
    Mar 13, 2004
    Messages:
    1,146
    Likes Received:
    6
    Trophy Points:
    38
    Location:
    Melbourne, Australia
    cPanel Access Level:
    Root Administrator
    We weren't talking about changing scripts, just modifying it slightly so it only sends a mail on failure.

    I doubt cron changed, it's possible the script is now outputting a blank line where it wasn't before. That would result in apparently empty emails where none were generated before.

    You could change the cron line to do something like:
    Code:
    scriptname >output 2>&1 || echo ERROR $?;  grep -q . output && cat output
    This will output the string ERROR if the script exits with an error, and will only send email if there is either an error or the output file is non-empty or both. The output file must have more than just blank lines to be considered non-empty. This will only help if the reason why they're receiving an empty email is that there is a blank line in it, and you haven't shown us the email so we don't know that for sure, but it's a reasonable guess as previously no emails were being generated and now they are.

    Probably you'll need to either do some reading on this, or get some help scripting if this doesn't work.
     
  17. cpane1

    cpane1 Well-Known Member

    Joined:
    Jun 10, 2007
    Messages:
    56
    Likes Received:
    0
    Trophy Points:
    6
    Thank you so much for your response, really appreciated.... I will definitely go on to try your suggestion.

    However we ended up sorting up this issue by adding another script to do the job of emailing only when there is a error with MYSQL....

    God knows what this clients really ask for sometimes, really tough to sort out :confused:



     
Loading...

Share This Page