4

I'm trying to automate the creation of a tmux session and windows within it running commands and have a script to do so and although it works fine when I run it from command line, it fails if I run it from cron or from a background/nohup process.

So is there any restriction that tmux needs an interactive TTY session in order to create a tmux session/window?

More detail on what I'm trying to do in this question, although I'm really asking about the more general question of whether tmux can run 'headless' at all: tmux from cron fails "lost server"

The 'background process' I refer to is that I tried having a tmux session one window of which was running a script looping around looking for a condition and if found it ran another script to kill the tmux session then recreate it. That did the kill but not the create, possibly because the script was running inside the session that it had killed, but when I experimented with using & for background or nohup on the process that killed/recreated the tmux session, it succeeded the kill but the create did nothing (no error I think).

I'm not very familiar with screen, is it likely to succeed where this fails?

Any suggestions?

1 Answer 1

2

You can tell tmux to start a new session and immediately detatch.

tmux new-session -d

If you give it a name, you can check to see if the session is already running.

tmux has-session -t session_name || tmux new-session -d -s session_name
2
  • Thanks, that does work if started with 'nohup, but my desire is to create a tmux session with split windows with clients running in them, and when I try to use tmux attach-session on that session in a 'nohup' script it fails with open terminal failed: not a terminal which leads me to the original question, that it feels like you need a console to do anything useful. I actually want to start the tmux session with commands from in cron but nohup is an easier proxy for running 'headless'
    – Mortice
    Commented Dec 6, 2020 at 16:12
  • The explicit purpose of nohup is to run a command without a terminal, so there's no way for terminal hangup to reach the command. When you want to attach a tmux session to a terminal, you need a terminal. Leave off nohup, and it should attach just fine.
    – wyrm
    Commented Dec 6, 2020 at 23:03

You must log in to answer this question.

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