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] 12500robin @ szdb:/tmp> tempsybo2sz. dbfrobin @ 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. dbfrobin @ 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 has started, but it needs to be placed in the background (use [CTRL] + Z) Robin @ s Zdb:/tmp> tar-czvf temp2.tar.gz tempsybo2sz. DBF tempsybo2sz. DBF [1] + stopped tar-czvf temp2.tar.gz tempsybo2sz. dbfrobin @ 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, in the jobs manager, three jobrobin @ szdb:/tmp> jobs [1] stopped tar are displayed. -Czvf temp2.tar.gz tempsybo2sz. DBF [2]-stopped find/-type F-size + 100000 K [3] + stopped find/u02-type F-size + 100000krobin @ 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 the corresponding process information Robin @ szdb: /tmp> PS-Ef | grep 32707 | Grep-V greprobin 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/3robin @ 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] + zrbin @ szdb:/tmp> FG % 1tar-czvf temp2.tar.gz tempsybo2sz. dbfrobin @ szdb:/tmp> jo BS [2]-stopped find/u02-type F-size + 100000 K [3] + stopped find/-type F-size + 100000kd, paused job in the background (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 jobrobin @ 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 + 100000ke, remove a specified job N (kill) robin @ SZD B:/tmp> jobs [3] + stopped find/-type F-size + 100000krobin @ szdb:/tmp> kill-9% 3 # force terminate job 3. Note, here % cannot be omitted Robin @ szdb:/tmp> jobs [3] + killed find/-type F-size + 100000krobin @ 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 # 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/bashsid = $1 sqlplus-s Scott/tiger @ $1 <eofselect to_char (sysdate, 'yyyy-mm-dd hh24: MI: ss') today from dual; begindbms_lock.sleep (300); end;/select to_char (sysdate, 'yyyy-mm-dd hh24: MI: ss') today from dual; exit; eofexit # directly execute the shell script with parameters # Author: Robinson # blog: http://blog.csdn.net/robinson_0612robin@SZDB :~ /Dba_scripts/custom/bin>./echo_time.sh CNMMBOTODAY-------------------2013-05-03 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 cnmmborobin @ szdb :~ /Dba_scripts/custom/bin> kill-9% 1 # force terminate the job [1] + stopped./echo_time.sh cnmmborobin @ szdb :~ /Dba_scripts/custom/bin> jobs # The job has been marked as killed [1] + killed./echo_time.sh cnmmborobin @ 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] 2233robin @ szdb :~ /Dba_scripts/custom/bin> # The output is still displayed, however, you can continue today--2013-05-03 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] 2256robin @ 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. logtoday ------------------- 2013-05-03 11: 09: 24pl/SQL procedure successfully completed. today---2013-05-03 11:14:24

4. Offline Job Management

Switching a job (process) to the background can avoid job interruptions due to misoperations, such as [CTRL] + C, and so on. Offline Management is mainly for abnormal disconnection of the terminal. 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 cnmmbonohup: 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.shrobin 2623 1 0? 00:00:00/bin/bash./echo_time.sh cnmmborobin @ szdb :~ /Dba_scripts/custom/bin> More nohup. out # The output log shows that the job is successfully completed today---2013-05-03 11: 26: 32pl/SQL procedure successfully completed. today--2013-05-03 11:31:32 # The following uses the nohup method and puts the job in the background for processing, if a log file is specified, nohup uses the specified log file instead of the default nohup. outrobin @ szdb :~ /Dba_scripts/custom/bin> nohup./echo_time.sh cnmmbo> temp2.log 2> & 1 & [1] 3019robin @ szdb :~ /Dba_scripts/custom/bin> jobs [1] + running nohup./echo_time.sh cnmmbo> temp2.log 2> & 1 &

More references

For more information about Oracle RAC, see
Use crs_setperm to modify the resource owner and permissions of RAC.
Use crs_profile to manage RAC resource configuration files
RAC database startup and Shutdown
Oracle RAC services
Services in Oracle Database 10g
Migrate datbase from single instance to Oracle RAC
Connect Oracle RAC to a specified instance
Oracle RAC load balancing test (combined with server and client)
Oracle RAC server connection Load Balance)
Load Balance)
Non-Default port listening configuration in Oracle RAC (listener. ora tnsnames. ora)
Oracle RAC Listener Configuration (listener. ora tnsnames. ora)
Configure RAC load balancing and Failover
CRS-1006, CRS-0215 fault case
Installing Oracle 10g RAC Based on Linux (RHEL 5.5)
Use runcluvfy to verify the Oracle RAC installation environment

For more information about the basics and concepts of Oracle network configuration, see:
Configure dynamic service registration for non-default ports
Configure sqlnet. ora to restrict IP Access to Oracle
Configure and manage Oracle listener logs
Set the Oracle listener password (listener)
Configure the Oracle client to connect to the database

For more information about user-managed backup and recovery, see
Oracle cold backup
Oracle Hot Backup
Concept of Oracle backup recovery
Oracle instance recovery
Oracle recovery based on user management
System tablespace management and Backup Recovery
Sysaux tablespace management and recovery
Oracle backup control file recovery (unsing backup controlfile)

For information on RMAN backup recovery and management, see
RMAN overview and architecture
RMAN configuration, Monitoring and Management
Detailed description of RMAN backup
RMAN restoration and recovery
Create and use RMAN catalog
Create RMAN storage script based on catalog
Catalog-based RMAN backup and recovery
RMAN backup path confusion
Use RMAN for recovery from different machine backups (WIN platform)
Use RMAN to migrate a file system database to ASM
Linux RMAN backup shell script
Use RMAN to migrate the database to a different machine

For the Oracle architecture, see
Oracle tablespace and data files
Oracle Password File
Oracle parameter file
Oracle online redo log file)
Oracle Control File)
Oracle archiving logs
Oracle rollback and undo)
Oracle database instance startup and Shutdown Process
Automated Management of Oracle 10g SGA
Oracle instances and Oracle databases (Oracle Architecture)

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.