Author | Ponytails and Shawls
Link | https://blog.51cto.com/u_10808695/1841519
Edit | I don't love machine learning
1. Pause a process
kill -STOP 1234 // 将该进程暂停
2. Continue a process
To bring it back to the background, use:
kill -CONT 1234 // (很多在前台运行的程序这样是不行的)
If you want to return to the foreground, use the jobs command to query the suspended process in the terminal where the process was running at the time . Then use fg [job number] to restore the process to the foreground.
fg, bg, jobs, &, ctrl+z are all related to system tasks. Although these commands are basically not needed now, they are very useful to learn.
-
& is most often used: this is used at the end of a command to put the command in the background for execution
-
ctrl + z : You can put a command that is executing in the foreground into the background and pause it
-
jobs : see how many commands are currently running in the background
-
fg : transfer the command in the background to the foreground to continue running
-
If there are multiple commands in the background, you can use fg %jobnumber to call out the selected command. %jobnumber is the serial number (not pid) of the command being executed in the background found through the jobs command.
-
bg : Change a command suspended in the background to continue execution
-
If there are multiple commands in the background, you can use bg %jobnumber to call out the selected command. %jobnumber is the serial number (not pid) of the command being executed in the background found through the jobs command.
3. Using Shell Commands to Control Task Jobs Execution under Linux
The following commands can be used to manipulate process tasks:
-
ps lists the running processes in the system;
-
kill sends a signal to one or more processes (often used to kill a process);
-
jobs list the status of tasks that have been started in the current shell environment. If jobsid is not specified, all active task status information is displayed; if the termination of a task is reported (that is, the status of the task is marked as Terminated), the shell starts from the current shell. The process ID of the delete task in the list known to the environment;
-
bg moves the process to the background (Background);
-
fg moves the process to the foreground (Foreground);
If there are 2 task numbers in the background, [1], [2]; if the first background task is successfully executed and the second background task is still being executed, the current task will automatically become the background task number" [2]" background task. So it can be concluded that the current task will change. When the user enters commands such as "fg", "bg", and "stop", if no quotation marks are added , all the changes are the current task.
view jobs
Use the jobs or ps command to view the jobs being executed.
The result of the jobs command execution, + indicates a current job, -list is a job after a current job, the jobs -l option can display the PIDs of all tasks, and the status of jobs can be running, stopped, Terminated, but if When the task is terminated (kill), the shell removes the task's process ID from the list known to the current shell environment; that is, the jobs command displays background running or suspended tasks in the current shell environment information;
process suspension
Suspension of background processes:
Execute through the stop command, view the job number (assuming num) through the jobs command, and then execute stop %num ;
When you want to re-execute the currently suspended task, you can change the status of the suspended job from stopped to running through bg %num , and it is still executed in the background; when it needs to be executed in the foreground, execute the command fg %num You can;
Suspend the foreground process: ctrl+Z
termination of the process
Termination of background processes:
-
View the job number (assuming num) through the jobs command, and then execute kill %num
-
View the process number (PID, assuming pid) of the job through the ps command, and then execute kill pid
Termination of foreground process: ctrl+c
4. Other functions of kill
In addition to killing the process, kill can also send other signals to the process. Use kill -l to view the signals supported by kill.
SIGTERM is a signal sent by kill without parameters, which means that the process is to be terminated, but it depends on whether the process supports it or not.
If the process has not terminated, you can use kill -SIGKILL pid , which is terminated by the kernel, and the process cannot listen for this signal.
Under Unix/Linux, you generally want a program to run in the background, and many use & at the end of the program to make the program run automatically. For example, to run MySQL in the background:
/usr/local/mysql/bin/mysqld_safe --user=mysql &
But many of our programs cannot be made into daemons like mysqld. Maybe our programs are just ordinary programs. Generally, even if the program ends with & , if the terminal is closed, the program will also be closed. In order to run in the background, we need to use the nohup command. For example, we have a start.sh that needs to run in the background, and we want to run in the background all the time, then use nohup:
nohup /root/start.sh &
Prompt after carriage return in the shell:
[1] 15696
The PID of the current process. At this time, close the terminal, open it again, and use the ps command to view it. The pid of 15696 is still there.