Results 1 to 10 of 10

Thread: Sorry newbie question, Varables and output

  1. #1
    Join Date
    May 2012
    Beans
    3

    Sorry newbie question, Varables and output

    I would like to run a script that checks to see is Mysql is live. I think something like

    Code:
    mysqladmin -umysql ping
    If the result does match mysqld is alive then I would like it to send an email using mailx.


    My question is this
    Why does this only show the last line? and how do I get it to show the full results
    Code:
    mysqladmin -umysql ping | $result
    echo $result
    Thanks

    Stefan

  2. #2
    Join Date
    Nov 2007
    Location
    Newry, Northern Ireland
    Beans
    1,258

    Re: Sorry newbie question, Varables and output

    Are you sure there is more than one line in the output? On my server the output is a single line:
    Code:
    stefan@fileserver:~$ mysqladmin -u root -p ping
    Enter password: 
    mysqld is alive
    stefan@fileserver:~$
    I'm also not sure what the pipe to $result is trying to achieve, do you want to append the output of the ping command to $result? Or is $result simply the output from the command? A different way of doing that would be:
    Code:
    result=$(mysqladmin -umysql ping)
    echo $result
    Can't think of anything profound or witty.
    My Blog: http://gonzothegeek.blogspot.co.uk/

  3. #3
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    5,442
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Sorry newbie question, Varables and output

    How about:
    Code:
    mysqladmin -umysql ping
    if [ $? == 1 ] ; then
        sendmail or whatever the command is
    fi
    Sorry, I don't know the command to send an email

    Or...
    Code:
    if ! mysqladmin -umysql ping
    then
        sendmail or whatever the command is
    fi
    Last edited by The Cog; February 5th, 2013 at 04:34 PM. Reason: Afterthought

  4. #4
    Join Date
    Nov 2007
    Location
    Newry, Northern Ireland
    Beans
    1,258

    Re: Sorry newbie question, Varables and output

    Quote Originally Posted by The Cog View Post
    How about:
    Code:
    mysqladmin -umysql ping
    if [ $? == 1 ] ; then
        sendmail or whatever the command is
    fi
    Sorry, I don't know the command to send an email

    Or...
    Code:
    if ! mysqladmin -umysql ping
    then
        sendmail or whatever the command is
    fi
    The OP said he wanted an email sent if the mysqld is alive, counterintuitive but there you go...

    Anyway, if the mail is to contain the results of the ping command, another option would be:
    Code:
    mysqladmin -umysql ping > /path/to/outfile
    if [ $? == 0 ] ; then
        mailx -s "Some Subject" -a /path/to/outfile some.address@somedomain
    fi
    Can't think of anything profound or witty.
    My Blog: http://gonzothegeek.blogspot.co.uk/

  5. #5
    Join Date
    Mar 2011
    Location
    UK
    Beans
    125
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Sorry newbie question, Varables and output

    Hi

    I would recommend instead of using a variable actually using a file in /tmp to store all the info. Then just output that file with cat notice thee difference between backtick ` (left of 1 on most keyboards) and apostrophe '

    Code:
    mysqladmin -umysql ping > /tmp/result
    cat `/tmp/result` | mail -s “mysql” your@email.com
    Hope this helps!

    Barry
    Desktop: Z77X-UD5H, Intel i7 3770k, NVidia GeForce 660 Ti Sli 16GB DDR3 RAM 2TB HDD
    OSes: Mint 14 x64 Windows 7 Ultimate x64
    Laptop: Dell Latitude E6430
    OSes: Ubuntu 13.04 x64, Backtrack 5 R3

  6. #6
    Join Date
    May 2012
    Beans
    3

    Re: Sorry newbie question, Varables and output

    Ok I'm getting closer

    Code:
     #!/bin/bash
     
    mysqladmin -umysql ping > /tmp/sqlresults
    if ! cmp -s /tmp/sqlresults /tmp/sqlalive; then
            mailx -A gmail -s "Test Number 5" stefan.thorpe@gmail.com < /tmp/sqlresults
    fi
    sqlalive is and exact copy of any alive output.( A single line that shows mysqld is alive)

    The compare checks to see if there is a difference if there is then it send me an email. With the results attached. However when the mysql is not running it displays the message below in the terminal and doesn't place it in the file. How do I resolve this?

    Code:
    mysqladmin: connect to server at 'localhost' failed
    error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
    Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

  7. #7
    Join Date
    Apr 2012
    Beans
    5,158

    Re: Sorry newbie question, Varables and output

    That would be because the message is going to the error stream instead of the standard output stream - if you want to capture both streams try either

    Code:
    mysqladmin -umysql ping > /tmp/sqlresults 2>&1
    or (bash 4+ only iirc)

    Code:
    mysqladmin -umysql ping &> /tmp/sqlresults

  8. #8
    Join Date
    Dec 2010
    Beans
    570
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Sorry newbie question, Varables and output

    If you are tyring to get both stdout and error you have to do the redirect of stderr to std out before you redirect to the file like this:
    Code:
    mysqladmin -umysql ping 2>&1 > /tmp/sqlresults
    Or if you want to capture it in a varable and not put it in a file:
    Code:
    result=$(mysqladmin -umysql ping 2>&1)

  9. #9
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,617
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Sorry newbie question, Varables and output

    I much prefer to run ps to find out if some daemon is running:

    Code:
    [ "$(ps ax | grep mysqld)" = "" ] && mail -s 'mysql down' you@example.com
    This approach generalizes to all daemon processes and doesn't depend on a specific feature of MySQL.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  10. #10
    Join Date
    Nov 2007
    Location
    Newry, Northern Ireland
    Beans
    1,258

    Re: Sorry newbie question, Varables and output

    Quote Originally Posted by StefanThorpe View Post
    Ok I'm getting closer

    Code:
     #!/bin/bash
     
    mysqladmin -umysql ping > /tmp/sqlresults
    if ! cmp -s /tmp/sqlresults /tmp/sqlalive; then
            mailx -A gmail -s "Test Number 5" stefan.thorpe@gmail.com < /tmp/sqlresults
    fi
    sqlalive is and exact copy of any alive output.( A single line that shows mysqld is alive)

    The compare checks to see if there is a difference if there is then it send me an email. With the results attached. However when the mysql is not running it displays the message below in the terminal and doesn't place it in the file. How do I resolve this?

    Code:
    mysqladmin: connect to server at 'localhost' failed
    error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
    Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
    Can you please confirm that you want to send a mail when mysql is down or when it is alive? The OP suggested you wanted to send a mail if mysql was alive, but the post quoted above suggests the opposite, which admittedly does make more sense.

    If you want to get an email if it is down, SeijiSensei's solution is tidier if the output of the ping is not really important.
    Can't think of anything profound or witty.
    My Blog: http://gonzothegeek.blogspot.co.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •