systemd创建自定义服务(Ubuntu)


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

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

 

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

sudo systemctl daemon-reload

 

 

创建test.sh

vim /opt/systemd-sh/test.sh

 

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

赋予可执行权限 

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

 

设置开机启动

systemctl enable test.service

显示

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

 

重启

reboot

 

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

vim /opt/test.log

 

systemd集中式日志管理

查看该unit的日志

sudo journalctl -u test.service

 

sudo journalctl -u test.service --since="2018-11-11 11:11:11"
sudo journalctl -u test.service --since "30 min ago"
sudo journalctl -u test.service --since yesterday sudo journalctl -u test.service --since "2018-11-10" --until "2018-11-11 11:11"
sudo journalctl -u test.service --since 04:00 --until "2 hour ago"

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM