首页 > 解决方案 > 在 Expect 中产生的 rsync 在 10-15 秒后停止

问题描述

我正在尝试通过编写脚本来自动化一项非常繁琐的任务。理想情况下,脚本应该:

  1. 为列表中的每个目录创建目录。
  2. rsync从服务器到 working_dir 的一组文件。
  3. 在一组文件上运行 FastQCworking_dir/$FOLDERNAME
  4. 删除文件和目录。

rsync提示我输入密码,因此我将其集成到期望函数中。密码输入似乎工作正常,因为 rsync 开始同步。但是,在 10-15 秒后,同步会取消 rsync,而是直接跳转到下一步。有谁知道问题可能是什么?

有问题的脚本如下:

#!/bin/bash

# Change directory to working_dir.
cd /tmp/working_dir

# Load fastqc
module load fastqc/0.11.9

# Function for inputting password prompt rsync
function f_rsync
{
/usr/bin/expect <<EOF
spawn rsync -P username@host.something.com:/something/$FOLDERNAME/*.fastq.gz /tmp/working_dir/$FOLDERNAME
expect "Enter passphrase for key '/home/username/.ssh/id_rsa':"
send "PASSWORD\n"
expect eof
EOF
}

for FOLDERNAME in 2020-02-09 2020-02-10 2020-02-12 2020-04-06 2020-04-07 2020-04-17 2020-04-20 2020-04-22 2020-05-04 2020-08-18 2020-08-19 2020-08-21 2020-08-28 2020-11-02 2020-11-20 2020-12-17 2021-01-26 2021-02-24 2021-02-27 2021-03-01 2021-03-02 2021-03-03 2021-03-15 2021-03-16 2021-04-23 2021-04-26 2021-05-29 2021-05-31 2021-06-02 2021-07-19 2021-07-28 2021-08-08 2021-08-09 2021-08-12 2021-08-13 2021-08-25 2021-08-27;
do
  mkdir $FOLDERNAME
  f_rsync
  fastqc -t 40 -o /home/username/ /tmp/working_dir/${FOLDERNAME}/*.fastq.gz
  rm ${FOLDERNAME}/*
  rmdir ${FOLDERNAME}

done

标签: linuxbashbioinformaticsexpectrsync

解决方案


Expect 的默认超时时间为 10 秒,因此expect eof会在 10 秒后返回。您可以使用更改默认超时

set timeout 300 ;# use -1 for no timeout

或为每个expect命令使用显式超时:

expect -timeout 300 eof

推荐阅读