Debian docker-compose 开机自启

将 rc.local 到系统服务

程序或脚本需要添加开机自启,最简单的方法就是使用rc.local

检查下系统中是否已有rc.local

1
2
3
cat /lib/systemd/system/rc.local.service
#
systemctl status rc-local.service

添加rc-local.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 以下为一整条命令,一起复制运行
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
EOF

新建rc-local文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 以下为一整条命令,一起复制运行
cat > /etc/rc.local <<EOF
#!/bin/sh
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
EOF

添加权限并设置开机自启

1
2
3
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local.service

检查状态:

1
systemctl status rc-local.service

返回Active:active信息,则成功。

最后我们就可以在/etc/rc.local里,添加开机的自启命令了。记住添加在exit 0之前。

docker-compose seafile 专业版开机自启

1
2
3
vi /etc/rc.local
#exit 0 上一行添加如下代码
/usr/bin/docker compose -f /home/docker-compose.yml up -d