Shell

行内操作

  • ^a: jump to BOL
  • ^e: jump to EOL
  • ^u: delete the line
  • ^k: delete to EOL
  • ^w: delete a word forward
  • alt+f: jump a word forward
  • alt+b: jump a word backward
  • ^r: search history
  • alt+.: complete second parameter

任务控制

  1. 执行command
  2. ^z挂起当前job
  3. bg后台继续该job
  4. fg召回前台

后台运行命令

command &

或者如果你不想看到任何输出,使用

command &> /dev/null &
  • 如此你可以继续使用当前shell
  • 使用bg查看是否有任务在后台运行
  • 使用jobs查看后台任务
  • 使用fg将任务召回前台
  • 不能退出shell,否则进程会被杀掉
  • 使用disown丢掉进程,可以退出shell

又:

nohup command &> /dev/null &

等价于以上的操作。单纯的nohup command会在当前目录创建一个隐藏文件以写入命令的输出。以上命令将程序的输出重定向至比特桶丢弃。

同时输出到console和文件

将命令输出重定向到文件:

SomeCommand > SomeFile.txt  # overwrite
SomeCommand >> SomeFile.txt # append

将命令输出(stdout)及报错(stderr)重定向到文件:

SomeCommand &> SomeFile.txt
SomeCommand &>> SomeFile.txt

同时输出到console和文件:

SomeCommand 2>&1 | tee SomeFile.txt     # overwrite
SomeCommand 2>&1 | tee -a SomeFile.txt  # append
          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
          ||          |          ||          |          ||
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append

Ref:

  1. https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file
  2. https://www.gnu.org/software/bash/manual/bash.html#Redirections

从系统中踢出某个用户

# See the pid of the user's login process.
$ who -u
yychi    tty1         2020-02-19 21:06        460
 
# Let him know he will be kick off.
$ echo "You'll be kick off by system administrator." | write yychi
 
# Kick off.
$ kill -9 460
 
# Done.

Ref: https://www.putorius.net/kick-user-off-linux-system.html

查看指定进程的所有连接

Cf. https://www.cnblogs.com/zl1991/p/10895485.html

# 查看pid为4721的所有TCP连接
lsof -p 4721 -nP | grep TCP