Fixing weird Redis server daemon
Depok,Few days ago I set up my Vagrant virtual machine to be used for work. At Cermati, we use Redis as our key-value storage. After I successfully installed Redis, I notice there’s a weird thing about managing the daemon using service in my Ubuntu 14.04 VM. When I did ps aux | grep redis, I saw the daemon listed but when I run service redis_6379 status, it said that redis was not running. It seemed that the redis daemon could not be managed using service.
After some time tinkering, I finally opened the init script in /etc/init.d/redis_6379 and took a look at the code. This was actually the first time I dig into an init script so upon reading the code, I did not recognize some of the commands like start-stop-daemon and status-of-proc. I searched and found this article really helpful in understanding basic init scripts stuff. What particularly relevant to my issue were those two commands: start-stop-daemon and status-of-proc. Upon RTFM, I found out that:
- Starting daemon with start-stop-daemon will create a pidfile containing the PID of the daemon process.
- status-of-proc will check for a status of a daemon by reading the pidfile and compare its content with the actual PID. If these two match, the daemon is reported as running. Otherwise, the daemon is not running.
- Stopping daemon with start-stop-daemon will delete the pidfile.
So my issue was very likely related to this pidfile. The default location of redis pidfile is /var/run/redis/6379.pid and when I checked, no /var/run/redis/ existed. So I created the directory, killed my redis daemon process, and started it again with sudo service redis_6379 start. However, the issue still persisted. After a little bit of searching, I found someone with the same issue. It turned out that the directory permission had not been set properly. Setting the ownership to user redis did the trick and I finally could manage my redis daemon with service.
But the problem was not over. When I powered off the VM and turned it on again, redis was listed in ps aux | grep redis but service redis_6379 status said redis was not running. What happened? Another searching and I figured out that /var/run/ is a temporary directory. Meaning that its content will be destroyed on shutdown. Thinking for a while, I had two solutions in mind:
- Write an init script to be run before redis daemon is started to create /var/run/redis/ directory.
- Use another directory for pidfile.
I chose the last option since it is just a matter of changing one line in the redis configuration file and one line in the redis init script. I created $HOME/.redis/ and tell redis to use that directory to store its pidfile. And now the problem is solved :-)