Use of linux front and back task commands bg, fg, ctrl+z, ctrl+d and ctrl+c

1. & (commonly used)

& Is used at the end of a command. After use, you can put this command in the background for execution.
Example:$ pct &

二、ctrl + z

Suspend a command executed in the foreground

Three, jobs

See how many commands are currently running in the background

The result of jobs command execution, + (plus sign) means a current job,-(minus sign) means a job after the current job, jobs -l option can display the PID of all tasks.

The status of jobs can be running, stopped, or Terminated, but if the task is terminated (kill), the shell deletes the process ID of the task from the list known in the current shell environment;

In other words, the jobs command displays information about the tasks that are running or suspended in the background in the current shell environment;
Insert picture description here
[1] indicates that the jobnumber is 1, the PID is the process identification number, and the status of jobs is stopped, followed by the command

Four, fg

Transfer commands 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 by the jobs command

Insert picture description here
Open git.txt with vim to run in the foreground, press ctrl + Z to suspend it, and bg will run in the background.

Five, bg

Turn a command that is paused in the background into continued execution

Through bg %num, the status of the suspended job can be changed from stopped to running, and it is still executing in the background; when it needs to be changed to execute in the foreground, execute the command fg %num;

Insert picture description here
The pct above is a graphical user interface program. The non-graphical user interface is slightly different.

 如果后台中有多个命令,可以用bg %jobnumber将选中的命令调出,%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid)

Use Shell commands to control task execution under Linux

The following commands can be used to manipulate process tasks:

ps lists the processes running in the system;

kill sends a signal to one or more processes (usually used to kill a process);

jobs List the status of the tasks that have been started in the current shell environment. If jobsid is not specified, the status information of all active tasks will be displayed;

If the termination of a task is reported (that is, the status of the task is marked as Terminated), the shell deletes the task's process ID from the list known by the current shell environment;

bg moves the process to the background to run (Background);

fg moves the process to the foreground to run (Foreground);

Six, transfer the job to the background to run

If you often work under X graphics, you may have this experience: Run a GUI program through a terminal command, the GUI interface comes out, but your terminal stays in place, you can't continue to execute other commands in the shell, unless Turn off the GUI program.

In order to enable the terminal to continue to accept commands after the program is executed, you can move the process to the background and run the program with the following command: #If you want to run xmms
  Insert picture description here
like this, after opening pct, the terminal prompt comes back. Now pct is running in the background; but in case you forgot to use "&" when running the program, and don't want to execute it again;

You can use ctrl+z to suspend the program first, and then type the bg command, so that the program will continue to run in the background.

Concept: current task

If there are two background task numbers, [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 is subject to change. When the user enters commands such as "fg", "bg" and "stop", without any quotation marks, all changes are the current task.

Seven, the suspension of the foreground process

ctrl+Z;

8. Termination of the process

Termination of background process:

Method 1: Use the jobs command to view the job number (assuming it is num), and then execute kill %num $ kill %1

Method 2: Use the ps command to view the job process number (PID, assuming pid), and then execute kill pid $ kill 5270

Termination of the foreground process: ctrl+c

Other functions of kill: In addition to terminating the process, kill can also send other signals to the process. Use kill -l to view the signals supported by kill. SIGTERM is the signal sent by kill without parameters,

It means that the process should be terminated, but the execution depends on whether the process supports it. If the process has not been terminated, you can use kill -SIGKILL pid, this is the kernel to terminate the process, the process can not listen to this signal.

Nine, shortcut keys introduction

     ctrl+c强行中断当前程序的执行。

     ctrl+d表示结束当前输入(即用户不再给当前程序发出指令),那么Linux通常将结束当前程序。

     ctrl+z表示将当前前台运行的经常放在后台并挂起,如需其在后台继续运行,需用“bg 进程号”使其继续运行;再用"fg 进程号"可将后台进程前台化。

----- ctrl+c, ctrl+d, ctrl+z meaning in linux:

    ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程。常用于终止正在运行的程序。
    ctrl-z 发送 SIGTSTP 信号给前台进程组中的所有进程,常用于挂起一个进程。
    ctrl-d 不是发送信号,而是表示一个特殊的二进制值,表示 EOF。
    ctrl-\ 发送 SIGQUIT 信号给前台进程组中的所有进程,终止前台进程并生成 core 文件。
    ctrl-s:中断控制台输出;
    ctrl-q:恢复控制台输出;
    ctrl-l:清屏
    Ctrl+c是强制中断程序的执行。

Ctrl+z is to interrupt the task, but the task is not over, he is still in the process, he just maintains the suspended state.

The user can use the fg/bg operation to continue the foreground or background task,
fg: restart the interrupted task in the foreground,
bg: put the interrupted task in the background for execution.
ctrl-d represents a special binary value, which represents EOF.

Note: In the shell, ctrl-d means to exit the current shell

Guess you like

Origin blog.csdn.net/Daoke37/article/details/114849220