nohub and & run programs in the background without interruption on linux

1. nohub (if not installed, install it first)

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 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 is not invoked if no file can be created or opened for appending.

Exit Status: The command returned the following exit values:   

2.&

Purpose: run in the background

3. View process

jobs command

Only look at the current terminal to take effect . 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) or view the GPU process

 The jobs -l option can display the PIDs of all tasks in the current terminal, and the status of jobs can be running, stopped, or terminated. The + sign indicates the current task, and the - sign indicates the next task.

ps command

Function: View all current processes

 ps -aux | grep "test.sh" #a: Display all programs u: Display in user-oriented format x: Display all programs, not distinguished by terminal

ps -A / ps -e //显示所有进程信息
ps -u root //显示指定用户信息
ps -ef //显示所有进程信息,连同命令行
ps -ef|grep ssh // ps 与grep 常用组合用法,查找特定进程
ps -l // 将目前属于您自己这次登入的 PID 与相关信息列示出来
ps aux | egrep '(cron|syslog)' // 找出与 cron 与 syslog 这两个服务有关的 PID 号码

sudo nvidia-smi can view the processes running on the GPU

sudo nvidia-smi

4. Redirection of nohup

The following is the definition of the log printing level. Except for the alarm information higher than level 2 recorded in the log file, the rest will not be recorded directly

//只输出错误信息到日志文件
nohup ./program >/dev/null 2>log &
//什么信息也不要
nohup ./program >/dev/null 2>&1 &

0: means standard input
1: standard output, in general use, the default is standard output
2: standard error information output

3. About the /dev/null file

There is also a special file /dev/null under Linux, which is like a bottomless pit, and all information redirected to it will disappear without a trace. This is very useful, when we don't need to echo all the information of the program, we can redirect the output to /dev/null.

5. Close the command currently running in the background

      kill command: end the process

     (1) View the jobnum through the jobs command, and then execute kill %jobnum

     (2) View the process number PID through the ps command, and then execute kill %PID

       If it is a foreground process, you can terminate it by directly executing Ctrl+c

6. Switching and control of front and back processes

     (1) fg command

       Function: Transfer the commands in the background to the foreground to continue running

       If there are multiple commands in the background, you can use jobs to view jobnun first, and then use fg %jobnum to call out the selected command.

     (2) Ctrl + z command

       Function: Put a command that is being executed in the foreground into the background, and it is in a suspended state

     (3) bg command

       Function: Change a command suspended in the background to continue execution in the background

       If there are multiple commands in the background, you can use jobs to view the jobnum first, and then use bg %jobnum to recall the selected command and continue to execute.

7. The top command displays the process

A performance analysis tool that can display the resource usage of each process in the system in real time

Guess you like

Origin blog.csdn.net/qq_42845932/article/details/125232631