Contents

tool:ubuntu配置定时任务

crontab

cron是一个Linux定时执行工具,可以在无需人工干预的情况下运行作业。在Ubuntu中,cron是被默认安装并启动的。

crontab定时任务语法

1
minute   hour   day   month   week  user  command     #顺序:分 时 日 月 周 用户 命令
  • minute: 表示分钟,可以是从 0 到 59 之间的任何整数。
  • hour:表示小时,可以是从 0 到 23 之间的任何整数。
  • day:表示日期,可以是从 1 到 31 之间的任何整数。
  • month:表示月份,可以是从 1 到 12 之间的任何整数。
  • week:表示星期几,可以是从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日。
  • user:linux的用户身份,例如root,或者其他用户
  • command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。

特殊字符

  • 星号(*):代表所有可能的值,例如 month 字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
  • 逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
  • 中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
  • 正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,如*/10,如果用在 minute 字段,表示每十分钟执行一次。

设置定时任务

  1. 编辑ect下crontab文件就行了,这个文件里存放的就是cron要执行的命令,以及定时执行的时间
1
sudo vim /etc/crontab
  1. 修改文件: 定时每凌晨,自动执行保存在/root目录下hello.sh脚本
1
00 00   * * *   root    /root/hello.sh
  1. 使用
1
2
3
00 01   * * *   root    /home/zyh/data/blogs/deploy.sh
00 03   * * *   root    /home/zyh/data/blogs/deploy.sh
00 05   * * *   root    /home/zyh/data/blogs/deploy.sh

cron 服务查看

1
2
3
4
5
service cron start  # 启动服务
service cron stop # 关闭服务
service cron restart # 重启服务
service cron reload # 重新载入配置
service cron status # 查看crond状态 

expect

expect 就是完成一些需要与用户交互的任务,例如 telnet、ftp、ssh 远程登录机器的时候,这些命令会要求用户输入用户名、密码等相关信息,而这些,是无法通过 shell 脚本来完成的。这是因为这些命令是从控制终端而不是标准输入上读取的,所以无法事先将信息重定向到标准输入从而实现自动化运行。而 expect 就是用来解决这类问题的,举个expect 进行 ssh 登录的例子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/usr/bin/expect -f
set ipaddr "localhost"
set passwd "iforgot"

spawn ssh root@$ipaddr
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}

expect "]# "
send "touch a.txt\r"
send "exit\r"
expect eof
exit

expect 脚本里有这么几个关键动作:

  • spawn :启动需要执行的命令;
  • expect :解析命令输出,并根据下面的匹配语句进入子控制块;
  • send :向命令发送信息,这些信息相当于是命令从控制终端读取的;
  • interact :继续命令与控制终端的交互,此时用户可以正常向命令输入信息(本例未展示)。
  • ……

安装 expect

1
2
3
4
# 安装
sudo apt-get install tcl tk expect
# 查看版本
expect -v

自动代码拉提交脚本

  • timeout

    1
    2
    
    set timeout -1 # 没有timeout 
    set timeout XX # 设定具体的timeout时间(秒)
    
  • 自动拉取重试

    1
    2
    3
    4
    5
    6
    7
    8
    
    #! /usr/bin/expect -f
    set timeout 30
    for {set i 0} {$i<=10} {incr i} {
        puts "start pulling git $i"
        spawn git pull
        #expect "Already up-to-date." { puts "pulling ok"; exit }
        expect "已经是最新的。" { puts "pulling ok"; exit }
    }
    
  • 自动提交重试

    1
    2
    3
    4
    5
    6
    7
    
    #! /usr/bin/expect -f
    set timeout 30
    for {set i 0} {$i<=10} {incr i} {
        puts "start pushing git $i"
        spawn git push
        expect "Everything up-to-date" { puts "pushing ok"; exit }
    }
    
  • 完整脚本

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    
    
    #!/bin/sh
    PATH=/bin:/usr/bin:/snap/bin
    export PATH
    
    # If a command fails then the deploy stops
    set -e
    cd /home/zyh/data/blogs
    
    echo "LOG DATE:" $(date +"%Y-%m-%d %H:%M:%S")
    DATE=`date '+%Y%m%d-%H%M%S'`
    echo $DATE
    LogNameDATE=`date '+%Y'`
    LogFile='./log/log'$LogNameDATE'.log'
    
    echo " " >> $LogFile
    echo "———————————————–" >> $LogFile
    echo "BACKUP DATE:" $(date +"%Y-%m-%d %H:%M:%S") >> $LogFile
    
    
    printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"
    
    printf "\n\n"
    printf "\033[0;32mPull note files ...\033[0m\n"
    
    expect pull.exp
    
    printf "\n\n"
    printf "\033[0;32mHugo build ...\033[0m\n"
    
    # clean Public folder
    # cd public
    # rm -r *
    # cd ../
    
    # Build the project.
    echo $(hugo -t LoveIt)  >> $LogFile # if using a theme, replace with `hugo -t <YOURTHEME>`
    
    printf "\n\n"
    printf "\033[0;32mpush public ...\033[0m\n"
    
    # Go To Public folder
    cd public
    
    # Add changes to git.
    if [ -n "$(git status -s)" ];then
      git add ./
      # Commit changes.
      msg="update $(date)"
      if [ -n "$*" ]; then
        msg="$*"
      fi
      git commit -m "$msg"
    
      cd ../
    
      expect push_main.exp
    else
      cd ../
    fi
    
    echo "———————————————–" >> $LogFile
    
    printf "\n\n"
    printf "\033[0;32mPush ...\033[0m\n"
    
    # Add changes to git.
    if [ -n "$(git status -s)" ];then
      git add ./
      # Commit changes.
      msg="update $(date)"
      if [ -n "$*" ]; then
        msg="$*"
      fi
      git commit -m "$msg"
    
      # Push source and build repos.
      expect push_master.exp
    fi
    
    printf "\n\n"
    printf "\033[0;32mDone\033[0m\n"