Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Running redis using upstart on Ubuntu

Running redis using upstart on Ubuntu

I've been trying to understand how to setup systems from the ground up on Ubuntu. I just installed redis onto the box and here's how I did it and some things to look out for.

To install:

sudo apt-get install redis-server

That will create a redis user and install the init.d script for it. Since upstart is now the replacement for using init.d, I figure I should convert it to run using upstart.

To disable the default init.d script for redis:

sudo update-rc.d redis-server disable

Then create /etc/init/redis-server.conf with the following script:

description "redis server"

start on runlevel [23]
stop on shutdown

exec sudo -u redis /usr/bin/redis-server /etc/redis/redis.conf

respawn

What this is is the script for upstart to know what command to run to start the process. The last line also tells upstart to keep trying to respawn if it dies.

One thing I had to change in /etc/redis/redis.conf is to change daemonize yes to daemonize no. What happens if you don't change it is that redis-server will fork and daemonize itself, and the parent process goes away. When this happens, upstart thinks that the process has died/stopped and you won't have control over the process from within upstart.

Now you can use the folowing commands to control your redis-server:

sudo start redis-server
sudo restart redis-server
sudo stop redis-server

Hope this was helpful!

@caius

This comment has been minimized.

Show comment Hide comment
@caius

caius Jan 2, 2011

It was helpful - thanks.

caius commented Jan 2, 2011

It was helpful - thanks.

@aoberoi

This comment has been minimized.

Show comment Hide comment
@aoberoi

aoberoi Sep 10, 2011

sweet!

aoberoi commented Sep 10, 2011

sweet!

@mschwar99

This comment has been minimized.

Show comment Hide comment
@mschwar99

mschwar99 Sep 14, 2011

+1 to you. Thanks!

+1 to you. Thanks!

@ganeshnaidu

This comment has been minimized.

Show comment Hide comment
@ganeshnaidu

ganeshnaidu Mar 25, 2012

Thanks

Thanks

@gremmie

This comment has been minimized.

Show comment Hide comment
@gremmie

gremmie May 4, 2012

Thank you.

gremmie commented May 4, 2012

Thank you.

@SamDuvall

This comment has been minimized.

Show comment Hide comment
@SamDuvall

SamDuvall Oct 8, 2012

Great article. Thank you!

Great article. Thank you!

@nathancahill

This comment has been minimized.

Show comment Hide comment
@nathancahill

nathancahill Oct 18, 2012

Thanks, very helpful.

Thanks, very helpful.

@jmfederico

This comment has been minimized.

Show comment Hide comment
@jmfederico

jmfederico Nov 5, 2012

THANKS!!!

THANKS!!!

@phpguru

This comment has been minimized.

Show comment Hide comment
@phpguru

phpguru Jan 14, 2013

Works perfectly, thanks. The biggest surprise is setting daemonize to no in redis.conf.

phpguru commented Jan 14, 2013

Works perfectly, thanks. The biggest surprise is setting daemonize to no in redis.conf.

@twoolie

This comment has been minimized.

Show comment Hide comment
@twoolie

twoolie Jan 24, 2013

check out an updated version that works with default redis.conf https://gist.github.com/4617553

twoolie commented Jan 24, 2013

check out an updated version that works with default redis.conf https://gist.github.com/4617553

@derEremit

This comment has been minimized.

Show comment Hide comment
@derEremit

derEremit Feb 12, 2013

about that "daemonize no":
http://upstart.ubuntu.com/cookbook/#expect

@cvrebert

This comment has been minimized.

Show comment Hide comment
@cvrebert

cvrebert Mar 28, 2013

I think you can setuid redis instead of using sudo -u

I think you can setuid redis instead of using sudo -u

@zampino

This comment has been minimized.

Show comment Hide comment
@zampino

zampino May 10, 2013

very nice

zampino commented May 10, 2013

very nice

@em-cliqz

This comment has been minimized.

Show comment Hide comment
@em-cliqz

em-cliqz May 22, 2013

The "disable" option for update-rc doesn't completely remove the redis bits. Use the 'remove' option instead:
update-rc.d -f redis-server remove

The "disable" option for update-rc doesn't completely remove the redis bits. Use the 'remove' option instead:
update-rc.d -f redis-server remove

@rogerleite

This comment has been minimized.

Show comment Hide comment
@rogerleite

rogerleite Jul 4, 2013

@bdotdub Great article! I saw @twoolie fork and merged all in a shell script version: https://gist.github.com/rogerleite/5927948#file-redis-install-sh

@bdotdub Great article! I saw @twoolie fork and merged all in a shell script version: https://gist.github.com/rogerleite/5927948#file-redis-install-sh

@dh1tw

This comment has been minimized.

Show comment Hide comment
@dh1tw

dh1tw Aug 14, 2013

That did the trick. thanks!

dh1tw commented Aug 14, 2013

That did the trick. thanks!

@beckyb8

This comment has been minimized.

Show comment Hide comment
@beckyb8

beckyb8 Nov 8, 2013

You made it almost /too/ easy! Many thanks!

beckyb8 commented Nov 8, 2013

You made it almost /too/ easy! Many thanks!

@MonikaMahanthappa

This comment has been minimized.

Show comment Hide comment
@MonikaMahanthappa

MonikaMahanthappa Nov 18, 2013

Helpful

Helpful

@jzwolak

This comment has been minimized.

Show comment Hide comment
@jzwolak

jzwolak Nov 26, 2013

I also had to create the redis user since I didn't have one.

I used

adduser --system --no-create-home redis

And I created a directory for the database dump in /var/redis, chown redis /var/redis

And finally, changed the /etc/redis/redis.conf to have the following line

dir /var/redis

Thanks a bunch for this info!

jzwolak commented Nov 26, 2013

I also had to create the redis user since I didn't have one.

I used

adduser --system --no-create-home redis

And I created a directory for the database dump in /var/redis, chown redis /var/redis

And finally, changed the /etc/redis/redis.conf to have the following line

dir /var/redis

Thanks a bunch for this info!

@MattSurabian

This comment has been minimized.

Show comment Hide comment
@MattSurabian

MattSurabian Dec 20, 2013

Why not use setgid and setuid stanzas instead of sudo -u.

description "redis server"

# optional to run under the redis user
#setgid redis
#setuid redis

start on runlevel [23]
stop on shutdown
respawn 

script
  redis-server PATH_TO_CONF
end script

Why not use setgid and setuid stanzas instead of sudo -u.

description "redis server"

# optional to run under the redis user
#setgid redis
#setuid redis

start on runlevel [23]
stop on shutdown
respawn 

script
  redis-server PATH_TO_CONF
end script

@dh1tw

This comment has been minimized.

Show comment Hide comment
@dh1tw

dh1tw Mar 2, 2014

good tip to run change redis.conf -> deamonized no

dh1tw commented Mar 2, 2014

good tip to run change redis.conf -> deamonized no

@sr3d

This comment has been minimized.

Show comment Hide comment
@sr3d

sr3d Mar 22, 2014

nice write up. Very helpful for me to setup a new Redis instance

sr3d commented Mar 22, 2014

nice write up. Very helpful for me to setup a new Redis instance

@antonzaytsev

This comment has been minimized.

Show comment Hide comment
@antonzaytsev

antonzaytsev Apr 11, 2014

Thanks so much! Very helpful!

Thanks so much! Very helpful!

@beckyb8

This comment has been minimized.

Show comment Hide comment
@beckyb8

beckyb8 Aug 20, 2014

Worked a treat. Thank you!

beckyb8 commented Aug 20, 2014

Worked a treat. Thank you!

@JREAM

This comment has been minimized.

Show comment Hide comment
@JREAM

JREAM Apr 11, 2015

Thanks awesome

JREAM commented Apr 11, 2015

Thanks awesome

@algodave

This comment has been minimized.

Show comment Hide comment
@algodave

algodave Apr 14, 2015

Great job! Thanks

Great job! Thanks

@ctownsen357

This comment has been minimized.

Show comment Hide comment
@ctownsen357

ctownsen357 Oct 16, 2015

Just what I needed - thank you for the detailed write up!

Just what I needed - thank you for the detailed write up!

@tvdsluijs

This comment has been minimized.

Show comment Hide comment
@tvdsluijs

tvdsluijs Jul 13, 2016

Yes! Great! Thank you!!!!

Yes! Great! Thank you!!!!

@minhvn

This comment has been minimized.

Show comment Hide comment
@minhvn

minhvn Sep 28, 2016

Thank, great :)

minhvn commented Sep 28, 2016

Thank, great :)

@padi

This comment has been minimized.

Show comment Hide comment
@padi

padi Dec 28, 2016

Thank you! I would've missed that last detail on redis.conf.

padi commented Dec 28, 2016

Thank you! I would've missed that last detail on redis.conf.

@onepip

This comment has been minimized.

Show comment Hide comment
@onepip

onepip Jul 17, 2017

thank u

onepip commented Jul 17, 2017

thank u

@xcLtw

This comment has been minimized.

Show comment Hide comment
@xcLtw

xcLtw Aug 14, 2017

mark

xcLtw commented Aug 14, 2017

mark

@briancyzhang

This comment has been minimized.

Show comment Hide comment
@briancyzhang

briancyzhang Nov 28, 2017

mark. good post. thank you!

mark. good post. thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment