Overall, tmux is the best among all selections here, but you should know how do others work in case tmux is unavailable. 你还需要理解VNC, Screen, tmux 的原理是什么? 为什么使用它们则SSH 断开之后进程仍可以继续运行呢? 哪里在收集和记录状态。
Run in Background
The most straightforward way is put long running task in background, redirect
the stdout/stderr to files or /dev/null
, for example:
1 | ./task.sh &> log.$(date "+%Y-%m-%d") & |
Note that ctrl+z also works when editting Vim file, it will suspend editing, put it in background and bring you back to terminal.
To detach a job from the current jobs
list, using disown %<job id>
command.
The detched job will still be running but cannot bring it back to jobs
.
Nohup
Why the work gets lost when SSH session drops is because the signal SIGHUP will be sent to the foreground process. If we let the process ignore this signal, then it can keep running when remote SSH connection is gone.
1 | # nohup will redirect the stdin from /dev/null |
You can use jobs
, bg
and fg
command to operate it.
VNC
之前用VNC的目的之一是为了keep SSH long running task on remote machine , prevent lost
of work, the alternative is screen
and tmux
command(best).
Virtual Network Computing (VNC)
is a graphical desktop-sharing system that
uses the Remote Frame Buffer protocol (RFB) to remotely control another computer.
It transmits the keyboard and mouse events from one computer to another, relaying
the graphical-screen updates back in the other direction, over a network.
Popular uses for this technology include remote technical support and accessing files on one’s work computer from one’s home computer, or vice versa.
I usually use it to do remote development on Fyre, I have a Fyre central control machine with VNC installed, after vnc to that machine I use it to SSH to other machines within the same internal network(usually connections will not break), you can also install IDE in VNC to do general programming work rather than developing locally.
Note, there are other remote terminal tools such as
Termius
.
Install VNC Server
The settings are varies on different Linux distros.
1 | yum update -y |
Note you will get new vnc session everytime you run vncserver
, check with
1 | ps aux | grep vnc |
you can kill it by running:
1 | vncserver -kill :2 |
If the VNC is broken due to Fyre maintenance, open a new vnc session again by
vncserver
.
Install VNC viewer
Install VNC viewer on your local laptop, then connect by for example:
1 | centctl1.fyre.ibm.com:1 |
Copy and Paste
If you want to copy from local to viewer, sometimes it’s malfunction, kill the
klipper
process:
1 | ps aux | grep klipper |
You can adjust shortcuts in VNC viewer for copy and paste: Settings -> Configure Shortcuts -> copy / paste
Other Settings
Other settings useful: Settings -> Edit Current Profile -> Mouse -> copy on select / Trim trailing space
Adjust font size the themes: Settings -> Manage Profiles -> Edit Profile -> Appearabce -> Black on Random Light -> check Vary the background color for each tab
Also change the text size under Appearance.
Screen resolution
If the screen open by VNC viewer is small, you can change the resolution, run:
1 | xrandr -s 1920x1080 |
on the terminal in your remote machine.
Screen
Create virtual terminal lives beyond your terminal session.
How To Use Linux Screen
Screen or GNU Screen is a terminal multiplexer. In other words, it means that
you can start a screen session and then open any number of windows (virtual
terminals) inside that session. Processes running in Screen will continue to run
when their window is not visible even if you get disconnected, the work or
progress will not get lost! 所以说可以不用&
后台运行了。
screen command cheat sheet,
or see help in screen: crtl+a ?
, hit enter to quit. you can use either screen
or ctrl+a
(key binding).
Note that install and run screen
on target machine, VNC 应该是同样的.
1 | apt update |
You can create multiple windows in one session
1 | Ctrl+a c Create a new window (with shell) |
1 | # list running sessions |
How do I know I am in screen and which session:
1 | # show session id if you are in screen |
Tmux
Tmux 相比Screen 有terminal底部的提示,不容易和host terminal搞混.
Note that tmux has 3 terms(same as Screen): session -> window -> pane. you can have multiple sessions, each session can have mutliple windows and each window can have several panes. Usually rename the session and window with meanful symbol.
1 | # version |
In the bottom line, *
means current window, you will see the windows list and
name here.
Some key bindings for window:
1 | Ctrl+b ? help, press `q` to quit |
Add panes to windows, active one has the green color.
1 | Ctrl+b " horizontal split |
You can also adjust the pane size or even promote the pane to a its own window.
1 | Ctrl+b [esc + arrow] 连续多次esc + arrow resize the pane |
Tmux also has command mode to accomplish the same task by key bindings, similar
to Vim. 比如说monitor-activity设置,在有输出变化时会提示高亮. You can pre-set tmux
config: .tmux.conf
, there are a lot examples on Internet.