Command-line Environment · the missing semester of your cs education
- Metadata:
- Link: missing.csail.mit.edu
- Date: 2022年01月20日 09:10:30
- Tags: #Annotation
-
- [ ]
- 总结
- [[01. Linux 和 命令行]]
Your shell is using a UNIX communication mechanism called a signal to communicate information to the process.
备注:
In our case, when typing
Ctrl-C
this prompts the shell to deliver aSIGINT
signal to the process.
备注:
Signals can do other things beyond killing a process. For instance,
SIGSTOP
pauses a process. In the terminal, typingCtrl-Z
will prompt the shell to send aSIGTSTP
signal, short for Terminal Stop (i.e. the terminal’s version ofSIGSTOP
).We can then continue the paused job in the foreground or in the background using
fg
orbg
, respectively.
备注:
The
jobs
command lists the unfinished jobs associated with the current terminal session.
备注:
More intuitively, you can also refer to a process using the percent symbol followed by its job number (displayed by
jobs
). To refer to the last backgrounded job you can use the$!
special parameter.
备注:
One more thing to know is that the
&
suffix in a command will run the command in the background, giving you the prompt back, although it will still use the shell’s STDOUT which can be annoying (use shell redirections in that case).
备注:
To background an already running program you can do
Ctrl-Z
followed bybg
. Note that backgrounded processes are still children processes of your terminal and will die if you close the terminal (this will send yet another signal,SIGHUP
). To prevent that from happening you can run the program withnohup
(a wrapper to ignoreSIGHUP
), or usedisown
if the process has already been started.
备注:
$ sleep 1000 ^Z [1] + 18653 suspended sleep 1000 $ nohup sleep 2000 & [2] 18745 appending output to nohup.out $ jobs [1] + suspended sleep 1000 [2] - running nohup sleep 2000 $ bg %1 [1] - 18653 continued sleep 1000 $ jobs [1] - running sleep 1000 [2] + running nohup sleep 2000 $ kill -STOP %1 [1] + 18653 suspended (signal) sleep 1000 $ jobs [1] + suspended (signal) sleep 1000 [2] - running nohup sleep 2000 $ kill -SIGHUP %1 [1] + 18653 hangup sleep 1000 $ jobs [2] + running nohup sleep 2000 $ kill -SIGHUP %2 $ jobs [2] + running nohup sleep 2000 $ kill %2 [2] + 18745 terminated nohup sleep 2000 $ jobs
备注:
A special signal is
SIGKILL
since it cannot be captured by the process and it will always terminate it immediately. However, it can have bad side effects such as leaving orphaned children processes.
备注:
You can learn more about these and other signals here or typing
man signal
orkill -l
.
备注:
- Sessions - a session is an independent workspace with one or more windows
tmux
starts a new session.tmux new -s NAME
starts it with that name.tmux ls
lists the current sessions- Within
tmux
typing<C-b> d
detaches the current sessiontmux a
attaches the last session. You can use-t
flag to specify which- Windows - Equivalent to tabs in editors or browsers, they are visually separate parts of the same session
<C-b> c
Creates a new window. To close it you can just terminate the shells doing<C-d>
<C-b> N
Go to the N th window. Note they are numbered<C-b> p
Goes to the previous window<C-b> n
Goes to the next window<C-b> ,
Rename the current window<C-b> w
List current windows- Panes - Like vim splits, panes let you have multiple shells in the same visual display.
<C-b> "
Split the current pane horizontally<C-b> %
Split the current pane vertically<C-b> <direction>
Move to the pane in the specified direction. Direction here means arrow keys.<C-b> z
Toggle zoom for the current pane<C-b> [
Start scrollback. You can then press<space>
to start a selection and<enter>
to copy that selection.<C-b> <space>
Cycle through pane arrangements.
备注:
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519
备注:
You should choose a passphrase, to avoid someone who gets hold of your private key to access authorized servers. Use
ssh-agent
orgpg-agent
so you do not have to type your passphrase every time.
备注:
A simpler solution can be achieved with
ssh-copy-id
where available:ssh-copy-id -i .ssh/id_ed25519.pub foobar@remote
备注:
The most common scenario is local port forwarding, where a service in the remote machine listens in a port and you want to link a port in your local machine to forward to the remote port. For example, if we execute
jupyter notebook
in the remote server that listens to the port8888
. Thus, to forward that to the local port9999
, we would dossh -L 9999:localhost:8888 foobar@remote_server
and then navigate tolocahost:9999
in our local machine.
备注: port forward 对服务器来说用的很爽,也不会暴露不安全端口给公网,值得一记
However, there is a better alternative using
~/.ssh/config
.Host vm User foobar HostName 172.16.174.141 Port 2222 IdentityFile ~/.ssh/id_ed25519 LocalForward 9999 localhost:8888 # Configs can also take wildcards Host *.mit.edu User foobaz
备注: