Run programs on Linux terminals in the background

Source: Internet
Author: User

Run programs on Linux terminals in the background

In Linux, if you want to run the process in the background, we usually add & to the end of the command. In fact, the command is put into a Job Queue:

$./Test. sh &

[1] 17208

$ Jobs-l

[1] + 17208 Running./test. sh &

For commands that have been executed on the foreground, you can also run them in the background. First, press ctrl + z to pause the running processes, and then run the bg command to run the stopped jobs in the background:

$./Test. sh

[1] + Stopped./test. sh

$ Bg % 1

[1] +./test. sh &

$ Jobs-l

[1] + 22794 Running./test. sh &

However, for processes executed from the top to the backend, the parent process is still the shell process of the current terminal. Once the parent process exits, The hangup signal is sent to all sub-processes, the child process also exits after receiving the hangup. If we want to continue running the process when exiting the shell, we need to use nohup to ignore the hangup signal, or setsid to set the parent process as the init process (process number 1)

$ Echo $

21734

$ Nohup./test. sh &

[1] 29016

$ Ps-ef | grep test

515 29710 21734 0 00:00:00 pts/12/bin/sh./test. sh

515 29713 21734 0 00:00:00 pts/12 grep test

$ Setsid./test. sh &

[1] 409

$ Ps-ef | grep test

515 410 1 0? 00:00:00/bin/sh./test. sh

515 413 21734 0 00:00:00 pts/12 grep test

The above test demonstrates that the process is run in the background with nohup/setsid and is not affected by the current shell exit. So what should we do for processes that are already running in the background? You can use the disown command:

$./Test. sh &

[1] 2539

$ Jobs-l

[1] + 2539 Running./test. sh &

$ Disown-h % 1

$ Ps-ef | grep test

515 410 1 0? 00:00:00/bin/sh./test. sh

515 2542 21734 0 00:00:00 pts/12 grep test

Another method is to execute a process in a subshell, which is similar to setsid. The method is simple. Enclose the command in parentheses:

$ (. /Test. sh &)

$ Ps-ef | grep test

515 410 1 0? 00:00:00/bin/sh./test. sh

515 12483 21734 0 00:00:00 pts/12 grep test

Note: The test environment in this article is Red Hat Enterprise Linux AS release 4 (Nahant Update 5) and shell is/bin/bash. Different OS and shell may have different commands. For example, there is no disown in ksh of AIX, but nohup-p PID can be used to obtain the same effect of disown.

Another more powerful method is to use screen. First, create a virtual terminal in disconnected mode, and then use the-r option to re-connect to the virtual terminal to execute any commands in it, can achieve the nohup effect, which is more convenient when multiple commands need to be executed continuously in the background:

$ Screen-dmS screen_test

$ Screen-list

There is a screen on:

27963. screen_test (Detached)

1 Socket in/tmp/uscreens/S-jiangfeng.

$ Screen-r screen_test

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)

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.