ubuntu 20.04 使用systemd创建自定义服务

2020-08-21  本文已影响0人  小桥流水啦啦啦

1、/lib/systemd/system下创建test.service文件

sudo vim /lib/systemd/system/test.service

[Unit]

Description=test

 

[Service]

Type=forking

PIDFile=/run/test.pid

#EnvironmentFile=/etc/systemd/test.conf

ExecStart=/opt/systemd-sh/test.sh

ExecReload=/bin/kill -SIGHUP $MAINPID

ExecStop=/bin/kill -SIGINT $MAINPID

 

[Install]

WantedBy=multi-user.target graphical.target

2、修改配置文件后需要重加载配置

sudo systemctl daemon-reload

3、创建test.sh

sudo vim /opt/systemd-sh/test.sh

#!/bin/bash
  
echo `date`,"ok" >>/tmp/test.log

4、赋予可执行权限

sudo chmod +x /opt/systemd-sh/test.sh

5、设置开机启动

systemctl enable test.service

会显示:

Created symlink /etc/systemd/system/multi-user.target.wants/test.service → /lib/systemd/system/test.service.

6、重启系统

reboot

7、重启后,看下/tmp/test.log的内容

vim /opt/test.log

8、systemd集中式日志管理
查看该unit的日志

sudo journalctl -u test.service

(完)

上一篇 下一篇

猜你喜欢

热点阅读