您的位置:首页 > 汽车 > 新车 > 石湾网站设计制作_揭阳企业网站建设公司_百度小程序优化_阿里云服务器

石湾网站设计制作_揭阳企业网站建设公司_百度小程序优化_阿里云服务器

2025/8/22 2:38:30 来源:https://blog.csdn.net/qq_35241329/article/details/146008531  浏览:    关键词:石湾网站设计制作_揭阳企业网站建设公司_百度小程序优化_阿里云服务器
石湾网站设计制作_揭阳企业网站建设公司_百度小程序优化_阿里云服务器

AI越来越火了,我们想要不被淘汰就得主动拥抱。推荐一个人工智能学习网站,通俗易懂,风趣幽默,最重要的屌图甚多,忍不住分享一下给大家。点击跳转到网站

前面我们说过了如何将jar交由Systemctl管理,下面我们说说如何为被Systemctl管理的jar设置重启脚本。

  1. Systemd 服务单元文件
    首先最重要的一点是确保jar已经被Systemctl管理,前面交由其管理的是名为 hello.jar 的核心文件,并定义了如何启动、停止和重启 JAR 文件。需要确保这个文件已经存在并正确配置。

该文件位置:

 /etc/systemd/system/hello.service
  1. Systemd 定时器单元文件 (hello.timer)
    这是我们提供的定时器文件,用于每天凌晨 3 点触发重启服务。它的内容如下:
[Unit]
Description=Restart Hello Service Daily[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
Unit=hello.service  # 明确指定要触发的服务[Install]
WantedBy=timers.target
  • OnCalendar:设置定时任务的时间(每天凌晨 3 点)。
  • Persistent=true:如果系统在定时任务触发时关机,任务会在下次启动时执行。

该文件位置:/etc/systemd/system/hello.timer。

  • 启用定时器:sudo systemctl enable hello.timer
  • 关闭定时器: systemctl disable hello.timer
  • 检查是否已启用: systemctl is-enabled hello.timer
  • 立即启动定时器:sudo systemctl start hello.timer
  • 检查定时器状态:systemctl list-timerssudo systemctl status hello.timer
  • 查看定时器日志: journalctl -u hello.timer
  • 测试定时器: systemd-analyze calendar "*-*-* 03:00:00"
  • 修改配置后重新加载:加载配置-> sudo systemctl daemon-reload ;重启定时器->sudo systemctl restart hello.timer
  1. 定时器如何工作
  • 当定时器触发时,Systemd 会自动调用与定时器同名的服务(即 hello.service)。

  • hello.service 会执行 ExecStart 中的命令(即启动 JAR 文件)。

  • 如果服务已经在运行,Systemd 会先停止服务,然后重新启动它。

  1. 验证
  • 查看定时器状态:systemctl list-timers

应该看到:

[root@hcss-ecs-1675 usr]# systemctl list-timers
NEXT                         LEFT       LAST                         PASSED       UNIT                         ACTIVATES
Tue 2025-03-04 13:43:00 CST  11min left Tue 2025-03-04 11:47:53 CST  1h 44min ago dnf-makecache.timer          dnf-makecache.service
Tue 2025-03-04 14:00:00 CST  28min left n/a                          n/a          hello.timer                  hello.service
Wed 2025-03-05 00:00:00 CST  10h left   Tue 2025-03-04 09:07:01 CST  4h 24min ago unbound-anchor.timer         unbound-anchor.service
Wed 2025-03-05 09:21:53 CST  19h left   Tue 2025-03-04 09:21:53 CST  4h 10min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service4 timers listed.
Pass --all to see loaded but inactive timers, too.

问题:为什么我按照配置做了,定时脚本也运行了,但我的jar仍然没有重启?
分析:定时器hello.timer的作用指定时间触发hello.service,但如果触发时hello.service已经在运行了,系统不会重新启动它,除非服务配置允许重启。但一般情况下定时器只会触发类型为“start”的操作,而不是“restart”。
解决方案:通过中间服务触发重启。

  1. 创建重启专用服务单元
    新建文件 /etc/systemd/system/hello-restart.service:
[Unit]
Description=Restart Hello Service[Service]
Type=oneshot
ExecStart=/bin/systemctl restart hello.service[Install]
WantedBy=multi-user.target
  1. 修改定时器配置指向中间服务
#更新 /etc/systemd/system/hello.timer
[Unit]
Description=Restart Video Service Daily[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
Unit=hello-restart.service  # 指向新服务单元[Install]
WantedBy=timers.target
  1. 重载配置并启用
sudo systemctl daemon-reload
sudo systemctl enable hello-restart.service
sudo systemctl restart hello.timer

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com