ubuntu 上使用 systemd 重启时自启动 redis 及 laravel queue worker

发布时间: 2018-06-20 09:19:36 作者: 大象笔记

之前一直使用 supervisor 来管理进程,但是偶然发现 systemd 已经成为主流并内置于 Ubuntu,所以拿 redis 测试一下效果。

测试环境

安装 Redis

下载最新的 redis

https://redis.io/download

编译安装

tar xzvf redis-4.0.10.tar.gz
cd redis-4.0.10
make
make test
sudo make install

确认安装完成

$ which redis-cli 
/usr/local/bin/redis-cli

redis 的配置文件

sudo cp redis.conf /etc/
sudo vim /etc/redis.conf

supervised no

修改为

supervised systemd

指定 dir 参数的路径,方便备份 dump.rdb 文件。

redis 的 systemd 配置文件

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

新建一个 redis 的 systemd 配置文件

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

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

[Install]
WantedBy=multi-user.target

启用配置.

$ sudo systemctl enable redis.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.

若不 enable 配置,重启开机时,并不会调用该服务。

为了测试效果,可以重启系统

$ sudo reboot

配置说明

使用 systemd 启动 redis

$ sudo systemctl start redis
$ ps axuw | grep redis
zhongwei 31847  0.1  0.6  44760  6344 ?        Ssl  17:10   0:00 /usr/local/bin/redis-server 127.0.0.1:6379

查看 redis 进程的状态

$ sudo systemctl status redis
● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-06-19 17:10:14 CST; 1min 0s ago
 Main PID: 31847 (redis-server)
   CGroup: /system.slice/redis.service
           └─31847 /usr/local/bin/redis-server 127.0.0.1:6379 

查看 Redis 启动失败的日志

查看 systemd 启动失败的服务列表

$ systemctl --state=failed
  UNIT                               LOAD   ACTIVE SUB    DESCRIPTION                
● NetworkManager-wait-online.service loaded failed failed Network Manager Wait Online
● redis.service                      loaded failed failed Redis In-Memory Data Store 

使用 journalctl 查看具体的错误日志

journalctl -u redis -b
-- Logs begin at Wed 2018-05-23 09:52:37 CST, end at Wed 2018-06-20 08:12:41 CST. --
6月 20 07:54:43 zhongwei-Inspiron-3847 systemd[1]: Started Redis In-Memory Data Store.
6月 20 07:54:43 zhongwei-Inspiron-3847 redis-server[4027]: 4027:C 20 Jun 07:54:43.473 # Can't chdir to '/home/zhongwei/work/data/redis': No such file or directory
6月 20 07:54:43 zhongwei-Inspiron-3847 systemd[1]: redis.service: Main process exited, code=exited, status=1/FAILURE
6月 20 07:54:43 zhongwei-Inspiron-3847 systemd[1]: redis.service: Failed with result 'exit-code'.
6月 20 07:54:43 zhongwei-Inspiron-3847 systemd[1]: redis.service: Service hold-off time over, scheduling restart.
6月 20 07:54:43 zhongwei-Inspiron-3847 systemd[1]: redis.service: Scheduled restart job, restart counter is at 1.
6月 20 07:54:43 zhongwei-Inspiron-3847 systemd[1]: Stopped Redis In-Memory Data Store.

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

journalctl - Query the systemd journal

journal 日报,日志

systemd supervisor 的区别

https://gunes.io/2017/08/25/systemd-vs-supervisor/

我倾向于使用 systemd 的原因

使用 systemd 管理 laravel 队列

[Unit]
Description=SomeProject Laravel queues

[Service]
User=zhongwei
Group=zhongwei
Type=simple
Restart=on-failure
RestartSec=30
Nice=10
WorkingDirectory=/home/xxx/some_project/
ExecStart=/usr/bin/php artisan queue:listen
StandardOutput=null
TimeoutStartSec=30

[Install]
RequiredBy=multi-user.target

报错:You need tcl 8.5 or newer in order to run the Redis test

make[1]: Entering directory '/xxx/install/redis-4.0.10/src'
    CC Makefile.dep
You need tcl 8.5 or newer in order to run the Redis test
Makefile:242: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/xxx/install/redis-4.0.10/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2

解决方法,安装 tcl8.5

sudo apt-get install tcl8.5

为何叫 systemd

Because it's a system daemon, and under Unix/Linux those are in lower case, and get suffixed with a lower case d. And since systemd manages the system, it's called systemd.

检查是否启用服务

systemctl status redis.service

如果显示为 disabled,说明没有执行 enable 操作,则无法在系统重启时调用。

参考

我是一名山东烟台的开发者,联系作者