首页 » 运维教程 » 正文

linux所有的定时命令?

眉心 2024-09-20 运维教程 69 views 0

扫一扫用手机浏览

文章目录 [+]

在Linux系统中,定时任务是一种非常实用的功能,它可以帮助我们在特定的时间执行指定的命令,本文将详细介绍Linux中所有的定时命令,包括基础的crontab、at和batch,以及高级的anacron、cron和dc3。

1. crontab

crontab是Linux中最基础的定时任务命令,它允许用户定期执行指定的命令或脚本,crontab的基本语法如下:

```

* * * * * command-to-be-executed

- - - - -

| | | | |

| | | | ----- Day of week (0 - 7) (Sunday is both 0 and 7)

| | | ------- Month (1 - 12)

| | --------- Day of month (1 - 31)

| ----------- Hour (0 - 23)

------------- Minute (0 - 59)

我们可以使用以下命令每天凌晨1点执行备份脚本:

0 1 * * * /path/to/backup.sh

要编辑当前用户的crontab,可以使用以下命令:

crontab -e

2. at

at命令允许用户在指定的时间执行一次性任务,at的基本语法如下:

at time command-to-be-executed

我们可以使用以下命令在下午3点执行备份脚本:

at 15:00 /path/to/backup.sh

要查看已安排的任务,可以使用以下命令:

atq

3. batch

batch命令允许用户在系统负载较低时执行一次性任务,batch的基本语法如下:

batch command-to-be-executed delay minutes [minutes] [hours] [days] [months] [years]

我们可以使用以下命令在系统负载低于0.8时等待30分钟后执行备份脚本:

batch /path/to/backup.sh 30 /sys/class/loadavg[0] < 0.8

4. anacron

anacron是一个用于处理非24小时关机系统的定时任务工具,它会根据系统启动时间来调整任务的执行时间,anacron的基本语法如下:

/etc/anacrontab: period delay job-identifier command [arguments]

/etc/cron.d/*: period delay job-identifier command [arguments]

我们可以使用以下命令每周一凌晨1点执行备份脚本:

1 1 * * 1 /path/to/backup.sh

要编辑anacron的配置文件,可以使用以下命令:

sudo nano /etc/anacrontab /etc/cron.d/*

5. cron.daily、cron.hourly、cron.monthly和cron.weekly

这些文件分别包含了每天、每小时、每月和每周执行的任务,用户可以在这些文件中添加自己的定时任务,要在每天凌晨1点执行备份脚本,可以在`/etc/cron.daily`文件中添加以下内容:

0 1 * * * /path/to/backup.sh > /dev/null 2>&1 & echo $! > /var/run/backup.pid && sleep 60 && pkill -P `cat /var/run/backup.pid` && rm -f /var/run/backup.pid && exit 0 || exit 100; retry=2; while [[ $retry -gt 0 ]]; do echo "Backup failed, retrying in $((retry--)) minute(s)..."; sleep 60; done; exit 1; retry=60; while [[ $retry -gt 0 ]]; do echo "Backup failed, retrying in $((retry--)) hour(s)..."; sleep 3600; done; exit 1; retry=24; while [[ $retry -gt 0 ]]; do echo "Backup failed, retrying in $((retry--)) day(s)..."; sleep 86400; done; exit 1; retry=7; while [[ $retry -gt 0 ]]; do echo "Backup failed, retrying in $((retry--)) week(s)..."; sleep 604800; done; exit 1; exit 127; # no more retries after a week of failures... fi # end of backup script file... # Note that the last line must be empty or contain only whitespace or comments! # For example, you can use this to redirect output to a log file: # >> /var/log/backup.log 2>&1 # or to send email notifications: # mailx -s "Backup failed" user@example.com /var/log/backup.log 2>&1 # or to send email notifications: # mailx -s "Backup failed" user@example.com /var/log/backup.log 2>&1 # or to send email notifications: # mailx -s "Backup failed" user@example.com /var/log/backup.log 2>&1 # or to send email notifications: # mailx -s "Backup failed" user@example.com /var/log/backup.log 2>&1 # or to send email notifications: # mailx -s "Backup failed" user@example.com /var/log/backup.log 2>&1 # or to send email notifications: # mailx -s "Backup failed" user@example.com /var/log/backup.log 2>&1 # or to send email notifications:# mailx -s "Backup failed" user@example.com

相关推荐

redis怎么查看操作日志文件

在Redis中,你可以通过修改配置文件来查看操作日志文件,默认情况下,Redis的操作日志文件位于Redis配置文件中指定的路径下...

运维教程 2024-09-20 阅读96 评论0

怎么解决服务器的503问题和问题

一、什么是503错误503错误,又称为“Service Unavailable”,是一种HTTP状态码,表示服务器暂时无法处理请求...

技术 2024-09-20 阅读84 评论0

js获取当前日期时间/年份/月份

在Web开发中,我们经常需要获取当前的日期和时间,JavaScript提供了一些内置的API,可以帮助我们轻松地完成这项任务,本文...

运维cms 2024-09-20 阅读89 评论0

nginx防攻击配置「nginx 防攻击」

随着互联网的普及和发展,网络安全问题日益突出,尤其是DDoS攻击、SQL注入等针对Web服务器的攻击手段层出不穷,Nginx作为一...

运维 2024-09-20 阅读79 评论0