WebFaction
Community site: login faq
0
1

Newbie question.

I've just installed redis. I can run it manually with redis-server, but I'd like it to be running all the time. How can I ensure it's always running in the background, so that my apps can connect to it when needed?

Perhaps a cron task? Although I obviously don't want to restart it if it's already running.

asked 30 Oct '11, 16:04

AnnaPS
10711020
accept rate: 100%


You have to make it daemonized. This appears to be a setting for redis,

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

Try editing that setting to yes. You will also need to edit the pid path since /var is not writable by you,

# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis.pid
permanent link

answered 30 Oct '11, 17:08

johns ♦♦
5.3k212
accept rate: 23%

edited 30 Oct '11, 17:09

Thank you - and sorry, but I need the last bit spelling out - once I've made that edit, what exactly do I need to do to get Redis to start? And do I need to add a cron job to restart it, in case it falls over?

(03 Nov '11, 17:41) AnnaPS
1

You should be able to find the running server's PID using:

ps -u $USER -F |grep redis

You can then kill it using "kill <pid>" and restart it using redis-server as you mentioned in the question after making the chnages to the conf as shown above.
You can add a cronjob, it would be a full path to the redis-server executable, something like:

0,10,20,30,40,50 *  *  path-to-redis-in-your-home-dir

You can add it by following:
http://docs.webfaction.com/software/general.html?highlight=crontab#scheduling-tasks-with-cron

(03 Nov '11, 21:18) neeravk

Thanks. Will that cron job (a) kill any previous running versions of redis, (b) spawn ever-increasing numbers of redis instances or (c) simply start redis if it isn't already running? Forgive my ignorance, but I really need to ensure that only (c) is happening here.

(11 Nov '11, 14:55) AnnaPS

@AnnaPS (c) will happen. I tested it. You can test it with starting Redis (whit daemonize yes) multiple times in a row and ensuring after each start that only one process is running and the PID stays unchanged. Find the PID using neeravk's instructions above.

(17 Mar '13, 03:41) Akseli Palén

You might also want to include a @reboot rule to your crontab to start Redis immediately after server reboot.

See my tutorial for installing Redis on Webfaction for details and examples.

permanent link

answered 04 Sep '13, 15:04

Akseli Palén
5614
accept rate: 50%

Thanks for sharing!

(04 Sep '13, 16:08) seanf

Once a program is able to be run daemonized, the next important thing is to determine if running the program's start command initiates multiple instances of the program simultaneously, or if the program checks to determine if it is already running and does not start a new instance if this is the case.

There is no standard behavior in this respect; different programs will behave differently. To determine which is the case, simply run the activation command multiple times and observe the behavior (the command ps -u $USER -F will show you all of your running processes).

In the event that a program does fire up multiple instances of itself if you run it multiple times, then you will want to use a watchdog script with cron to regulate the process and cause it to be started up if it is not active.

Hope that helps!

permanent link

answered 11 Nov '11, 19:01

ryans ♦♦
5.0k73159
accept rate: 43%

edited 11 Nov '11, 19:02

Very helpful general advice, thank you. In this case it looks as though the Redis daemon doesn't restart, just carries on running - so no need for a watchdog script.

(14 Nov '11, 05:38) AnnaPS

I can't believe you need to use a cronjob to run the redis server in the background, isn't there a better way to do it. And once the redis server is launched in the background, I suppose it just will keep running or am I incorrect?

permanent link

answered 10 Jan '12, 16:11

larrybolt
111
accept rate: 0%

I actually managed to make it work, running src/redis-server& (notice the & sign) Also log level set to "notice" in the redis.conf doesn't mess up the shell. I hope I did right, please let me know if I didn't.

(10 Jan '12, 16:21) larrybolt

Set this in /etc/redis.conf daemonize yes

Then trigger redis-server with the conf file as arguement:

/usr/sbin/redis-server /etc/redis.conf

permanent link

answered 14 Aug '15, 10:05

prx
111
accept rate: 0%

you are probably referring to paths relative to your home directory right? It's impossible for a user to edit the /etc/redis.conf file, as it doesn't exist.

(14 Aug '15, 10:50) iliasr ♦♦

yes, use the full path of your redis.conf. /etc/redis.conf is the default path if you have done a yum or apt-get install. if you have complied, it will be different.

(14 Aug '15, 10:54) prx

yum or apt-get installs are not allowed in a WebFaction server. If one runs a redis server it will be a compiled one.

(14 Aug '15, 11:04) iliasr ♦♦

I've followed Akseli Palén's excellent tutorial to get redis running. Starting Redis is working ( I can log in with the client and SET and GET), but the stopping it with cat redis.pid | xargs kill isn't working because it appears the pid file isn't being created correctly.

I've edited the config file correctly: pidfile /home/me/webapps/myapp/redis.pid

but a pid file doesn't appear when redis is running.

permanent link

answered 27 Nov '15, 20:20

leehinde
45111
accept rate: 0%

@leedhinde I'm not sure why your pid file isn't being created.

As a workaround, assuming you have only one redis running, you can stop it with the following command:

pkill -u $USER redis-server
(27 Nov '15, 20:46) seanf

Thanks, that worked for stopping redis.

(28 Nov '15, 20:45) leehinde
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×81
×12

question asked: 30 Oct '11, 16:04

question was seen: 25,453 times

last updated: 28 Nov '15, 20:45

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM