/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"

 

相关文章:

  • 2022-01-19
  • 2021-07-16
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2021-08-04
  • 2022-01-19
  • 2021-05-18
猜你喜欢
  • 2022-12-23
  • 2021-07-02
  • 2021-10-15
  • 2022-03-07
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案