Detailed Linux background running and shutting down, viewing background tasks

Source: Internet
Author: User

First, &

At the end of a command, you can put this command in the background to execute, as

Watch-n sh test.sh & #每10s在后台执行一次test. Sh script

Two, CTRL + Z (the task is placed in the background and in a paused state)

You can place a command that is being executed in the foreground in the background and be in a paused state.

Iii. Jobs (see how many commands are currently running in the background)

The JOBS-L option shows that the pid,jobs status of all tasks can be running, stopped, Terminated (terminating). However, if the task is terminated (kill), the shell removes the process identity of the task from the list known to the current shell environment. In other words, the jobs command shows the task information that is running in the background or suspended in the current shell environment.

FG (background task is moved to foreground operation)

The commands in the background are moved to the foreground to continue running. If there are multiple commands in the background, you can use FG%jobnumber (jobs-l to view the command number, not the process number) to bring up the selected command.

BG (background suspended tasks continue to run in the background)

A command that pauses in the background changes to execute in the background. If there are multiple commands in the background, you can use BG%jobnumber to bring up the selected command.

Move a task to run in the background: Ctrl + Z, then BG%jobnumber, so the process is moved to the background and the terminal can continue to accept commands.

Concept: Current task

If there are 2 task numbers in the background, [1],[2]; If the first background task executes successfully and the second background task is still in progress, the current task will automatically become a background task for the background task number "[2]". So it can be concluded that the current task is subject to change. When the user enters commands such as FG, BG, and stop, the current task is changed if no quotation marks are added

Vi. Kill

    • Scenario 1: View the job number (assuming num) with the jobs command and then execute the kill%num
    • Scenario 2: View the Job's process number (PID, assuming PID) with the PS command and then execute the kill PID (with limitations such as background processes, daemons, etc.)

Termination of the foreground process: Ctrl + C

Other effects of Kill

In addition to terminating the process, kill can also send other signals to the process, using kill-l to see the signals that kill supports.

Sigterm is the signal that kill sends without parameters, meaning that the process terminates, but execution depends on whether the process supports it. If the process has not yet terminated, you can use the Kill-sigkill pid (kill-9 pid), which is the kernel to terminate the process and the process cannot listen for this signal.

Seven, Nohup

If the program is always executed in the background, even if the current terminal is closed (& cannot be done before), Nohup is required. This command can continue to run the process after you exit the account/close the terminal. After shutting down the terminal, jobs in the other terminal can not see the background running program, this time using PS (Process View command).

" test.sh " #a: Show All Programs U: Display x in user-oriented format: Show All Programs, do not differentiate by terminal

Detailed Linux background running and shutting down, viewing background tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.