Job Switching and offline management in the front and back of Linux

Source: Internet
Author: User
In the single terminal interface of Linuxbashshell for job switching and offline management in the front and backend of Linux, we often need to manage or complete multiple jobs at the same time, such as performing compilation while implementing data backup, and other tasks such as SQL query. All of the above work can be done in... in the Linux bash shell single terminal interface for job switching and offline management in the front and back of Linux, we often need to manage or complete multiple jobs at the same time, such as performing compilation while implementing data backup, and other tasks such as SQL query. All of the above work can be implemented in a bash, that is, in the same terminal window. The following describes how to switch and manage foreground and background jobs in Linux and how to manage jobs offline. 1. the definition of the foreground and background jobs corresponds to the foreground and background processes, so there is a corresponding pid. Here we are collectively referred to as a job. Both foreground and background jobs come from the current shell and are subprograms of the current shell. The following is the definition of the two. Foreground jobs: jobs that can be interacted and controlled by users are called foreground jobs. Background job: a job that runs in the memory. you cannot participate in the interaction or use [ctrl] + c to terminate the job. you can only use bg or fg to call the job. 2. several common job commands a and command & directly let the job run in the background B, [ctrl] + z switch the current job to the background c, jobs view the background job status d, fg % n switch the Job n running in the background to the foreground. d and bg % n. run e and kill % n in the background to remove the specified job n "n" as the jobs command. the job ID, not the process id. Each job has a corresponding job number, which is allocated from 1 on the current terminal. The format of the job number is [n], followed by "+" or "-", or nothing. "+" Indicates the most recent job, and "-" indicates the second to last executed Job. Note: "+" and "-" dynamically change as the job is completed or added. Jobs are managed by jobs. jobs on the current terminal are invisible to other terminals. 3. demonstrate the background job command [python] a. directly add the job to the background (append & symbol) robin @ SZDB:/tmp> tar-czvf temp.tar.gz tempSYBO2SZ. dbf & [1] 12500 robin @ SZDB:/tmp> tempSYBO2SZ. dbf robin @ SZDB:/tmp> # Other operations can be performed at this time. after the job is completed, the following prompt [1] + Done tar-czvf temp.tar.gz tempSYBO2SZ is displayed. dbf robin @ SZDB:/tmp> ls-hltr temp *-rwxr-xr-x 1 robin oinstall 490 M tempSYBO2SZ. dbf-rw-r -- 1 robin oinstall 174 M temp.tar.gz B. Execution started Line, but it needs to be placed in the background (use [ctrl] + z) robin @ SZDB:/tmp> tar-czvf temp2.tar.gz tempSYBO2SZ. dbf tempSYBO2SZ. dbf [1] + Stopped tar-czvf temp2.tar.gz tempSYBO2SZ. dbf robin @ SZDB:/tmp> jobs [1] + Stopped tar-czvf temp2.tar.gz tempSYBO2SZ. dbf # release two jobs at the same time, and press [ctrl] + z in the middle to submit the current job to the background round robin @ SZDB: /tmp> find/u02-type f-size + 100000 k robin @ SZDB:/tmp> find/-type f-size + 100000 k # When you view the current jobs again, three jobs in the stopp status are displayed in the jobs manager. Bin @ SZDB:/tmp> jobs [1] Stopped tar-czvf temp2.tar.gz tempSYBO2SZ. dbf [2]-Stopped find/-type f-size + 100000 k [3] + Stopped find/u02-type f-size + 100000 k robin @ SZDB: /tmp> jobs-l # use the-l parameter to view all jobs in the current shell and the corresponding job number. The process pid [1] 32682 Stopped tar-czvf temp2.tar.gz tempSYBO2SZ. dbf [2]-32687 Stopped find/u02-type f-size + 100000 k [3] + 32707 Stopped find/-type f-size + 100000 k # The following can be viewed through pid to Corresponding process information robin @ SZDB: /tmp> ps-ef | grep 32707 | grep-v grep robin 32707 32095 0 00:00:00 pts/1 find/-type f-size + 100000 robin @ SZDB: /tmp> tty # The current terminal information is pts/1/dev/pts/1 # open another terminal, round @ SZDB: ~> Tty/dev/pts/3 robin @ SZDB: ~> Jobs # Now we can see that the jobs command does not return any round robin @ SZDB: ~> Ps-ef | grep 32707 | grep-v grep # The job robin 32707 32095 0 00:00:00 pts/1 find/-type f-size + 100000 can be found only based on the process id # we can see from the above, for jobs in the current shell, only the current shell (terminal) can see c, switch the background Job to the foreground (fg command) robin @ SZDB:/tmp> fg # omit the Job number, switch the default job to the front-end find/-type f-size + 100000 k/u02/database/old/CNMMBOBK/undo/undotbsCNMMBOBK. dbf ...... [ctrl] + z robin @ SZDB:/tmp> fg % 1 tar-czvf temp2.tar.gz tempSYBO2SZ. dbf robin @ SZD B: /tmp> jobs [2]-Stopped find/u02-type f-size + 100000 k [3] + Stopped find/-type f-size + 100000 k d, running background paused job (bg command) # The first two jobs are in the stopped state. now let them run in the background. directly enter the bg command to continue running the default job. otherwise, enter the job number, run the specified job robin @ SZDB:/tmp> bg 2 # After inputting bg 2, we can see that the original command is appended with & [2]-find/u02-type f-size + 100000 k & robin @ SZDB: /tmp> jobs [2]-Running find/u02-type f-size + 100000 k & [3] + Stopped find/-type f-size + 100000 k e, remove specified Job n (kill) robin @ SZDB:/tmp> jobs [3] + Stopped find/-type f-size + 100000 k robin @ SZDB: /tmp> kill-9% 3 # force terminate job 3. Note that the % here cannot be omitted robin @ SZDB: /tmp> jobs [3] + Killed find/-type f-size + 100000 k robin @ SZDB:/tmp> jobs # kill-9 indicates that the specified Job is forcibly terminated, -15 indicates that the specified job is terminated normally. Kill-l lists all signals that can be used by kill # detailed help for the preceding commands, use man command to obtain help information f. background processing of shell scripts with parameters # The following is a shell script for testing robin @ SZDB :~ /Dba_scripts/custom/bin> more echo_time.sh #! /Bin/bash SID = $1 sqlplus-S scott/tiger @ $1 < ./Echo_time.sh cnmmbo today ----------------- 11:07:48 [1] + Stopped./echo_time.sh CNMMBO # Press [ctrl] + z to switch it to the background robin @ SZDB :~ /Dba_scripts/custom/bin> jobs [1] + Stopped./echo_time.sh CNMMBO robin @ SZDB :~ /Dba_scripts/custom/bin> kill-9% 1 # force terminate the job [1] + Stopped./echo_time.sh CNMMBO robin @ SZDB :~ /Dba_scripts/custom/bin> jobs # The job has been marked as killed [1] + Killed./echo_time.sh CNMMBO robin @ SZDB :~ /Dba_scripts/custom/bin>./echo_time.sh CNMMBO & # add the shell script parameters and the & symbol to put the job in the background [1] 2233 robin @ SZDB :~ /Dba_scripts/custom/bin> # you can still see the output at this time, but you can continue to operate TODAY ----------------- 11:08:25 robin @ SZDB :~ /Dba_scripts/custom/bin> jobs [1] + Running./echo_time.sh CNMMBO & robin @ SZDB :~ /Dba_scripts/custom/bin>. /echo_time.sh CNMMBO> temp. log 2> & 1 & # The best way is to directly output it to the log file [2] 2256 robin @ SZDB :~ /Dba_scripts/custom/bin> jobs [1]-Running. /echo_time.sh CNMMBO & [2] + Running. /echo_time.sh CNMMBO> temp. log 2> & 1 & # view the log. The two queries in the log are exactly 5 minutes different. round @ SZDB :~ /Dba_scripts/custom/bin> more temp. log TODAY ------------------- 2013-05-03 11:09:24 PL/SQL procedure successfully completed. TODAY ------------------- 11:14:24 4. offline job management [python] switches the job (process) to the background to avoid job interruptions due to misoperations such as [ctrl] + c, the offline management is mainly for abnormal disconnection of terminals. Generally, the nohup command is used to enable the Job to continue running after it is offline or logged out. That is to say, nohup ignores all SIGHUP signals. If the & symbol is not specified after the command in this mode, the job is located at the foreground. if the & symbol is specified, the job is located at the background. # The following example uses nohup to omit log output, because the output of the original job is automatically redirected to the default nohup. out log file robin @ SZDB :~ /Dba_scripts/custom/bin> nohup./echo_time.sh CNMMBO nohup: appending output to 'nohup. out' # disconnect the terminal directly and reconnect to a new terminal window robin @ SZDB :~ /Dba_scripts/custom/bin> jobs # as a new terminal, jobs cannot see any job robin @ SZDB :~ /Dba_scripts/custom/bin> ps-ef | grep echo_time.sh robin 2623 1 0? 00:00:00/bin/bash./echo_time.sh CNMMBO robin @ SZDB :~ /Dba_scripts/custom/bin> more nohup. out # The output log shows that the job is successfully completed TODAY ----------------- 2013-05-03 11:26:32 PL/SQL procedure successfully completed. TODAY ------------------- 11:31:32 # The following uses the nohup method and puts the Job in the background for processing. if the log file is specified, the nohup uses the specified log file instead of the default nohup. out robin @ SZDB :~ /Dba_scripts/custom/bin> nohup./echo_time.sh CNMMBO> temp2.log 2> & 1 & [1] 3019 robin @ SZDB :~ /Dba_scripts/custom/bin> jobs [1] + Running nohup./echo_time.sh CNMMBO> temp2.log 2> & 1 &
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.