3

I'm looking for a way to check if a redis instance (on a local machine with default port) is running or not. If not, I want to start it from my python code.

1
  • Try to connect to it and see if your connection is denied? Ask the sentinels which master they know about? There are a few options here. Commented Mar 15, 2013 at 18:29

3 Answers 3

4

If you start a redis client you can first try to ping -- if you get a redis.exceptions.ConnectionError then the service is probably not running. Below is an example of such a function. There are other ways to get a similar or more robust result -- this one is just an easy approach. Also note that this doesn't tell you if a particular key is setup or anything about the redis setup. It only tells you if there is a live redis server on localhost or not.

def redisLocalhostLive():
    redtest = redis.StrictRedis() # non-default ports could go here
    try:
        return redtest.ping()
    except ConnectionError:
        return False
Sign up to request clarification or add additional context in comments.

Comments

3

gah, Pyrce beat me to a similar answer. posting anyway:

import redis

server = redis.Redis()
try:
    server.ping()
except redis.exceptions.ConnectionError:
    # your redis start command here

Comments

1

One approach is to use psutil, which is a Python module that provides a cross-platform way to retrieve info on running processes.

>>> import psutil
>>> processes = psutil.process_iter()   # Get all running processes
>>> if any(process.name == 'redis-server' for process in processes):
...     print "redis is running"
... 
redis is running

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.