0

I have a bash script that I use to run a script inside a tmux session. It looks like this:

#!/bin/bash
session="myjob"
tmux new-session -d -s $session
tmux send-keys 'myscript' 'C-m'

The problem is that after myscript is done, the session myjob remains. I want to automatically kill it after myscript is finished. I tried:

#!/bin/bash
session="myjob"
tmux new-session -d -s $session
tmux send-keys 'myscript' 'C-m'
tmux kill-session -t $session

but this skips myscript entirely and goes straight to killing the session.

2
  • Out of curiosity, why start a tmux session to run an automated script? Why not just execute the script in the normal shell? Thanks for helping me understand the context. – iangetz Feb 22 at 21:47
  • 1
    This is running in a computational cluster and I want to be able to log out, that's why I use tmux – Gabriel Feb 22 at 21:57
2

You should be able to do:

tmux send-keys 'exec myscript' 'C-m'

With that, the shell will exec the script (i.e, myscript will replace the shell process instead of getting started as a child). Then, when that process terminates, it'll be the process that tmux ran, so tmux will clean up after it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.