Job switching and Offline Management in the front and back of Linux

Source: Internet
Author: User

On the Linux bash shell single terminal interface, we often need to manage or complete multiple jobs at the same time, for example, execute compilation, implement data backup, and execute SQL queries and other tasks. 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. Definition of foreground and background jobs
The front and back-end jobs actually correspond to the front and back-end 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. command & directly allow the job to run in the background
B. [ctrl] + z switch the current job to the background
C. view the background job status by jobs
D. fg % n switch the job n running in the background to the foreground.
D. bg % n run the specified job n in the background.
E. kill % n remove the specified job n
"N" is the job number viewed by the jobs command, 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. Demo background job commands

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 is displayed:
[1] + Done tar-czvf temp.tar.gz tempSYBO2SZ. 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. The execution has started, 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

# The following two jobs are released at the same time, and you can 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
Round robin @ SZDB:/tmp> find/-type f-size + 100000 k

# When you view the current jobs again, three jobs in the stopp status appear in the jobs manager.
Robin @ 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, the corresponding job number, and 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 shows the process information through the pid.
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
Robin @ SZDB: ~> Tty
/Dev/pts/3
Robin @ SZDB: ~> Jobs # at this time, we can see that the jobs command does not return any
Robin @ SZDB: ~> Ps-ef | grep 32707 | grep-v grep # the job can be found only based on the process id.
Robin 32707 32095 0 00:00:00 pts/1 find/-type f-size + 100000

# It can be seen that jobs in the current shell are only visible to the current shell (terminal ).

C. Switch the background job to the foreground (fg command)
Robin @ SZDB:/tmp> fg # When the Job number is omitted, the default job is switched to the foreground
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 @ SZDB:/tmp> jobs
[2]-Stopped find/u02-type f-size + 100000 k
[3] + Stopped find/-type f-size + 100000 k

D. Run the paused job (bg command) on the background)
# 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 to run the specified job.
Robin @ SZDB:/tmp> bg 2 # After inputting bg 2, you can see that the original command is appended &
[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 the 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, and-15 indicates that the specified job is terminated normally. Kill-l lists all signals that can be used by kill.
# For Detailed Help of 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 <EOF
Select to_char (sysdate, 'yyyy-mm-dd hh24: mi: ss') today from dual;
Begin
Dbms_lock.sleep (300 );
End;
/
Select to_char (sysdate, 'yyyy-mm-dd hh24: mi: ss') today from dual;
Exit;
EOF
Exit

# Directly execute the shell script with Parameters

# Author: Robinson
# Blog: http://blog.csdn.net/robinson_0612

Robin @ SZDB :~ /Dba_scripts/custom/bin>./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> # The output is still displayed, but subsequent operations can be continued.
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 below. The two queries in the log are exactly 5 minutes different.
Robin @ SZDB :~ /Dba_scripts/custom/bin> more temp. log

TODAY
-------------------
11:09:24

PL/SQL procedure successfully completed.

TODAY
-------------------
11:14:24

  • 1
  • 2
  • Next Page

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.