DEV Community

Bo
Bo

Posted on

Systemd: start supervisor after redis on reboot

I am using supervisor to run my web application on system boot, and one of the application is using redis, so that requires redis start first.

supervisor doesn't have a good way to support start dependency, but systemd support this.

Steps

0, Create your redis start config file, e.g. 6379.conf, and make sure the daemonize is set to no

1, Create redis systemd file redis.service:

[Unit]
Description=Redis
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /home/redis/6379.conf
ExecStop=kill -s HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

2, Copy this file to path:

cp redis.service /lib/systemd/system/
Enter fullscreen mode Exit fullscreen mode

3, Start redis with systemd:

systemctl start redis
Enter fullscreen mode Exit fullscreen mode

4, Set start redis on system boot:

systemctl enable redis
Enter fullscreen mode Exit fullscreen mode

If you changed the service file content, you need to reload it:

systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

5, Go to folder /etc/systemd/system/multi-user.target.wants, and find supervisor.service, under the [Unit] section, add line:

Requires=redis.service
Enter fullscreen mode Exit fullscreen mode

All done. Now on the system reboot, redis will start before supervisor, so if you applications are booted by supervisor, now they can safely connect to redis.

Discussion (0)