I am trying to "re-implement" nohup inside of tmux (so dispatching a job without blocking me and then killing the tmux command acting as nohup). My attempt is here (and it has several issues as outlined the beginning of it won't work but it's a seperate question, this question is about the last part of the script). My main issue right now is that I want to kill the tmux session once my real python script running in the background is done. This is my full attempt:
# - get a job id for this tmux session
export SLURM_JOBID=$(python -c "import random;print(random.randint(0, 1_000_000))")
echo SLURM_JOBID = $SLURM_JOBID
export OUT_FILE=$PWD/main.sh.o$SLURM_JOBID
export ERR_FILE=$PWD/main.sh.e$SLURM_JOBID
# - CAREFUL, if a job is already running it could do damage to it, rm reauth process, qian doesn't do it so skip it
# top -u brando9
# pkill -9 reauth -u brando9
# - start tmux,
https://unix.stackexchange.com/questions/724877/custom-kerberos-tmux-doesnt-let-me-name-my-sessions-help-forcing-it
tmux new -s $SLURM_JOBID
# /afs/cs/software/bin/krbtmux new -s $SLURM_JOBID
# cat /afs/cs/software/bin/krbtmux
# - reauth
# /afs/cs/software/bin/reauth
echo $SU_PASSWORD | /afs/cs/software/bin/reauth
# echo 'Secret' | /afs/cs/software/bin/reauth
# echo 'totally secret password' | kinit [email protected]
# to see reauth running
# top -u brando9
# - expt python script
python expts.py &
python -u ~/diversity-for-predictive-success-of-meta-learning/div_src/diversity_src/experiment_mains/main_sl_with_ddp.py --manual_loads_name sl_hdb1_5cnn_adam_cl_filter_size --filter_size 4 > $OUT_FILE 2> $ERR_FILE &
export JOB_PID=$!
echo JOB_PID = $JOB_PID
# - Echo tmux id, should be jobid
tmux display-message -p '#S'
echo SLURM_JOBID = $SLURM_JOBID
# - detach current tmux session
tmux detach &
# - wait for pid from python to be done, if done kill this tmux sess
wait $JOB_PID
tmux ls
tmux kill-session -t $SLURM_JOBID
# check it was killed
tmux ls
# - Done
echo "Done with bash script (experiment or dispatched daemon experiments). "
But the above won't work because: once the python script is running and been dispatched with &
, what I want is to kill the tmux session when that job is done. I tried that at the end but I suspect it won't work. I think it won't work because once it runs tmux detach &
it will go out of the tmux session and run the wait ...
command outside of tmux and block my main terminal. Instead what I want is to run that wait command inside of tmux (to not block me) and once the python script is done to kill the tmux session entirely. But that is just to add context to the final part of my script.
How do I make this work properly?
related: How does one authenticate with a command that requires your password in linux?
tmux new
looks suspicious. See this answer where it says that "tmux does not magically alter the flow of your script". Aftertmux new
you are in an interactive shell inside tmux and your script won't continue until you exit the tmux client. Is this what you want? Dispatching a job is as easy astmux new-session -d 'shell code here'
and the session will exit automatically when the job exits (compare this answer).