Linux

無掛斷腳本,完成後關閉機器的耗時過程

  • February 3, 2021

我正在嘗試製作一個腳本,該腳本啟動一些分離的耗時程序(使用 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 顯示機器所在的時間,並且始終是相同的小時/分鐘/秒啟動時間

我覺得我並不完全了解命令替換是如何/何時發生的,但我找不到如何做到這一點的範例。

歡迎所有關於我所擁有的建議/改進:)

好吧,我剛剛在這裡發現,對於 HERE 文件,a<<'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

引用自:https://serverfault.com/questions/1052047