一、定时任务

一、写一个简单的shell命令:

1、先进入根目录

cd/root

2、使用vi编辑器 可以直接 # vim hello.sh编写第一个shell文件 hello.sh, 注意一定要以.sh结尾

vim hello.sh

3、编写第一个shell文件,#!/bin/bash 是必须要写的,表示要是/bin/bash这个执行脚本的命令执行接下来写的脚本, echo "hello world !!"表示想前端打印一句话,具体看各自需求。

4、通过chmod命令赋予该脚本的执行权限chmod 755 hello.sh,否则没有执行权限,/root/hello.sh表示在全路径下执行该shell脚本

./hello.sh

 就此一个简单的shell脚本就这样写完了 具体的这里就不讲了。(说白了,shell脚本就相当于一个文件,它专门来干自己独立的任务)

二、定时任务

虽然一个简单的shell脚本写完了 但是运行后只能执行一次 对很多应用场景来说还是不够,接下来说一下定时任务

linux应该都有crontab,没有的话可以安装一下:

yum install  vixie-cron
yum install  crontabs

vixie-cron软件包是cron的主程序; 
crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。

安装完以后开启crontab服务

service crond start

用以下的方法启动、关闭这个cron服务: 
service crond start //启动服务 
service crond stop //关闭服务 
service crond restart //重启服务 
service crond reload //重新载入配置

查看crontab服务状态:service crond status 
手动启动crontab服务:service crond start

三、设置需要执行的脚本

新增调度任务可用两种方法: 
1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。 
2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务。 
crontab -e配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务 
查看调度任务 
crontab -l //列出当前的所有调度任务 
crontab -l -u jp //列出用户jp的所有调度任务 
删除任务调度工作 
crontab -r //删除所有任务调度工作 
直接编辑 vim /etc/crontab ,默认的文件形式如下:

猜你喜欢

转载自www.cnblogs.com/fger/p/10428878.html