失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > linux定时任务每两天 linux定时任务

linux定时任务每两天 linux定时任务

时间:2019-09-07 17:12:06

相关推荐

linux定时任务每两天 linux定时任务

1、先来一个小小的例子

查看当前路径:

[root@roottest]#pwd

‍/home/admi

[root@roottest]#crontab-l

查看当前用户的定时任务 也可以sudo crontab -u root -l 查看指定用户的定时任务。

千万不要忘了中间的 sh 表示用户拿什么来执行命令

00 02 * * * sh /home/admin/optbash/dailyBackup.sh

00 02 * * * sh /home/admin/optbash/deleteDebugSql.sh

建立一个用定时任务跑的bash脚本:

[root@roottest]#touchtest.sh[root@roottest]#vimtest.sh

编辑如下内容,将系统当前时间输出到 console.txt 文件然后保存,增加可执行权限

/bin/echo`date`>/home/admin/test/console.txt

[root@roottest]#chmod+xtest.sh

追加 crontab 定时任务,每分钟触发:

[root@roottest]#crontab-e

* * * * * sh /home/admin/test/test.sh

前面是已经存在的定时任务,后面执行test.sh脚本的是追加的

保存后提示已经装载了新的定时任务

再次查看定时任务列表,可以看到定时任务已经添加

[root@roottest]#crontab-l

* * * * * sh /home/admin/test/test.sh

[root@root test]#

查看console.txt有没有每分钟写入console.txt文件

[root@roottest]#vimconsole.txt

Fri Mar 27 21:40:01 EDT

可以看到最近一次的写入时间。

2、看看crontab 的时间表达式

基本格式 :*****command 分时日月周命令

然后来几个实际的例子:

1、每分钟执行一次

*****

2、每隔一小时执行一次

00****

**/1***(/表示频率)

3、每小时的15和30分各执行一次

15,45****(,表示并列)

4、在每天上午8-11时中间每小时15,45分各执行一次

15,458-11***command(-表示范围)

5、每个星期一的上午8点到11点的第3和第15分钟执行

3,158-11**1command

6、每隔两天的上午8点到11点的第3和第15分钟执行

3,158-11*/2**command

3、其他命令介绍

名称 : crontab

使用权限 : 所有使用者

使用方式 : (有些系统可能需要sudo权限)

crontabfile[-uuser]-用指定的文件替代目前的crontab。

crontab-u[user]-用标准输入替代目前的crontab.

crontab-1[user]-列出用户目前的crontab.

crontab-e[user]-编辑用户目前的crontab.

crontab-d[user]-删除用户目前的crontab.

crontab-cdir-指定crontab的目录。

4、crond 安装与配置服务

安装crontab:

[root@roottest]#yuminstallcrontabs

服务操作说明:

/sbin/servicecrondstart//启动服务

/sbin/servicecrondstop//关闭服务

/sbin/servicecrondrestart//重启服务

/sbin/servicecrondreload//重新载入配置

查看crontab服务状态:

[root@roottest]#servicecrondstatus

手动启动crontab服务

[root@roottest]#servicecrondstart

查看crontab服务是否已设置为开机启动,执行命令:

[root@roottest]ntsysv

加入开机自动启动:

[root@roottest]chkconfig–level35crondon

如果觉得《linux定时任务每两天 linux定时任务》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。