4

I have tmux installed on a server for running jupyter. Fitting neural nets takes a lot of time, but server is public, so there is also a lack of resources. I wonder if I could run my programs, estimate its running time roughly, then write a command like tmux kill --time 5000, where 5000 is the estimated runtime in seconds. That way, I do not borrow unused resources.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
taciturno
  • 151
  • perhaps you want to use the timeout command. – toppk Oct 06 '22 at 04:46
  • @toppk you mean timeout for commands? – taciturno Oct 06 '22 at 06:44
  • How do you run programs in tmux? The tmux server will exit when all commands started by new-session, new-window, split-window and such exit, unless some pane uses remain-on-exit or exit-empty is off. It's easy to run commands in tmux in a way so the tmux server exits automatically when it's no longer needed, unless some command doesn't exit by itself and has to be killed (killing manually may be simpler than building logic). – Kamil Maciorowski Oct 06 '22 at 17:36

2 Answers2

1

I kind of found out it by myself.

To kill process you can use sleep command:

sleep <time in seconds> && tmux kill-session -t <session name>

or, if you need to kill all the tmux session, you simply can type:

sleep <time in seconds> && pkill tmux

UPD: Also, like was recommended from comments, one can use timeout <time> tmux <command>

taciturno
  • 151
  • As mentioned in comments, also try timeout 5000 tmux some-command. See man timeout on your system. – Kusalananda Oct 06 '22 at 14:25
  • @Kusalananda, thanks, I will definitely try! – taciturno Oct 06 '22 at 16:14
  • @Kusalananda In timeout 5000 tmux some-command the tmux being a child of timeout is always a tmux client. In some circumstances a forked descendant becomes a tmux server. Any tmux server survives disconnection of a client (after all this is one of the main features of tmux). IMO this means timeout cannot affect tmux server. My tests seem to support this. – Kamil Maciorowski Oct 06 '22 at 17:07
  • @KamilMaciorowski Well, tmux timeout 5000 some-command then. – Kusalananda Oct 06 '22 at 17:16
  • @Kusalananda timeout is not a valid command in tmux. My impression is you think tmux something is like nohup something or sudo something. Well, it's not. – Kamil Maciorowski Oct 06 '22 at 19:07
  • @KamilMaciorowski I seem to have forgotten to include the -c option, which you could have pointed out yourself. tmux -c 'timeout 5000 some-command'. You may have to tweak the other options to timeout, obviously, depending on what the command you're timing out is, and sort out quoting if needed, but that's the basics. – Kusalananda Oct 06 '22 at 20:01
1

killing the tmux server won't necessarily stop processes you run inside tmux. It seems, however, you want to kill tmux when presumably all the programs you have started in it are no more.

By default tmux server exits when all processes assigned to its panes exit. There is remain-on-exit you can use to keep a pane open and examine the results of its process that exited; but it seems you want to kill the server anyway, without reattaching and reviewing, so I don't think you use remain-on-exit. There is exit-empty that can be set to off to keep the server running without sessions, but you don't want this. It seems to me the default behavior is exactly what you want. All you need to do is to make sure the processes assigned to panes exit.

When you do tmux new-session command1, command1 is the process assigned to the single pane in the single window in the newly created session. You can then tmux new-window command2 and tmux split-window command3 and so on. After command1, command2, command3 (and others, if any) exit and there is no other session, the tmux server will exit.

Note if you create a session/window/pane with an interactive shell then the shell is the process that matters. command4 started from the shell may exit, but the shell will remain, so the tmux server will remain. If you are used to running programs from interactive shells in tmux and you want to keep it this way, to make tmux exit as soon as it's not needed you should make sure each shell exits just after the task in it exits. In a shell inside tmux you can do

command4; exit

or even

command4; command5 && command6 | (command7; command8) | command9; exit

This way the shell will eventually exit, if only the commands exit. In case of a sole simple command4 you can invoke

exec command4

to replace the shell with command4, so from now on command4 is what matters to the tmux server.

One way or another make sure all the panes in all the windows in all the sessions will exit eventually. If you do this right then the tmux server will exit automatically after they all exit.

With this approach you don't need to estimate anything. The tmux server will last exactly for as long as it's needed and you don't risk killing it prematurely.

Again, all this is under assumption that any output to a terminal (pane) in this tmux is expendable. In your own answer I see you want to kill the session or the tmux server without checking anything.

Note my solution allows you to spawn a new session or a window or a pane if needed, anytime, without worrying about some old timer that no longer fits the new situation. Each pane will disappear exactly when it's not needed; the same for each window, each session and the entire tmux server.

The solution will not really work if some command assigned to a pane is meant to run indefinitely (i.e. until killed on demand). I think the question is not about such command though. But in case it is, remember that command4 may be like timeout 5000 actual_command4.