On the Ubuntu command line:
sudo netstat -tap | grep mysql
if MySQL is running, it returns something like this:
tcp 0 0 localhost:mysql *:* LISTEN 6732/mysqld
and nothing if it is not.
I use a subprocess to find out from within python code if MySQL got up looking for "LISTEN" in what netstat returns by doing this:
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 an argument from a single word (say, "df") - it works fine. If the argument is more than one word (ie "Df -h /" or, as here, "sudo netstat -tap | grep mysql"), I get the error "There is no such file or directory."
And the related question (# 2), when I run this command on the command line - sometimes it asks for the root password. How to pass password from python script?
source share