How to run programs in the background after Xshell is closed

The nohup method of running programs in the background after Xshell is closed


nohup usage

You want your program to run in the background. The nohup command can run the program in the background in the form of ignoring the hang signal. Even if the current xshell is closed, it will be running the next time you log in.

语法:
nohup 你要执行的命令

Regardless of whether the output of the nohup command is redirected to the terminal, the output result of the nohup command execution will be written to the nohup. out file in the current directory. If the nohup. out file in the current directory is forbidden to write data, the result of the nohup command will be output automatically Go to the $HOME/ nohuo. out file .

  1. Nohup directly adds the command to be executed, even if your terminal is closed, the program will continue to run in the background
[root@chaogelinux ~]# nohup ping baidu.com 

之后会给出提示:
nohup:忽略输入并把输出追加到" nohup.out"
#注:输出结果会写入到当前目录的nohup. out文件中,但是程序会卡在前台!
#关闭终端,命令不会挂掉,进程还会一直在运行,直到进程被杀死
  1. Generally, using nohup will not close the window directly after typing the command, but hope to continue to run the command line, just add an ampersand at the end. (recommend
nohup ping baidu. com &
  1. The execution result of the command is not displayed, and the correct output result and the wrong output result of the ping process are written to the nohup.out file (recommend)
    Standard error output: 2
    Standard output: 1
nohup ping www.pythonav.cn > nohup.out 2>&1 & 

#把命令放在后台运行,且无论是正确的输出或是错误的输出,都直接扔进垃圾站(linux的黑洞文件), 直接销毁。
nohup ping www.pythonav.cn > / dev/null 2>&1 &

Observation and process

  1. vim directly view the nohup.out file
vim nohup.out
  1. cat view
cat nohup.out
  1. more View: Space is the size of scrolling a screen, = is to display the current line number, q is to exit.
分屏显示文件内容:
more nohup.out 
  1. You can observe the process in real time, which is particularly easy to use : If
    you want to pause the refresh, use [Ctrl]+[S] to pause the refresh.
    If you want to continue the terminal, use [Ctrl]+[Q].
    If you want to exit the tail command, use [Ctrl]+[C] directly.
tail -f nohup.out

Top usage to observe the progress of the process

Direct input top : After entering the top command, press the q command to indicate quit, exit top

#top's actual use: After entering the top command, enter the number 1 instruction, which means to view the number of logical cpus in Linux
# Sort by memory usage: After entering the top, enter the uppercase M command, and the memory usage is sorted from largest to smallest
#top displays the absolute path of the process: top -C#displays the absolute path of the process command
#Set the refresh time of the top process: top -d seconds
#Set the refresh times of the top command: top -n 3
#end after refreshing 3 times#top Specify the process and observe the dynamic resource information separately: top -P pid #Specify
a column to highlight input: z, turn on the color; input x, a column is highlighted; input b, a column color is bold

<> Move left and right

The interface is as follows:
Insert picture description here

Use htop to observe the process

This is better than top, you need to download and install first:

sudo apt-get install htop

Then enter htop:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41917697/article/details/114632924