Efficiently manage Linux processes: how to execute programs in the background, view processes, and terminate tasks


foreword

Efficiently manage Linux processes: how to execute programs in the background, view processes, and terminate tasks

1. Detailed explanation of nohup command

1-1. Introduction to nohup command

nohup : nohup is an abbreviation for "no hangup", which is used to run commands in the background without hanging up, so that it is not affected when the user exits or the terminal is closed. It does this by ignoring the SIGHUP signal. In other words, nohup is a Unix command used to execute a program in the background, even if the current terminal session is closed or disconnected, the program can continue to run. The use of the nohup command is very simple, just add nohup and & symbols before the command to be executed.

1-2. Grammatical format

1-2-1. Introduction to Basic Grammar

The syntax of the nohup command is as follows :

nohup command [arg...] &

# 其中,command 表示要在后台执行的命令或进程,arg 表示命令或进程的参数,& 符号表示将命令或进程放到后台执行。

Note : The execution result of the nohup command will be output to the nohup.out file in the current directory. If you want to redirect the output of a command or process into another file, you can use the redirection symbol (>) to specify the path to the output file. For example:

nohup command [arg...] > output_file &

1-2-2. Execute the script file

When executing the nohup command, if the command is a script file, you need to add executable permission to the script file (chmod +x script.sh). Otherwise, it will prompt "no such file or directory" error. Execute the nohup command after adding execution permissions

nohup sh filename.sh &

1-2-3. Execute the python file

Executing python files : The nohup command is typically used for long-running tasks, such as running background processes on a server or executing scripts that take a long time. When using the nohup command, it is recommended to redirect the output of the program to a log file so that you can check the running status of the program later.

For example, to execute a Python script in the background and redirect the output to a log file, use the following command:

nohup python my_script.py > my_script.log &

This will execute the my_script.py script in the background and redirect the output to the my_script.log file. If the current terminal session is closed or disconnected, my_script.py will continue to run in the background and write output to the my_script.log file.

1-2-4. Extended extension: running background processes on the server

Extended extension : A common use of the nohup command is to run background processes on a server, such as a web server, database server, etc. When executing these services, you can use the nohup command to put the service in the background and redirect the output to a log file.

For example, on a Linux system, to start an Apache web server in the background and redirect output to a log file, use the following command:

nohup /usr/local/apache2/bin/httpd -k start > /var/log/httpd.log &
this will start the Apache web server in the background and redirect the output to the /var/log/httpd.log file.

It should be noted that the nohup command is not a panacea, and it cannot guarantee that the executed command or process will always run. If there is a problem or anomaly in the command or process itself, it may still be terminated or crashed. Therefore, when using the nohup command, it is still necessary to monitor and manage the executed command or process.

The difference between 1-2-5, nohup and &

& : refers to running in the background, when the user exits (hangs), the command automatically ends

nohup : run without hanging up, note that there is no background running function, which means that running commands with nohup can make the commands execute permanently, which has nothing to do with the user terminal. For example, if we disconnect the SSH connection, it will not affect its operation. Note that nohup does not mean running in the background; & is running in the background. After using nohup, the standard input is closed, and the terminal can no longer accept any input.

Therefore, the combination of nohup and & can realize the function of permanently executing the command in the background. After the combination, the terminal can accept any input.

Two, process view

2-1, jobs command (basically not used)

jobs command : The jobs command is used to view background tasks in the current session. It can only show background processes in the current terminal , i.e. processes associated with the current session. Some commonly used jobs parameters are as follows:

  • -l: List process ID and task status
  • -n: list only tasks whose status has changed
  • -r: list only running tasks
  • -s: list only stopped tasks

For example, use the jobs -l command to view background tasks and their process IDs in the current session.

2-2. ps command

ps command : The ps command is used to report the process status of the current system. Unlike the jobs command, ps can display process information for all users, not just the current session. Some commonly used ps parameters are as follows:

  • -A: show all processes
  • -u: Display processes by user, display detailed information of processes, including user, process number, CPU usage, memory usage and other information.
  • -x: show processes without a controlling terminal
  • -e: display environment variables
  • -aux : Display detailed information of all processes, including user, process number, occupied CPU and memory, etc. (Usually just use aux directly)

For example, use the ps -A command to view all processes in the system. To find a specific process, you can pipe the output to the grep command, such as ps -A | grep my_script.sh.
insert image description here
insert image description here

2-3. top command

top command : The top command provides a real-time dynamic process monitor. It can view the real-time status of all processes in the system and sort the processes according to their resource usage. In the top interface, you can see the process ID, user, CPU usage, memory usage and other information.

To find a specific process, you can press the u key in the top interface and enter a user name to filter the user's processes. In addition, you can also press the o key, and then enter filter conditions, such as COMMAND=my_script.sh. Press Enter to confirm, and top will only display processes that match the criteria. Finally, press ctrl+c to exit the preview.
insert image description here

3. Terminate the process

3-1. Introduction to the concept of process termination

Process termination refers to the process by which a running process ends its execution. Process termination can be normal (for example, the process has completed its task, or the user has requested termination of the process through an interface to the operating system) or abnormal (for example, the process has crashed or encountered an error). Here are some details about process termination:

  • Normal termination: When a process has completed its task and exited normally, it notifies the operating system, which marks the process as "exited". At this time, the operating system will release the memory occupied by the process, close resources such as files related to the process, and return the exit code (an integer value) of the process to the parent process. The parent process can determine the status of the process completion by reading this exit code. If the parent process does not call the wait() or waitpid() function in time to obtain the exit status, the process will become a "zombie process" until the parent process obtains the exit status or the parent process itself terminates.

  • Abnormal Termination: When a process encounters an error or crashes, it may be forcibly terminated by the operating system. For example, if a process segfaults (accesses unallocated memory), or if the process exceeds allowed resource limits (such as memory limits or time limits), the operating system terminates the process and reports an error message to its parent process. In this case, the parent process can obtain the status of the process termination by calling the wait() or waitpid() function.

  • Signal termination: The operating system can send a signal to a process to terminate the process. For example, if the user presses Ctrl+C at the command line, the operating system sends a SIGINT signal to the running process, telling it to terminate execution. Processes can catch these signals and take appropriate action, for example, saving progress and safely terminating execution. If the process does not catch these signals, the operating system will terminate the process by default and report an error message to its parent process.

In summary, process termination is a very important concept that plays a vital role in the stability and security of the operating system. Understanding the reasons and mechanisms of process termination can help us better understand how the operating system works, and thus better develop and debug applications.

3-2. Introduction to kill command

The kill command is used to terminate the specified process. Common options include :

  • -9: Forcibly terminate the process, which is equivalent to sending the SIGKILL signal.
  • -15 (default option): Terminate the process normally, equivalent to sending a SIGTERM signal.

For example, to kill process number 12345, use the following command:

# 使用top、ps等命令查看进程号
kill 12345

This will send a SIGTERM signal to process number 12345, causing it to terminate gracefully.

If the process cannot be terminated normally, the -9 option can be used to forcibly terminate the process. For example:

kill -9 12345

This will send a SIGKILL signal to process number 12345, forcing it to terminate.

Be aware that killing a process may result in loss of unsaved data or other unpredictable consequences. When using the kill command, you should try to avoid killing processes by mistake and ensure that only necessary processes are killed.


Summarize

Knowledge points have increased!

Guess you like

Origin blog.csdn.net/weixin_42475060/article/details/130082364