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
相关文章
|
9月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
823 3
Linux系统禁用swap
|
9月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
1398 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
887 3
Linux系统初始化脚本
|
9月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
1046 1
|
9月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1352 1
|
Linux
Linux关机和重启命令
Linux关机和重启命令
174 0
|
Linux 数据安全/隐私保护
Linux关机与重启命令
Linux关机与重启命令
353 0
|
Linux Shell Windows
linux(九)关机重启命令
接下来,我们来看一下linux 关机与重启相关的命令。 Linux 不同于我们的windows系统,时间长了不关机卡的要死,而且linux一般作为服务器使用,一般是不会关机的。耽误事啊。 这部分的内容比较枯燥,基本上就是照本宣科,但是,还是很有必要了解一下的,说实话,下面的很多命令我之前也不知道。 一:关机命令 关机命令这里大概有4个,我下边分别来看。 1:halt:立刻关机 若系统的 runlevel 为 0 或 6 ,则Linux halt命令关闭系统,否则以 shutdown 指令(加上 -h 参数)来取代。 (1):参数: -n : 在关机前不做将记忆体资料写回硬盘的动作
509 0
|
安全 Linux
Linux常用命令(8)——关机和重启命令
Linux常用命令(8)——关机和重启命令
476 0
|
移动开发 网络协议 Linux
Linux常用命令(网络命令和关机重启命令)
Linux常用命令(网络命令和关机重启命令)
429 0