[Turn] "nohup and & background running, process viewing and termination"

Original title: "nohup and & background running, process viewing and termination"

Original link: https://www.cnblogs.com/baby123/p/6477429.html

1.nohup

Purpose: Run commands without hanging up.

Syntax: nohup Command [Arg…] [&]

Regardless of whether the output of the nohup command is redirected to the terminal, the output will be 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.

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

Exit status: This command returns the following exit values:

126 The command specified by the Command parameter can be searched but cannot be called.

127 An error occurred in the nohup command or the command specified by the Command parameter could not be found.

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

Generally use two together

nohup command &

eg:

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

img

Process ID 7585

View running background processes

(1)jobs -l

img

The jobs command only takes effect on the current terminal. After closing the terminal, the jobs running in the background can no longer be seen in another terminal. At this time, use ps (process view command)

(2) ps -ef

ps -aux|grep chat.js
 a:显示所有程序 
 u:以用户为主的格式来显示 
 x:显示所有程序,不以终端机来区分

img

Note:

Use ps -def | grep to find the process is very convenient, the last line will always grep itself

Use the grep -v parameter to exclude the grep command

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

img

Use awk to extract the process ID

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

img


3. If a process can't get up, it may be that a certain port is occupied

View the process using a port

lsof -i:8090

img

netstat -ap|grep 8090

img

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

netstat -nap|grep 7779

img

Use kill to kill and enter the city before starting

4. Terminate processes running in the background

kill -9 进程号

img

Guess you like

Origin blog.csdn.net/weixin_43438052/article/details/113999564