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

更新日期: 2019-07-20 阅读次数: 19524 字数: 868 分类: Linux

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

测试环境

  • ubuntu 18.04
  • ubuntu 16.04

安装 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

配置说明

  • After=network.target 代表 run redis after network service has been started.
  • Restart=always 代表无论什么原因 redis 进程被干死,都会自动将 redis 重启。
  • multi-user is the systemd equivalent of runlevel 3, which is a multi-user system that boots to a console, not a GUI. The equivalent of runlevel 5, which boots to X, is graphical.target. 而 ubuntu 下 runlevel2~5 没有区别都是 Full multi-user with display manager (GUI) 。在命令行下输入 runlevel 得到的结果永远是 N 5.

使用 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 的原因

  • ubuntu 14.04 server 之后都是内置的 systemd,不需要再下载安装、配置
  • 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 操作,则无法在系统重启时调用。

参考

  • https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04

微信关注我哦 👍

大象工具微信公众号

我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式

tags: systemd