How to check whether the Redis server is running
Asked Answered
V

7

95

How to check whether the Redis server is running?

If it's not running, I want to fallback to using the database.

I'm using the FuelPHP framework, so I'm open to a solution based on this, or just standard PHP.

Variate answered 26/3, 2012 at 4:18 Comment(0)
B
9

What you can do is try to get an instance (\Redis::instance()) and work with it like this:

try
{
    $redis = \Redis::instance();
    // Do something with Redis.
}
catch(\RedisException $e)
{
    // Fall back to other db usage.
}

But preferably you'd know whether redis is running or not. This is just the way to detect it on the fly.

Brasserie answered 26/3, 2012 at 20:8 Comment(0)
S
270

You can use command line to determine if redis is running:

redis-cli ping

you should get back

PONG

that indicates redis is up and running.

Stephainestephan answered 7/7, 2017 at 23:53 Comment(2)
it won't work on redis clusterBrownell
In any case, that response is based on this official redis reference redis-cliFixed
E
19
redis-cli -h host_url -p 6379 ping
Eudoca answered 1/10, 2021 at 4:54 Comment(0)
D
11

All answers are great,

aAnother way can be to check if default REDIS port is listening

i.e port number 6379 lsof -i:6379

if you don't get any output for above command then it implies redis is not running.

Despair answered 21/6, 2021 at 12:32 Comment(1)
This is the way if you are running it off a docker containerBrindisi
B
9

What you can do is try to get an instance (\Redis::instance()) and work with it like this:

try
{
    $redis = \Redis::instance();
    // Do something with Redis.
}
catch(\RedisException $e)
{
    // Fall back to other db usage.
}

But preferably you'd know whether redis is running or not. This is just the way to detect it on the fly.

Brasserie answered 26/3, 2012 at 20:8 Comment(0)
H
8

You can use this command on linux:

systemctl status redis-server

It will give you this output:

● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2023-01-11 19:14:30 UTC; 1 day 4h ago
Docs: http://redis.io/documentation, man:redis-server(1)
Main PID: 43270 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 477)
Memory: 18.5M
CPU: 3min 34.450s
CGroup: /system.slice/redis-server.service
└─43270 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" ""
Jan 11 19:14:30 ip-172-31-17-230 systemd[1]: Starting Advanced key-value store...
Jan 11 19:14:30 ip-172-31-17-230 systemd[1]: Started Advanced key-value store.
Hives answered 13/1, 2023 at 0:11 Comment(0)
C
5

you can do it by this way.

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

echo $redis->ping();

and then check if it print +PONG, which show redis-server is running.

Chally answered 27/12, 2014 at 2:56 Comment(0)
E
-2

systemctl status redis

will help you

Extortionary answered 7/12, 2023 at 7:2 Comment(2)
Please don't duplicate existing answersUnpracticed
redis instead of redis-server as used in the other answer? Why?Orpington

© 2022 - 2024 — McMap. All rights reserved.