- /lib/systemd/system目录下创建test001.service文件
vim /lib/systemd/system/test001.service
/lib/systemd/system/test001.service 编辑完成后的文件内容
[Unit]
Description=test001
[Service]
Type=simple
PIDFile=/run/test001.pid
ExecStart=/opt/test001.sh
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target graphical.target
- 修改配置文件后需要重加载配置
sudo systemctl daemon-reload
- 编辑创建test001.sh脚本
vim /opt/test001.sh
脚本内容如下:
#! /bin/bash
while true;
do
echo `date`,"ok" >>/opt/test001.log
sleep 3600
done
脚本赋予可执行权限
chmod +x /opt/test001.sh
设置开机启动
systemctl enable test001.service
执行后显示效果
Created symlink from /etc/systemd/system/multi-user.target.wants/test001.service to /lib/systemd/system/test001.service.
Created symlink from /etc/systemd/system/graphical.target.wants/test001.service to /lib/systemd/system/test001.service.
重启服务器
reboot
重启后,看下/opt/test001.log的内容
vim /opt/test001.log
systemd集中式日志管理
查看该unit的日志
sudo journalctl -u test001.service
附录: 根据时间节点查看service 日志
sudo journalctl -u test001.service --since="2020-10-11 11:11:11"
sudo journalctl -u test001.service --since "30 min ago"
sudo journalctl -u test001.service --since yesterday
sudo journalctl -u test001.service --since "2020-10-10" --until "2020-10-11 11:11"
sudo journalctl -u test001.service --since 04:00 --until "2 hour ago"