0

I'm trying to make a script that launches some time-consuming processes detached (using tmux) and also launches a process that works like a sentinel: it looks for the tmux process (in /proc/$(pgrep tmux)) and, if it is not there, I assume everything was finished and the machine can be shut down.

This is what I have so far:

#!/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;" &

The result:

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


The problem is: none of the op from 1 to 6 do what I want: to log at which time the tmux is still alive.

op 1, 5, and 6 show the time from where I am, and always the same hour/minute/second - time launched.

op 2, 3, and 4 show the time from where the machine is, and always the same hour/minute/second - time launched.

I feel like I'm not fully aware of how/when the command substitution happens, but I could not find an example of how this can be done.

All advice/enhancements about what I have is welcome :)

griloHBG
  • 101
  • 1

1 Answers1

0

Well, I just found out here that, for a HERE-document, a <<'MYENDSTRING' makes expansion/evaluation of its lines only at the remote side. This makes EVERYTHING easier.

#!/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
griloHBG
  • 101
  • 1