(ubuntu操作系统)systemd创建自定义自启动服务

  1. /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
  1. 修改配置文件后需要重加载配置
sudo systemctl daemon-reload
  1. 编辑创建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"
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值