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

简介: 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 -- {
   mathJaxContainer[0]})
*/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 这个文件负责调度各种管理和维护任务。
```cpp
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文件或脚本。

```cpp
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
相关文章
|
1天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
|
1天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
2天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
|
2天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
3天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
10 0
|
3天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
监控 Unix Linux
Linux系统之计划任务的配置与管理
Linux系统之计划任务的配置与管理
128 0
|
Linux 调度
​linux 进程和计划任务管理
进程和计划任务管理 要求: 管理系统中的进程 启动系统中的portmap服务,然后使用kill命令终止它的运行。 步骤: 使用service命令启动portmap服务并确认运行状态,使用pgrep命令查看portmap服务的进程,再使用kill命令终止。
1263 0