Run the program in the background in linux

Source: Internet
Author: User

 

Test. sh:

 

I = 1

While:

Do

Echo $ I

Sleep 1

(I ++ ))

Done 1. When running a job on the foreground, the supervisor will be occupied by this job, so you need to open another terminal for other operations. To avoid this inconvenience, you can put the job in the background for execution, there are two main methods:

 

1. & commands

 

Sh test. sh &

 

This command places the script in the background for execution, but the standard output is still displayed on the current terminal, affecting user operations, so it is best to redirect the output to other files

 

Sh test. sh &>/dev/null

 

To view the output result, you can also direct it to a fixed file.

 

2. Run ctrl + z; bg and other commands to run the jobs that are already running on the foreground in the background.

 

If a job has been executed on the foreground, you can use ctrl + z to move the job to the background and suspend it. Then, run the jobs command to view the jobs executed in the background and find the corresponding job ID. Run bg % n (n is the job ID found through jobs) to wake up the job and continue running.

 

This method also outputs the results to the terminal. You can also use the redirection method to solve this problem.

 

Related commands:

 

Jobs ------------ view processes executed in the background

 

Fg % n ---------- transfer the background execution process n to the foreground for execution, and n to jobnumber (process number viewed through jobs, rather than pid)

 

Ctrl + z ---------- put the process executed on the foreground in the background and suspend

 

Bg % n --------- the process will be suspended in the background, continue to execute

 

Ctrl + c ---------- foreground process terminated

 

Kill % n --------- kill the processes running in the background, and n indicates jobnumber (process number viewed through jobs, rather than pid)

 

 

 

2. When a user logs out or the network is interrupted, the terminal receives a SIGHUP signal to close all its sub-processes. The above two methods will exit as the terminal closes, if we need to execute jobs in the background without being affected by terminal exit, we can use the following two methods:

 

1. nohup command

 

Nohup sh test. sh &>/dev/null &

 

The nohup command ignores the SIGHUP signal, so that the background job is not affected when the terminal exits.

 

2. Attach a job to a new session

 

(Sh test. sh &>/dev/null &) or place sh test. sh &>/dev/null & in another script to run it.

 

After adding & to (), we will find that the submitted jobs are not in the job list, that is, they cannot be viewed through jobs, we found that the PPID of the new job is 1 rather than the PID of the terminal through the ps view, so the terminal will not affect our job after exiting.

 

 

 

3. In addition, the screen command can implement corresponding functions and solve the problem that the program requires human-computer interaction.

 

 

 

This article is from the "Farmer-Hold" blog

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.