Skip to content

Instantly share code, notes, and snippets.

@geschke
Last active January 10, 2021 20:11
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geschke/ab6afa91b2d9dfcd5c25 to your computer and use it in GitHub Desktop.
Save geschke/ab6afa91b2d9dfcd5c25 to your computer and use it in GitHub Desktop.
WARNING. OUTDATED! The information on this page is severely out of date. Don't use it! Ubuntu systemd service file for Redis. The server was updated to a Ubuntu 15.04 (Vivid Vervet) distribution. Redis was installed from the PPA from Roman Wookey (https://launchpad.net/~rwky/+archive/ubuntu/redis). After the switch to systemd instead of Upstart,…
[Unit]
Description=Redis Datastore Server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
ExecStartPre=/bin/mkdir -p /var/run/redis
ExecStartPre=/bin/chown redis:redis /var/run/redis
ExecStart=/sbin/start-stop-daemon --start --chuid redis:redis --pidfile /var/run/redis/redis.pid --umask 007 --exec /usr/bin/redis-server -- /etc/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
@hos7ein
Copy link

hos7ein commented May 8, 2016

@macdabby
tnx , it's work for me ;)

content of /usr/lib/systemd/system/redis.service file :

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
ExecStart=/usr/bin/redis-server /etc/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/bin/redis-shutdown
User=redis
Group=redis

[Install]
WantedBy=multi-user.target

and on /etc/redis.conf file I edit this line :

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

@udf2457
Copy link

udf2457 commented May 22, 2016

May I suggest this is cleaner than the original :

[Unit]
Description=Redis Datastore Server
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis_6379
User=redis
Group=redis

Environment=statedir=/var/run/redis
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p ${statedir}
ExecStartPre=/bin/chown -R redis:redis ${statedir}
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

@denise-sanders
Copy link

If you are having trouble getting macdabby's code to run, check the logfile you have specified in /etc/redis/redis.conf. It is helpful if you missed a permission or chown

@AlexBuitrago
Copy link

  1. sudo vim /etc/systemd/system/redis.service

  2. Add this in the file:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Type=forking

@raysarebest
Copy link

raysarebest commented Dec 22, 2017

Just to toss my hat in this ring, this is the unit file that ended up working for me. I'm using a self-compiled Redis, v=4.0.6 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=f5447a0fadf7b78a running on Ubuntu 16.04:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid

User=redis
Group=redis

RuntimeDirectory=redis
RuntimeDirectoryMode=0700

ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

You have to edit /etc/redis/redis.conf to include these lines:

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

@sshymko
Copy link

sshymko commented Feb 17, 2019

Just wanted to add that Redis server parameters, such as daemonize no, can be passed as CLI arguments --daemonize no.
Having all the settings in the service declaration file releases from maintaining consistency across multiple config files.

See the example: Gist.

@tharun208
Copy link

What configuration should I add in the [Unit] section to make the service-disabled by default. Or any other options to make this by code will be really helpful

@geschke
Copy link
Author

geschke commented Jan 10, 2021

Just a remark: The original gist is really, really old. Ubuntu has reached the 20.10 version, so today these hints are outdated.
If you want to handle a systemd service, read the instructions for systemctl. To disable a service, a command like
"sudo systemctl disable " will be used.
And/or use Google to search for recent documentation, there are plenty of websites which describe systemd/systemctl, e.g. here: https://linoxide.com/linux-how-to/enable-disable-services-ubuntu-systemd-upstart/

Thanks & Kind regards

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