首页 > 系统相关 >Linux系统计划任务之系统定时重启任务

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

时间:2023-02-22 13:32:45浏览次数:40  
标签:onecloud 系统 crontab cron etc 任务 usr Linux root

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

一、crontab介绍

Linux crontab 是用来定期执行程序的命令。 crond 命令每分钟会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作

二、crontab相关目录

1./var/spool/cron/

/var/spool/cron/ 目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名。

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

/etc/crontab 这个文件负责调度各种管理和维护任务。

root@onecloud:~# cat  /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't 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/

/etc/cron.d/ 这个目录用来存放任何要执行的crontab文件或脚本。

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.*目录

可以把脚本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目录中,让它每小时/天/星期、月执行一次。

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

标签:onecloud,系统,crontab,cron,etc,任务,usr,Linux,root
From: https://blog.51cto.com/u_14664141/6078882

相关文章

  • linux源码解析15- 匿名页面的生命周期
    1.匿名页面的产生从内核角度看,如下情况会产生匿名页面:1.1用户空间malloc()/mmap()分配的内存;在内核发生缺页中断时,调用do_anonymous_page()产生匿名页;1.2写时复制:当......
  • 为什么选择软考《数据库系统工程师》科目?
    1、数据库和软考哪个科目联系较多?区别?难度?相同点? (1)从前后的科目衔接来看。在软考系列中,中级《数据库系统工程师》的初级科目是《信息系统运行管理员》,高级科目可以对应《信......
  • WorkPlus“Meet”,让企业高效协作的视频会议系统
    随着办公方式的多元化,数智化办公正在逐渐进入企事业单位。保持员工移动和远程高效工作的关键是为他们提供正确的协作工具以及最佳的连接性。高效的音视频会议系统成为办公......
  • 在线客服系统租用功能的实现
    前言实现在线客服系统的租用功能,并为您提供更多的选择,帮助您拓展您的业务。我的客服系统支持在线租用(gofly.v1kf.com),使用以下方式实现了租用功能确定租用方式:您可以考......
  • Linux系统调用实现方式的演进
    系统调用是用于用户空间和内核空间之间接口的API,是用户程序与内核进行通信的一种方式,系统调用和普通库函数调用非常相似,只是系统调用由操作系统内核提供,运行于内核核心态,......
  • Linux操作命令(一)ls命令 2.cd命令 3.pwd命令
    1、ls命令ls命令就是list的缩写,ls用来打印出当前目录的清单。参数描述-a–all列出目录下的所有文件,包括以.开头的隐含文件-l除了文件名之外,......
  • Linux - deepin 15 正式版 启动器打不开
    1.执行pkillyuan@LABELNET:~$pkilldde-launcher2.重新运行yuan@LABELNET:~$dde-launcher......
  • hadoop - hadoop2.6 分布式 - 集群环境搭建 - 系统搭建和网络配置
    1.配置   我的搭建环境是个人笔记本(deepinlinux)+VirtualBox(3个ubuntulinux);   宿主计算机(个人笔记本)配置如下(很低):            ......
  • WorkPlus“Meet”,让企业高效协作的视频会议系统
    随着办公方式的多元化,数智化办公正在逐渐进入企事业单位。保持员工移动和远程高效工作的关键是为他们提供正确的协作工具以及最佳的连接性。高效的音视频会议系统成为办公的......
  • 2Linux系统的启动过程
     系统启动过程的5个阶段:1、内核引导阶段;当计算机打开电源后,首先是BIOS开机自检,按照BIOS中设置的启动设备(通常是硬盘)来启动。2、运行init阶段;init进程是系统所有进程的......