Linux-nohup command: Run commands in the background of the system without hanging up. Exiting the terminal will not affect the running of the program.

1. nohup command (available to all users)

        The full name of nohup  in English is no hang up ( no hang up ), which is used to run commands in the background of the system without hanging up . Exiting the terminal will not affect the running of the program .

        The nohup  command, by default (when not redirected: in layman's terms, does not specify which file the results are output to), will output a file named nohup.out to the current directory . If the nohup.out file in the current directory is not available Write, output is redirected to  the $HOME/nohup.out  file.

2. Grammar examples

1. Basic syntax of nohup

1)语法格式:
nohup Command [ Arg … ] [ & ]

2)参数说明:
Command:要执行的命令。
Arg:一些参数,可以指定输出文件。
&:让命令在后台执行,终端退出后命令仍旧执行。

2. Usage examples (two examples)

Code:

例1:
#在后台执行当前目录下的test_user_str.sh,终端退出后命令仍旧执行
nohup test_user_str.sh &
#命令运行成功,终端会出现如下显示
appending output to nohup.out

例2:
#在后台执行test_user_str.sh,终端退出后命令仍旧执行,并且将运行结果输出到当前目录的test.log
nohup test_user_str.sh > test.log &

operation result:

3. Stop nohup

1. Find the running process

#你需要使用以下命令查找到 nohup 运行脚本的进程 PID(以上面例1为例子)
ps -aux | grep "test_user_str.sh" 

#参数说明:
#     a : 显示所有程序
#     u : 以用户为主的格式来显示
#     x : 显示所有程序,不区分终端机

2. Kill the process

#杀死PID等于11的进程(上面查到pid是多少就杀多少)
kill -11

Guess you like

Origin blog.csdn.net/weixin_45440484/article/details/128633759