-
-
Save geschke/ab6afa91b2d9dfcd5c25 to your computer and use it in GitHub Desktop.
[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 |
btw, restart seems to work, but reload doesn't. I looked in the redis docs and I don't think it implements any way to reload, although there are commands to modify the config once it's loaded. But that doesn't reload an entire config file.
The log when i reload:
● redis.service - Redis Datastore Server
Loaded: loaded (/lib/systemd/system/redis.service; disabled)
Active: active (running) since Wed 2016-05-04 11:49:02 CDT; 4s ago
Process: 1989 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=1/FAILURE)
Process: 1986 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS)
Process: 1992 ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 1993 (redis-server)
CGroup: /system.slice/redis.service
└─1993 /usr/local/bin/redis-server *:6379
@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
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
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
-
sudo vim /etc/systemd/system/redis.service
-
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
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
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.
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
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
I had several problems with this. My solution is below. I'm really new to systemd, so i'll be a little more verbose for other beginners. My example below is working on debian jessie (8) with redis 3.0.7
Final startup script in /lib/systemd/system/redis.service:
The redis conf file has to be updated as well. Make sure it's in the right location as above. You will need to update the following: