【树莓派】crontab设置Linux设备定时重启

简介:设置Linux设备定时重启或者关机

问题:有台设备每天总需要使用的人手动重启一下才可以正常工作,但是检查了日志,看起来服务一切都正常。时间和正确时间相差4mins。

解决办法:

1.增加定时任务,设置自动重启;

2.修改正确时间,并增加自动同步;

都是相对比较简单的命令,这里简单记录一下,可以供需要的同学参考;

 

增加定时任务,设置自动重启:

编辑crontab:

sudo  crontab -e

增加定时重启命令,查看crontab

复制代码

lifeccp@raspberrypi:~ $ sudo crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 10 * * * sudo -u root reboot 

复制代码

 

重启服务确保生效:

sudo service cron restart

 

sudo vi /etc/sudoers

 

其他:

需要root权限才能执行的crontab,比如shutdown关机 需要: sudo crontab -e
在编辑器中加入下面2行,先将时间记录到日志中,每天22点30分钟 执行关机命令,1分钟警告后会自动关机

30 22 * * * date >> /home/haochuang/temp/shutdown.log
30 22 * * * sudo -u root shutdown -h 1

 

</END>

 

posted @   念槐聚  阅读(1856)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2017-02-09 IDEA+Gradle相关资料
2017-02-09 【树莓派】Linux应用相关:自动删除n天前日志
2017-02-09 【树莓派】制作树莓派最小镜像:img裁剪瘦身
点击右上角即可分享
微信分享提示