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.
I kind of found out it by myself.
To kill process you can use
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>
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
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; 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
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.