Linux task control (bg job fg nohup & amp ;)

Source: Internet
Author: User

Linux task control (bg job fg nohup &) I. Introduction The biggest advantage of Linux/Unix which is different from Microsoft platform is real multi-user and multi-task. Therefore, there are also special management ideas in task management. We know that in Windows, we either have a program running on the background as a service or stop the service. The program cannot be switched between the foreground and background. Linux provides fg and bg commands, allowing you to easily schedule running tasks. Suppose you find it takes a long time to run a program on the foreground, but you need to do other things, you can use Ctrl-Z to suspend the program, and then you can see the system prompt: [1] + Stopped/root/bin/rsync. sh then we can schedule the program to the background for execution: (the number following bg is the job number) # bg 1 [1] +/root/bin/rsync. sh & run the jobs command to view the Running tasks: # jobs [1] + Running/root/bin/rsync. sh & if you want to call it back to the foreground, use # fg 1/root/bin/rsync. sh. In this way, you can only wait for the task to be completed on the console. & Throw the command to the background and execute [ctrl] + z. Roll the foreground task to the background. Pause jobs in the background. Check the background working status. fg % jobnumber. The background task is taken to the foreground to process bg %. jobnumber puts the task in the background to Process Task 2 in the kill management background. & in Linux, when running a job on the foreground, the terminal is occupied by the job, but it does not occupy the terminal when running the job on the background. You can run the job in the background using the & command. In fact, the command is put into a Job Queue: $. /test. sh & [1] 17208 $ jobs-l [1] + 17208 Running. /test. sh & be careful when running jobs in the background: Do not run commands that require user interaction in the background, because your machine will be silly there. However, running a job in the background will output the result to the screen, interfering with your work. If a job running in the background produces a large amount of output, you 'd better use the following method to redirect its output to a file: command> out. file 2> & 1 & in the above example, 2> & 1 indicates that all the standard output and error output will be redirected to an out. file. After a process is successfully submitted, a process number is displayed, which can be used to monitor or kill the process. For example, find the name "httpd. conf file, and redirect all standard output and error output to find. in the dt file: # find/etc/httpd/-name "httpd. conf "-print> find. dt 2> & 1 & [2] 7832 after the command is successfully submitted, the system gives its process number 7832. 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, for example, tesh is being executed on the front-end. sh use ctrl + z to suspend it: $. /test. sh [1] + Stopped. /test. sh $ bg % 1 [1] +. /test. sh & $ jobs-l [1] + 22794 Running. /test. sh & however, for the process executed from the top to the background, its 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 test515 29710 21734 0 00:00:00 pts/12/bin/sh. /test. sh515 29713 21734 0 00:00:00 pts/12 grep test $ setsid. /test. sh & [1] 409 $ ps-ef | grep test515 410 1 0 11: 49? 00:00:00/bin/sh. /test. the above test in sh515 413 21734 0 00:00:00 pts/12 grep test demonstrates the use of nohup/setsid plus & to make the process run in the background, without the impact of the current shell exit. So what should we do for processes that are already running in the background? You can run the disown command: $. /test. sh & [1] 2539 $ jobs-l [1] + 2539 Running. /test. sh & $ disown-h % 1 $ ps-ef | grep test515 410 1 0 11: 49? 00:00:00/bin/sh. /test. sh515 2542 21734 0 00:00:00 pts/12 grep test there is another way, even if the process is executed in a subshell, this is actually the same as setsid. The method is simple. Enclose the command in parentheses (): $ (./test. sh &) $ ps-ef | grep test515 410 1 0? 00:00:00/bin/sh. /test. sh515 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), shell is/bin/bash, different OS and shell commands may be different. 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-listThere is a screen on: 27963. screen_test (Detached) 1 Socket in/tmp/uscreens/S-jiangfeng. $ screen-r screen_test 3. If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account. Nohup means no hang up ). This command is generally in the form of: nohup conmmand & if you use the nohup command to submit a job, all the output of this job will be redirected to a job named nohup by default. unless the output file: nohup command> myout is specified. file 2> & 1 in the preceding example, the output is redirected to myout. file. 4 ,.*,?, [...], [!...] The following are the special characters: * match any string in the file name, including an empty string.? Match any single character in the file name. [...] Matches any character in. [!...] Match!. When s h e l encounters the above characters, it will treat them as special characters instead of common characters in the file name, so that users can use them to match the corresponding file name. 1) list file names starting with I or o: # ls [io] * 2) List logs. start with, followed by a number, and then can be any string file name: # ls log. [0-9] * 3) List logs in contrast to Example 2. start with, followed by no number, and can then be any string file name: # ls log. [! 0-9] * 4) list all files whose names start with LPS and can contain any two characters in the middle, and end with 1: # ls LPS ?? 15) list all names starting with an uppercase letter: $ ls [A-Z] * 6) list all names starting. name of the file starting. profile ,. rhosts ,. histo ry, etc.): $ ls. *

Related Article

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.