Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:42:31+00:00 2026-06-13T11:42:31+00:00

On Ubuntu this command line: sudo netstat -tap | grep mysql if MySQL is

  • 0

On Ubuntu this command line:

sudo netstat -tap | grep mysql

if MySQL is running returns something like that:

tcp  0  0 localhost:mysql  *:*  LISTEN  6732/mysqld

and nothing if it’s not.

I’m using subprocess to find out from inside the python code if MySQL is up by looking for a “LISTEN” in what that netstat returns, doing that:

import subprocess

msqlr = subprocess.Popen(["sudo netstat -tap | grep mysql"], stdout=subprocess.PIPE).communicate()[0]
msqlrLines = msqlr.split("\n")
vals = msqlrLines[0].split()
print "vals[0] : %s" % vals[0]
if vals[0][-2] == "LISTEN":
    print "OK - MySQL is running."
else:
    print "Not OK - MySQL is not running."

When I run this it returns:

OSError: [Errno 2] No such file or directory

When in the same subprocess.Popen… I use a one word argument (let’s say “df”) – it works OK. If the argument is more that one word (i.e. “df -h /” or, like here “sudo netstat -tap | grep mysql”) – I’m getting this “No such file or directory” error.

And related question (#2), when I’m running this command in a command line — sometimes it asks for a root password. How do I pass a password from a python script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:42:32+00:00Added an answer on June 13, 2026 at 11:42 am

    Try something line this.

    import subprocess
    import string
    
    msqlr = subprocess.Popen("sudo /usr/sbin/netstat -al".split(), stdout=subprocess.PIPE).stdout
    grep = subprocess.Popen(["/usr/bin/grep", "mysql"], stdin=msqlr, stdout=subprocess.PIPE).stdout
    msqlrLines = grep.read().split("\n")
    vals = map(string.strip, msqlrLines[0].split())
    print vals
    if vals[-1] in ("LISTENING", "LISTEN"):
        print "OK - MySQL is running."
    else:
        print "Not OK - MySQL is not running."
    

    OUTPUT on my machine:

    ['tcp4', '0', '0', '*.mysql', '*.*', 'LISTEN']
    OK - MySQL is running.
    

    The idea here is you do the normal netstat, and collect all of the data. Then use the stdout from that subproc as the stdin for the next subproc and do your grep there.

    Here is an example running on ubuntu 12.04

    import subprocess
    import string
    
    msqlr = subprocess.Popen("sudo /bin/netstat -al".split(), stdout=subprocess.PIPE).stdout
    grep = subprocess.Popen(["/bin/grep", "mysql"], stdin=msqlr, stdout=subprocess.PIPE).stdout
    msqlrLines = grep.read().split("\n")
    vals = map(string.strip, msqlrLines[0].split())
    print vals
    if len(vals) and vals[-1] in ("LISTENING", "LISTEN"):
        print "OK - MySQL is running."
    else:
        print "Not OK - MySQL is not running."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running Ubuntu 11.10 and installed Ruby 1.9 like this: $ sudo apt-get install
Using mysql directly from the command line (running on Ubuntu 9.10, standard bash terminal),
Using the command line program lessc installed by following this guide on a Ubuntu
In my Python script running at the command line on Ubuntu, it's selecting UTF8-encoded
If I create a file in ubuntu like this: echo asd > file.txt and
We have a ubuntu test server that this simple script works fine on where
Say I have a web page like this on an Ubuntu 12.04 web server
I would like to automate the following svn command. Note this command produces the
On the command line I try this: curl --user admin:admin -XGET http://localhost:2480/query/demo/sql/select from Profile/20/*:-1
I'm using the Dropbox command-line utility/daemon on Ubuntu 11.10 but it's not working with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.