Linux系统计划任务之系统定时重启任务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统计划任务之系统定时重启任务相关的知识,希望对你有一定的参考价值。

(Linux系统计划任务之系统重启定时任务)

一、crontab介绍

二、crontab相关目录

1./var/spool/cron/

root@onecloud:~# cat /var/spool/cron/crontabs/root 
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.cee4jr/crontab installed on Fri Jun  3 09:00:04 2022)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
*/20 * * * * /sbin/ntpdate -u ntp.aliyun.com > /dev/null 2>&1                   
30 2 * * * /www/server/panel/certbot-auto renew >> /www/server/panel/logs/certbot.log

2./etc/crontab

root@onecloud:~# cat  /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you dont have to run the `crontab
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

3./etc/cron.d/

root@onecloud:/etc/cron.d# ls
armbian-truncate-logs  armbian-updates  php  sendmail  sysstat
root@onecloud:/etc/cron.d# cat php 
# /etc/cron.d/php@PHP_VERSION@: crontab fragment for PHP
#  This purges session files in session.save_path older than X,
#  where X is defined in seconds as the largest value of
#  session.gc_maxlifetime from all your SAPI php.ini files
#  or 24 minutes if not defined.  The script triggers only
#  when session.save_handler=files.
#
#  WARNING: The scripts tries hard to honour all relevant
#  session PHP options, but if you do something unusual
#  you have to disable this script and take care of your
#  sessions yourself.

# Look for and purge old sessions every 30 minutes
09,39 *     * * *     root   [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi

4./etc/cron.*目录

root@onecloud:/etc# cd cron.
cron.d/       cron.daily/   cron.hourly/  cron.monthly/ cron.weekly/

三、crontab常用命令

1.crontab -e

2.crontab -l

3.crontab -r

4.crontab -u

四、crontab的时间格式写法

*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 为0)
|    |    |    +---------- 月份 (1 - 12) 
|    |    +--------------- 一个月中的第几天 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

五、设置一个定时重启任务

1.设置定时任务

crontab -e


* 02 */1 * *  echo "the systemc reboot-$(date +\\%Y\\%m\\%d\\%H\\%M\\%S)"  >> /data/log/logs  && reboot

2.查看定时任务日志

root@onecloud:~# crontab -l
*/20 * * * * /sbin/ntpdate -u ntp.aliyun.com > /dev/null 2>&1                   
30 2 * * * /www/server/panel/certbot-auto renew >> /www/server/panel/logs/certbot.log
* 02 */1 * *   echo "the systemc reboot-$(date +\\%Y\\%m\\%d\\%H\\%M\\%S)"  >> /data/log/logs   && reboot


六、测试定时任务效果

root@onecloud:~# cd /data/log
root@onecloud:/data/log# ls
logs
root@onecloud:/data/log# cat logs 
the systemc reboot-20220722112538

以上是关于Linux系统计划任务之系统定时重启任务的主要内容,如果未能解决你的问题,请参考以下文章

[Linux之crond计划任务定时任务]

linux之定时任务

Linux定时任务之crontab

创建计划任务

任务计划程序-Windows2008定时重启

Linux系统之crond