Redis Daemon not creating a PID file
Asked Answered
M

13

48

The Redis startup script is supposed to create a pid file at startup, but I've confirmed all the settings I can find, and no pid file is ever created.

I installed redis by:

$ yum install redis
$ chkconfig redis on
$ service redis start

In my config file (/etc/redis.conf) I checked to make sure these were enabled:

daemonize yes
pidfile /var/run/redis/redis.pid

And in the startup script (/etc/init.d/redis) there is:

exec="/usr/sbin/$name"
pidfile="/var/run/redis/redis.pid"
REDIS_CONFIG="/etc/redis.conf"

[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/var/lock/subsys/redis

start() {
    [ -f $REDIS_CONFIG ] || exit 6
    [ -x $exec ] || exit 5
    echo -n $"Starting $name: "
    daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $name: "
    killproc -p $pidfile $name
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

These are the settings that came by default with the install. Any idea why no pid file is created? I need to use it for Monit. (The system is RHEL 6.4 btw)

Madoc answered 26/8, 2014 at 21:8 Comment(3)
Does redis start correctly? Which user is used to launch redis? Does it have enough rights to write the PID file? Do you have something in redis log? You are using the shell scripts provided by rhel. Did you try to just launch redis without this script to see if it works without the script?Pathogen
@zenbeni, Yes, Redis starts and runs correctly, and is running under UID: redis. Doesn't appear to have permission to create a file there though. I'll try to set that upMadoc
I'll say this...whoever wrote the documentation for redis is a meth head...I shouldn't have to google and search down rabbit holes of errors and config file edits to install something. There should be a readme.txt or a man page that should do the job. This is ridiculous.Obstacle
M
28

Problem was that the user redis did not have permission to create the pid file (or directory it was in). Fix:

sudo mkdir /var/run/redis
sudo chown redis /var/run/redis

Then I killed and restarted redis and sure enough, there was redis.pid

Madoc answered 27/8, 2014 at 14:10 Comment(2)
My pidfile path was set as pidfile="/var/run/redis.pid" so redis couldn't create the file because of permissions on /var/run/, this solution helped me figured out my case, thanksGentes
This is not enough on Ubuntu 18.04. Permissions are fine. Sometimes the PID file is there, sometimes it isn't. No logic to it. This makes is hard to monitor Redis.Legitimize
E
53

For those experiencing on Debian buster:

Editing nano /etc/systemd/system/redis.service

and adding this line below redis [Service]

ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"

It suppose to look like this:

[Service]
Type=forking
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"
PIDFile=/run/redis/redis-server.pid

then:

sudo systemctl daemon-reload

sudo systemctl restart redis.service

Check redis.service status:

sudo systemctl status redis.service The pid file now should appear.

Elasticity answered 27/3, 2020 at 12:17 Comment(6)
Thanks a lot, this also solves the Ubuntu 18.04 issueGloomy
The proper fix for Ubuntu 20.04.2Celebes
this is legit for sureFetishism
For those experiencing on Debian and Ubuntu, the right thing to do if you manage redis via systemd is to just ignore the warning. redis attempts to create a PID file when started as a daemon, configured to be at /var/run/redis/redis.pid , and it's not configurable to not have it create one. But systemd already manages a PID file for you with PIDFile=/run/redis/redis-server.pid as you can see above. It's better this way than to have an ad-hoc PID file created.Markusmarl
Thanks, this is worked for me on the ubuntu 20.04 version too.Younker
Fixed indeed using Ubuntu 20.04 LTSSynopsis
F
49

On my Ubuntu 18.04, I was getting the same error.

Error reported by redis (on /var/log/redis/redis-server.log):

 # Creating Server TCP listening socket ::1:6379: bind: Cannot assign requested address

This is because I've disabled IPv6 on this host and redis-server package (version 5:4.0.9-1) for Ubuntu comes with:

bind 127.0.0.1 ::1

Editing /etc/redis/redis.conf and removing the ::1 address solves the problem. Example:

bind 127.0.0.1

Edit: As pointed out in the comments (thanks to @nicholas-vasilaki and @tommyalvarez), by default redis only allows connections from localhost. Commenting all the line, using:

# bind 127.0.0.1 ::1

works, but makes redis listen from the network (not only from localhost).

More details can be found in redis configuration file.

Fireboat answered 5/11, 2018 at 19:34 Comment(8)
This must not have been related to my problem because the step didn't help me. sudo systemctl status redis resulted in redis-server.service: Can't open PID file /var/run/redis/redis-server.pid (yet?) after start: No such file or directory, but that file does exist and is owned by "redis". Removing IPv6 binding didn't help.Pampas
I also faced this. There error in redis log was Creating Server TCP Listening socket ::1:6379: unable to bind socket, errno 97Einhorn
@Fireboat # bind 127.0.0.1 ::1 opens redis server to all "Hello world", isn't it?Contraposition
@NicholasVasilaki # bind 127.0.0.1 ::1 disables listen on port ::1. It closes redis to IPv6 clients.Fireboat
@Fireboat if only remove ::1 - yes. But if comment whole line as in example, than we will have next: monosnap.com/file/kzDbSaZDAyO4YcegsQR6sEhObBv6BDContraposition
@NicholasVasilaki You are right. With bind 127.0.0.1 you only have access from localhost.Fireboat
The answer should be edited to only comment out the ::1 part. Otherwise you expose your redis to the worldRafat
I didn't get this error on Ubuntu 18.04 (and hadn't made any special network settings to disable IPv6 AFAIK). I found the ExecStartPost suggestion from @sebuhi did the trick.Riel
M
28

Problem was that the user redis did not have permission to create the pid file (or directory it was in). Fix:

sudo mkdir /var/run/redis
sudo chown redis /var/run/redis

Then I killed and restarted redis and sure enough, there was redis.pid

Madoc answered 27/8, 2014 at 14:10 Comment(2)
My pidfile path was set as pidfile="/var/run/redis.pid" so redis couldn't create the file because of permissions on /var/run/, this solution helped me figured out my case, thanksGentes
This is not enough on Ubuntu 18.04. Permissions are fine. Sometimes the PID file is there, sometimes it isn't. No logic to it. This makes is hard to monitor Redis.Legitimize
E
19

i had a similar problem on Debian Buster, systemd complains about the missing PID file, even though the file exists and redis is running.

on my system the solution using "echo $MAINPID > /run/redis/redis.pid" works by accident, although/because the real PID file is set to /run/redis/redis-server.pid (spot the different filenames!) and on my system the content of /run/redis/redis.pid (the one of the echo) was empty.

in a discussion on [email protected] someone writes:

... systemd will add the MAINPID environment variable any time it knows what the main PID is. It learns this by reading the PID file ... So by the time ExecStartPost runs, the main PID may or may not be known.

having an empty MAINPID environment variable can be even harmful: if you notice the different PID filenames in the suggested solution, and correct it, you may end up in a situation where the PID file written by redis gets overwritten by an empty file. this happened to me, the result was that systemctl start redis.service never finished.

i also noticed that another server with 100% same OS and configuration, but different hardware did not have this problem.

my conclusion is that it just hits some sort of race condition, systemd seems to look for a PID file just a little too early. on my system, whatever command i used as ExecStartPost, it will add enough delay to make the error disappear.

therefore a solution is to use "sleep 1" (sleep 0.1 works too, but 1 second may be on the safe side):

ExecStartPost=/bin/sleep 1

/etc/systemd/system/redis.service now looks like:

[Service]
Type=forking
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStartPost=/bin/sleep 1
ExecStop=/bin/kill -s TERM $MAINPID
PIDFile=/run/redis/redis-server.pid
...

an alternative solution is to use "supervised systemd":

/etc/redis/redis.conf:

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

override the redis-server.service file using:

systemctl edit redis-server.service

and enter the following:

[Service]
Type=notify

reload the service and the error should be gone:

sudo systemctl restart redis.service
sudo systemctl status redis.service 
Easton answered 7/7, 2020 at 0:42 Comment(2)
Thank you; the Type=notify bit in the systemd service file was what worked for me on Debian 10 (buster)Etheridge
Bingo! This is the solution to this cryptic error.Belie
L
17

In CentOs 7 i need to add to the file:

$ vi /usr/lib/systemd/system/redis.service

The next line:

ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"

And then restart the service:

$ sudo systemctl daemon-reload
$ sudo systemctl restart redis.service

Reference:

CentOs 7: Systemd & PID File

Loritalorn answered 3/1, 2017 at 22:41 Comment(4)
this is exactly what is needed!Acquittance
Worked on Fedora. Thank you!Lacroix
This works using Ubuntu 18.04, however the file redis.service resides in path /etc/systemd/system/.Vulgus
@JoshuaFlood /etc/systemd/system/redis.service It is a symbolic link. In Ubuntu 18.04 resides in path: /lib/systemd/system/redis-server.service.Ostracon
M
3

Here from 2018

Before start, I am on Ubuntu 18.04.I wrote this if anyone comes here by searching same error.

In my case error is the same but problem is so different. No solutions that proposed here worked.

So I checked logs if they are exist and looked for is there anything useful. Found them on;

cat /var/log/redis/redis-server.log

Searched logs and found that problem is that another service is listening same port.

2963:C 21 Sep 11:07:33.007 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2963:C 21 Sep 11:07:33.008 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=2963, just started
2963:C 21 Sep 11:07:33.008 # Configuration loaded
2974:M 21 Sep 11:07:33.009 # Creating Server TCP listening socket 127.0.0.1:6379: bind: Address already in use 

I checked who is listening.

netstat anp | grep 6379

Found it.

tcp6       0      0 :::6379                 :::*                    LISTEN      3036/docker-proxy   

It was docker image of redis that installed by another tool

root@yavuz:~# docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                   PORTS                    NAMES
a6a94d401700        redis:3.2                   "docker-entrypoint.s…"   20 hours ago        Up 3 hours               0.0.0.0:6379->6379/tcp   incubatorsuperset_redis_1

So I stopped docker image

root@yavuz:~# docker stop incubatorsuperset_redis_1

And redis-server started without problem.

root@yavuz:~# systemctl start redis-server
root@yavuz:~# systemctl status redis-server
● redis-server.service - Advanced key-value store
   Active: active (running) since Fri 2018-09-21 11:10:34 +03; 1min 49s ago
  Process: 3671 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Miculek answered 21/9, 2018 at 8:35 Comment(0)
T
1

For CentOS:

In my case name of Redis server is redis.service, start it edit

systemctl edit redis.service

Add this:

[Service]

ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"
PIDFile=/var/run/redis/redis.pid

Im my case it create file: /etc/systemd/system/redis.service.d/override.conf

After restart service:

systemctl daemon-reload
systemctl restart redis

And the pid file is:

cat /var/run/redis/redis.pid 
=> 19755
Trolley answered 2/8, 2021 at 14:39 Comment(0)
G
1

From comments in /etc/redis/redis.conf:

Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

Redis attempts to create a PID file when started as a daemon, configured in the /etc/redis/redis.conf (thanks to @jhanschoo). But it try to check creation of /run/redis/redis.pid...

You can manually change path to pidfile in redis config: nano /etc/redis/redis.conf Go to line pidfile /run/redis/redis-server.pid and change it to pidfile /run/redis/redis.pid.
Then change systemd service file: nano /etc/systemd/system/redis.service

Change line below [Service] from PIDFile=/run/redis/redis-server.pid to PIDFile=/run/redis/redis.pid

Also you can change first line below [Service] to Type=forking to prevent "Start request repeated too quickly" error.

You get something like:

[Service]
Type=forking
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
PIDFile=/run/redis/redis.pid
...

Then restart:

sudo systemctl daemon-reload
sudo systemctl restart redis.service

Hope, it helps..

Gurango answered 4/12, 2023 at 15:37 Comment(0)
F
0
sudo nano /etc/redis/redis.conf

Inside the file, find the supervised directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The supervised directive is set to no by default. Since you are running Ubuntu, which uses the systemd init system, change this to systemd.

Ferrocyanide answered 23/5, 2020 at 14:47 Comment(0)
H
0

My default, Redis does not run as a daemon, and that is why it does not create a pid file. If you look at /etc/redis/redis.conf, it says so explicitly under General.

#By default Redis does not run as a daemon.  Use 'yes' if you need it...
daemonize no

So all you need to do is to change it to daemonize yes

Hydrolysate answered 8/2, 2022 at 17:4 Comment(0)
P
0

my solution is below:

ExecStartPost=/bin/sh -c "cp /var/run/redis/redis-server.pid /var/run/redis/redis.pid"
Pronouncement answered 10/10, 2023 at 5:53 Comment(0)
I
-1

For people struggling with getting it to work on Ubuntu 18.04 you need to edit /etc/redis/redis.conf and update the pidfile declaration to following:

pidfile "/var/run/redis/redis-server.pid"
Indemnify answered 11/11, 2019 at 12:0 Comment(0)
S
-1

Ubuntu 18. /var/run/redis had the wrong permissions: drwxr-sr-x 2 redis redis 60 Apr 27 12:22 redis

Changed to 755 (drwxrwxr-x) and the pid file now appears.

Shirker answered 27/4, 2020 at 4:28 Comment(1)
This change (at least alone) didn't fix the problem for me on Ubuntu 18.04 (also, 755 != drwxrwxr-x = 775)Riel

© 2022 - 2024 — McMap. All rights reserved.