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