无需挂断、耗时的过程的脚本,完成后关闭机器

无需挂断、耗时的过程的脚本,完成后关闭机器

我正在尝试编写一个脚本,启动一些分离的耗时进程(使用 tmux),同时启动一个像哨兵一样工作的进程:它会查找 tmux 进程(在 中/proc/$(pgrep tmux)),如果不存在,则我认为一切都已完成,机器可以关闭。

这是我目前所拥有的:

#!/bin/bash

# my log file on the machine running the process
LOG_FILE="/home/ubuntu/grilo_tmux.log"

# ssh to the machine that will run the processes, using HERE-document
ssh $1 /bin/bash -s << EOF

# begin of the log file
echo "\$(date +%H:%M:%S) processos comecando" > $LOG_FILE

# launching time-consuming processes
tmux new -s p0 -d 'echo PROCESS 0; sleep 1; exit'
tmux new -s p1 -d 'echo PROCESS 1; sleep 1; exit'
tmux new -s p2 -d 'echo PROCESS 2; sleep 2; exit'

# everything is running
echo "\$(date +%H:%M:%S) todos os processos foram lancados" >> $LOG_FILE

# sentinel: will shutdown the machine if there is no tmux alive
nohup bash -c " \
while [[ -d /proc/\$(pgrep tmux) ]]; \ # while tmux is still alive
  do sleep 1; \ # take some rest
  echo "" >> $LOG_FILE \ # a little separation
  echo    $(date +%H:%M:%S)   esperando os tmux acabarem >> $LOG_FILE; \ #op 1
  echo   \$(date +%H:%M:%S)   esperando os tmux acabarem >> $LOG_FILE; \ #op 2
  echo \"\$(date +%H:%M:%S)\" esperando os tmux acabarem >> $LOG_FILE; \ #op 3
  echo \'\$(date +%H:%M:%S)\' esperando os tmux acabarem >> $LOG_FILE; \ #op 4
  echo  \"$(date +%H:%M:%S)\" esperando os tmux acabarem >> $LOG_FILE; \ #op 5
  echo  \'$(date +%H:%M:%S)\' esperando os tmux acabarem >> $LOG_FILE; \ #op 6
  echo "" >> $LOG_FILE \ # a little separation
  done; \
 \ # replace a shutdown here
echo $(date +%H:%M:%S) TMUX MORREU >> $LOG_FILE;" &

结果:

04:31:37 processos comecando
04:31:37 todos processos foram lancados

01:31:33 esperando os tmux acabarem
04:31:37 esperando os tmux acabarem
04:31:37 esperando os tmux acabarem
'04:31:37' esperando os tmux acabarem
01:31:33 esperando os tmux acabarem
'01:31:33' esperando os tmux acabarem


01:31:33 esperando os tmux acabarem
04:31:37 esperando os tmux acabarem
04:31:37 esperando os tmux acabarem
'04:31:37' esperando os tmux acabarem
01:31:33 esperando os tmux acabarem
'01:31:33' esperando os tmux acabarem

01:31:33 TMUX MORREU


问题是:op从 1 到 6 都没有实现我想要的功能:记录 tmux 仍然处于活动状态的时间。

op1、5、6 表示从我在哪里, 和总是相同的小时/分钟/秒 - 时间启动

op2、3 和 4 表示从机器在哪里, 和总是相同的小时/分钟/秒 - 时间启动

我觉得我不完全了解命令替换是如何/何时发生的,但我找不到如何做到这一点的例子。

欢迎对我所拥有的内容提出所有建议/改进:)

答案1

嗯,我刚刚发现这里对于 HERE 文档来说,<<'MYENDSTRING'仅在远端对其行进行扩展/评估。这让一切都变得更容易

#!/bin/bash

#this should be launched like this:
# nohup ec2-experiment-launcher-scratch.sh remote_host_name &
#then you can close your local terminal.

echo lancando processos

#<<'string' allows substitution only to occur at remote side, which is nice!
ssh $1 /bin/bash -s <<'EOF'

export LOG_XABLAU="/home/ubuntu/grilo_tmux.log"

echo "\$(date +%H:%M:%S) processos comecando" > $LOG_XABLAU

tmux new -s p0 -d 'echo PROCESS 0; sleep 1; exit'
tmux new -s p1 -d 'echo PROCESS 1; sleep 1; exit'
tmux new -s p2 -d 'echo PROCESS 2; sleep 10; exit'

echo "\$(date +%H:%M:%S) todos processos foram lancados" >> $LOG_XABLAU

echo processos lancados

echo lancando sentinela

nohup bash -c ' \
while ps -p $(pgrep tmux) &>/dev/null; \
do \
  echo $(date) >> $LOG_XABLAU; \
  sleep 1; \
done; \
echo $(date) TMUX MORREU >> $LOG_XABLAU; '&

EOF

echo sentinela lancada

echo pode sair

相关内容