CentOS 7 如何添加Service并自动启动

建立service配置文件

在/usr/lib/systemd/system目录建立一个文件[service_name].service,  内容类似:

[Unit]
Description=Docker Compose gitlab Service
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/data/docker/gitlab
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

 更新Service配置

sudo systemctl daemon-reload

 

启动服务及设置自动启动

sudo systemctl enable [service_name]

会在/etc/systemd/system目录下相应的target建立符号链接

sudo systemctl start [service_name]

参考:

https://scottlinux.com/2014/12/08/how-to-create-a-systemd-service-in-linux-centos-7/ 

posted on 2020-04-24 09:45  omage  阅读(296)  评论(0编辑  收藏  举报