1. 程式人生 > >【樹莓派】crontab設置Linux設備定時重啟

【樹莓派】crontab設置Linux設備定時重啟

ngs started not crontab fields through hour 其他 his

簡介:設置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>

【樹莓派】crontab設置Linux設備定時重啟