5

So, I want to launch a shell command through tmux, but in the background and detaching from said tmux session...so I do it like so:

tmux new-session -A -s myprogramsession \; send -t myprogramsession "nohup /usr/bin/myscript.sh &>/dev/null &" ENTER \; detach -s myprogramsession

This works. But now, if I want to make tmux stop once it detached from the aforementioned session, I would need to either do:

this:

tmux new-session -A -s myprogramsession \; send -t myprogramsession "nohup /usr/bin/myscript.sh &>/dev/null &" ENTER \; detach -s myprogramsession \; kill-session -t myprogramsession

or this:

tmux new-session -A -s myprogramsession \; send -t myprogramsession "nohup /usr/bin/myscript.sh &>/dev/null &" ENTER \; detach -s myprogramsession && pkill tmux

While both methods (one using pkill and kill-session from tmux) seems to work on first glance, I noticed both prevent the process/script launched through nohup from staying in the background, compared to the first attempt which manage to do so (minus for the part about stopping tmux).

How could I make tmux (based on the above examples) launch a shell command on a session (in the background), detach it, then stop tmux completely while the shell command is still running in the background?

P.S.: I did check through this and this post, but failed to find a solution there.

3
  • What do you need tmux for? To kill it right away? What's the point? Jun 17, 2021 at 20:36
  • I need it because some programs sometimes (here I'm running an external program in the script.sh following the example) do not work even with nohup, depending on whether it's run in the terminal or in the background/through a few layer of scripts...I could have posted a question about this behavior to make sure why it happened, but by the time I thought of doing so, I found a method that worked (using tmux + nohup) so I ended up not doing that :) @KamilMaciorowski Jun 17, 2021 at 20:39
  • 1
    You may consider run-shell as a replacement of send-keys.
    – fra-san
    Jun 17, 2021 at 22:51

1 Answer 1

0

This looked easier than I thought:

tmux new-session -A -s myprogramsession \; send -t myprogramsession "nohup /usr/bin/myscript.sh &>/dev/null &" ENTER \; detach -s myprogramsession && sleep 1 && pkill tmux

This work if a sleep is added before pkill comes in. Probably not the best solution however.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .