Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Created February 21, 2014 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlemonvh/9138770 to your computer and use it in GitHub Desktop.
Save turtlemonvh/9138770 to your computer and use it in GitHub Desktop.
Register this as a crontab to run every hour to check if mysql had a hard time restarting. If so, it may be because this was brought down quickly. Use `sudo crontab -e` when creating the crontab because it needs to run as root to be able to move the pid files and restart the service.
#!/usr/bin/env python
import os
import subprocess
child = subprocess.Popen('service mysqld status', shell=True, stdout=subprocess.PIPE)
# Capture output string
outstr = ''
while True:
out = child.stdout.read(1)
if out == '' and child.poll() != None:
break
if out != '':
outstr +=out
# Is it running?
if 'is running' not in outstr:
# There's a problem
# http://stackoverflow.com/questions/20407292/centos-another-mysql-daemon-already-running-with-the-same-unix-socket
# sudo mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak
os.rename('/var/lib/mysql/mysql.sock', '/var/lib/mysql/mysql.sock.bak')
# Now restart it
subprocess.Popen('service mysqld start', shell=True)
print "Attempted to restart mysql"
else:
print "Everything looks fine here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment