1

I need to run a process and can't use &... But can run by tmux. Steps:

  1. terminal SSH to the server machine.
  2. tmux
  3. run application ... and wait... Oops, timeout...

How to (simplest way) back to the application, to see rest of the logs?

4
  • Please provide more details on what happens during step 3 -- specifically, what you mean by "timeout": Have you been disconnected from the server (in which case, ssh again then tmux at should retrieve your session)? Or has the application itself stopped for some reason? – JigglyNaga Oct 17 '19 at 12:57
  • Are you just interested in running it in the background? Is nohup an option here? What do you mean by "cannot use &"? – FelixJN Oct 17 '19 at 13:16
  • @Fiximan, I never used it, but also make sense, something as nohup COMMAND > log.txt... How to check the nohup process?... And, when it fineshed, the log.txt is there (folder) where I call it? – Peter Krauss Oct 17 '19 at 13:21
  • log.txt will be immediately created and all outputs from COMMAND redirected there. Check for the status using the PID reported by nohup upon start. See my answer. – FelixJN Oct 17 '19 at 13:34
2

You can list tmux sessions when reconnecting to the remote server:

tmux ls
0: 4 windows (created Tue Oct 15 07:55:07 2019) [255x62]
1: 2 windows (created Tue Oct 15 07:55:07 2019) [255x62]

You can then reattach to a target session:

tmux attach-session -t 1

This would attach to the second tmux instance listed above.

If you just have one tmux session running, then you can use the shorthand:

tmux a

to reattach the first session.

1

If you are prone to hangups or want to leave the ssh-session, you can use nohup. This allows you to detach the process from the ssh-session, runs it in the background and saves the output into a file.

nohup COMMAND &

Defaults outputs to $HOME/nohup.out , alternatively a redirect is possible

nohup COMMAND > mylogs.log &

When you run nohup, you will be shown the PID of the process, e.g.:

$ nohup bash -c "while ((1)) ; do echo 1 ; sleep 1 ; done" > echo.log &
[1] 21816

Note that PID and check the status of your process via e.g. ps <PID>

$ps 21816
  PID TTY      STAT   TIME COMMAND
21846 pts/6    S      0:00 bash -c while ((1)) ; do echo 1 ; sleep 1 ; done

No output with the given PID means the process is no more active (canceled, aborted, stopped, finished ... whatever).

0

Let say, for purpose of demonstration, you want to run this on a remote host:

for i in $(seq 60) ; do
    echo "$i seconds of 60"
    sleep 1
done

.... and want it to keep running when you ssh connection dies.

The simplest way I can think of to do this with tmux is:

tmux new-session -d
tmux new-window bash -c 'for i in $(seq 60) ; do echo "$i seconds of 60" ; sleep 1 ;done'

After this you will be back at the shell prompt as if nothing happened. But the script will be running in tmux in memory. If your exit the shell (or your ssh connection dies) it will remain running.

To connect to the tmux session in order to see the program running in your terminal, run this command:

tmux attach-session

To leave the tmux session while leaving it runnning in the background, hit ctrl-b followed by d.

To terminate the tmux session, just connect to it, terminate your program if is still running en exit the shell the normal way. Or from outside the tmux session:

tmux kill-session

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.