linux定时任务

This commit is contained in:
结发受长生 2018-05-14 17:01:23 +08:00
parent a29cff3aed
commit ea8e8d6bb0
3 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,70 @@
---
title: crontab-定时任务
date: 2018-5-14 15:16:46
tags:
- linux
categories:
- linux
---
linux下创建定时任务通常使用`crontab`命令
使用`cron表达式`来指定执行的定时规则
使用shell脚本编写需要执行的内容
> 脚本中涉及的文件路径是要写 **绝对路径**
<!-- more -->
执行`crontab -e`编辑当前用户crontab服务文件
#### cron语法
crontab的定时任务只能精确到分钟 , 而无法精确到秒
所以cron表达式只有5个值
```
分 小时 日 月 星期 命令
0-59 0-23 1-31 1-12 0-6 command
```
0表示周日(也可以用英文来表示sun表示星期天mon表示星期一)
一般一行对应一个任务
+ `*`代表任意值
+ `/`代表 **每**
+ `-`代表连续的时间段(从xx到xx)
+ `,`代表不连续的时间点(xx和xx)
常见的用法举例
```
5 * * * * command 指定每小时的第5分钟执行一次
30 5 * * * command 指定每天的 5:30 执行
30 7 8 * * command 指定每月8号的730分执行
30 5 8 6 * command 指定每年的6月8日530执行
30 6 * * 0 command 指定每星期日的6:30执行
30 3 10,20 * * command 每月10号及20号的330执行
25 8-11 * * * command 每天8-11点的第25分钟执行
*/15 * * * * command 每15分钟执行一次 [即每个小时的第0 15 30 45 60分钟执行]
30 6 */10 * * command 每个月中每隔10天6:30执行一次 [即每月的1、11、21、31日的630执行]
```
#### 其他用法
+ `crontab -l` - 列出某个用户cron服务的详细内容
+ `crontab -u <username>` - 设定某个用户的cron服务一般root用户在执行这个命令的时候需要此参数
+ `crontab -r` - 删除某个用户的cron服务
#### 全局配置
cron的主配置文件是`/etc/crontab`
![crontab config](/images/linux/crontab_config.jpg)
在这个文件里面也可以配置定时任务的执行规则
这里这个PATH并不会在实际的脚本运行当中生效
如果脚本单独运行没问题 , 但是在定时任务当中出现命令找不到的情况
比如是在`/etc/profile`当中配置的环境变量
可以用下面的方式指定定时任务规则
```bash
* * * * * . /etc/profile; command
```
#### 服务相关的操作
+ `serivce crond start` - 启动
+ `serivce crond stop` - 停止
+ `service crond restart` - 重启
+ `service crond status` - 查看运行情况

View File

@ -0,0 +1,104 @@
---
title: expect-交互式命令行自动化执行
date: 2018-5-14 10:45:12
tags:
- linux
categories:
- linux
---
linux当中存在很多交互式的命令行
简单来说就是执行过程中需要等待用户的输入 , 获得输入内容后继续执行
这种情况下我们可以借助`expect`来实现自动化执行这些命令
> 直接重定向输入流多数情况下是不行的
因为它会将需要输入的内容一次性全部输入 , 而无法做到多次的交互
<!-- more -->
expect本身是一个shell脚本的解释器 , 与bash属于同一类东西
所以我们可以在脚本开头添加
```bash
#!/usr/bin/expect
```
来指定使用该解释器来执行这个脚本 , 运行脚本直接`./test.sh`即可
当然也可以执行`expect test.sh`
#### 安装
```bash
yum install expect.x86_64
```
#### 编写脚本
使用`npm init`初始化项目的时候
需要多次输入项目的相关基本信息 , 这里就用它来做示例
```bash
#!/usr/bin/expect
set timeout -1
cd /root/pro_test
spawn /usr/local/nodejs/bin/npm init
expect "package name:*"
send "\n"
expect "version:*"
send "\n"
expect "description:*"
send "这里是项目的描述信息\n"
expect "entry point:*"
send "main.js\n"
expect "test command:*"
send "\n"
expect "git repository:*"
send "\n"
expect "keywords:*"
send "key1 key2\n"
expect "author:*"
send "Sookie\n"
expect "license:*"
send "MIT\n"
expect "Is this ok?*"
send "yes\n"
expect eof
```
说明:
+ `spawn``send`命令是只有在expect解释器当中才有的 , 所以该脚本必须用expect解释执行
+ `spawn`表示启动一个新的进程
+ `expect "xxx"`表示识别的进程输出( 可以使用通配符 )
+ 注意在输入内容的末尾加`\n`
#### interact
如果我们需要让用户在适当的时候手动干预这个过程
就需要执行`interact`
例如
```bash
spawn ftp ftp.test.com
expect "Name"
send "user\n"
expect "Password:"
send "123456\n"
interact
```
下载完ftp文件时仍然可以停留在ftp命令行状态以便手动的执行后续命令
#### timeout
执行`set timeout`可以设置等待进行输出信息的超时时间
单位是秒 , 默认是10秒
如果超过了这个时间还未能匹配到输出内容 , 则会跳过这个expect语句继续向下执行
-1则表示没有超时时间 , 也就是未匹配到输出内容会一直等待
#### 多分支模式
有时候程序的输出可能有多种分支路线
所以我们的自动化脚本也不能简单地进行单线匹配
expect还支持多分支模式语法
```bash
expect {
"hi" { send "You said hi\n" }
"hello" {
send "Hello yourself\n"
expect {
"Nice to meet you" { send "Nice to meet you too!" }
"How is it going" { send "It's good, thank you." }
}
}
"bye" { send "See you\n" }
}
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB