linux learning one

nohup and & run in the background, process viewing and termination

1.nohup

Purpose: Run a command without hanging up.

Syntax: nohup Command [ Arg … ] [ & ]

  Whether or not the output of the nohup command is redirected to the terminal, the output is appended to the nohup.out file in the current directory.

  If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file.

  The command specified by the Command parameter cannot be invoked if no file can be created or opened for appending.

Exit Status: The command returns the following exit values:   
  126 The command specified by the Command parameter can be found but not invoked.   
  127 The nohup command encountered an error or could not find the command specified by the Command parameter.   
  Otherwise, the exit status of the nohup command is the exit status of the command specified by the Command parameter.

2.&

Purpose: run in the background

Usually two are used together

nohup command &

eg:

1
nohup /usr/local/node/bin/node /www/im/chat.js >> /usr/local/node/output.log 2>&1 &

Process ID 7585

View running background processes

(1)jobs -l

The jobs command only sees that the current terminal is in effect. After closing the terminal, the jobs running in the other terminal can no longer see the programs running in the background. At this time, use ps (process view command)

(2) ps -ef 

1
ps -aux|grep chat.js
a: show all programs
 u: Display in user-oriented format
 x: Display all programs, not differentiated by terminal

Note:

  Finding processes with ps -def | grep is convenient, the last line will always grep itself

  The grep command can be excluded with the grep -v parameter

1
ps -aux|grep chat.js| grep -v grep

  Then use awk to extract the process ID 

1
ps -aux|grep chat.js| grep -v grep | awk  '{print $2}'

 
   

3. If a process fails to get up, it may be that a port is occupied

View processes using a port

1
lsof -i:8090

1
netstat -ap|grep 8090

After viewing the process id, use the netstat command to view the port occupied by it

1
netstat -nap|grep 7779

Use kill to kill and enter the city and then start

4. Terminate processes running in the background

1
kill -9  进程号

kill命令用来删除执行中的程序或工作。kill可将指定的信息送至程序。预设的信息为SIGTERM(15),可将指定程序终止。若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除程序。程序或工作的编号可利用ps指令或job指令查看。

语法

kill(选项)(参数)

选项

-a:当处理当前进程时,不限制命令名和进程号的对应关系;
-l <信息编号>:若不加<信息编号>选项,则-l参数会列出全部的信息名称;
-p:指定kill 命令只打印相关进程的进程号,而不发送任何信号;
-s <信息名称或编号>:指定要送出的信息;
-u:指定用户。

参数

进程或作业识别号:指定要删除的进程或作业。

实例

列出所有信号名称:

 kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

只有第9种信号(SIGKILL)才可以无条件终止进程,其他信号进程都有权利忽略,下面是常用的信号:

HUP     1    终端断线
INT     2    中断(同 Ctrl + C)
QUIT    3    退出(同 Ctrl + \)
TERM   15    终止
KILL    9    强制终止
CONT   18    继续(与STOP相反, fg/bg命令)
STOP   19    暂停(同 Ctrl + Z)

先用ps查找进程,然后用kill杀掉:

ps -ef | grep vim
root      3268  2884  0 16:21 pts/1    00:00:00 vim install.log
root      3370  2822  0 16:21 pts/0    00:00:00 grep vim

kill 3268
kill 3268
-bash: kill: (3268) - 没有那个进程

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324429108&siteId=291194637