Shell Multi-process instances

Source: Internet
Author: User

Shell Multi-Process basics
    • Code block: With {} as a whole
    • & function: Put the command in the background processing, idle out the current console for other operations
    • Wait command: Waits for the parent process to execute the instruction in the parent process after the child process has finished executing
    • Trap command: Get the control signal and make the processing; Format: Trap Cammands SIGNAL
    • EXEC execution: Create a new process with PID to process, the variables are shared, after executing the exec content, do not go back to the original call
    • Execute by Fork: Create New Process (subprocess) processing, variable can only be passed one way
    • Execute through Source: load to the same process, serial processing in the same console, variables are shared, the source content is executed, then back to the original place and then processed
Multi-process key points
    1. File Descriptor:
      is an index value, which is a map record table for each file that the kernel opens for each process and process. When a program opens an existing file or creates a new file, the kernel returns a file descriptor to the process.
      File and Descriptor relationship: Each file descriptor will have an open file, different file descriptors can correspond to the same open file, the same file can be opened by different processes, or can be opened multiple times by the same process.
      Default descriptor:/PROC/SELF/FD (0,1,2)
      Custom descriptors:
      Available range: Ulimit-n view, and then remove the default 0,1,2

    2. Pipe: Similar to queue
      Characteristics:
      Read and write operations on the pipe should be done at the same time, otherwise the operation will be stranded
      Action by unit of Action
      Anonymous pipe: Common "|", the standard output of the previous command as standard input for the next command
      Well-known pipeline: generally as a task queue, orderly access
      Create a named pipe using the MKFIFO command

3. Pipelines associated with file descriptors

mkfifo  /tmp/$$.fifoexec 5 <>/tmp/$$.fiform -f /tmp/$$.fifo

File descriptor and Pipeline binding: Resolving the features that must exist for the pipeline to read and write
Delete Pipeline: The system calls exec to replace the original process with a new process, but the PID of the process remains the same

Multi-process Instances

MySQL Sub-Library backup

#!/bin/bashhour= ' Date +%h ' day= ' date +%f ' now= ' date +%f_%h ' all_dbs= "no" passwd= "Mysql_pass" sshpass= "Ssh_pass" dbs= " DB1 DB2 db3 dbx dby DBZ "cpu_num= ' cat/proc/cpuinfo |grep processor|wc-l ' p_fifo="/tmp/$$.pipo "Mkfifo $p _fifoexec 111<& gt; $p _fifo #关联文件描述符和管道rm-F $p _fifotrap "exec 111>&-;exec 111<&-;exit 0" 2 for (i=1; i<= $cpu _nu M i++)) do echo >&111 #定义进程队列大小, CPU cores donefunction backup () {if [$all _dbs = = "No"];then for DB in $dbs do read-u111 #从队列中获取一个消息, the number of messages in the queue is reduced by one {dbname= ' echo $db |sed "s/-/@00 2d/g "' ops="--parallel=5--compress-threads=5--databases= $dbname "bpath="/data1/ehr-mysql-backup/${            DAY}/${DB} "Inbackup sleep echo >&111 #添加一个消息到队列中, maintaining the number of messages in the queue (number of processes) } & #将代码块放后台处理 done wait #等待所有后台进程处理完 exec 111>&-#关闭描述符, read  Write separate close exec 111<&-  else ops= "--parallel=5--compress-threads=5" bpath= "/data1/ehr-mysql-backup/${day}" Inbackup fi} function Inbackup () {if [-F ${bpath}/lsn_${last}/xtrabackup_checkpoints];then Echo ' Incremental_basedir found , do a increment backup "/usr/bin/innobackupex-u root-p${passwd}--no-timestamp ${ops}--incremental--incremental _basedir=${bpath}/lsn_${last}/--extra-lsndir=${bpath}/lsn_${now}/${bpath}--stream=xbstream |gzip |sshpass-p "${ Sshpass} "Ssh-p 18122 [email protected]" cat-> ${bpath}/${now}.xbstram.gz "Else echo" Incremental_bas Edir not found,do A full backup "/usr/bin/innobackupex-u root-p${passwd}--no-timestamp ${ops}--extra-lsndir=${b path}/lsn_${now}/${bpath}--stream=tar |gzip |sshpass-p "${sshpass}" Ssh-p 18122 [email protected] "cat-> ${b Path}/${now}.tgz "Fi}case $hour in22) last= ' date +%f-d-yesterday ' find/data1/ehr-mysql-backup/-name ' ${last}* "-type d-exec rm-r"{}" \; day= ' Date +%f-d +1days ' backup;; last= ' Date +%f_%h-d -12hours ' backup;; 14|18) last= ' date +%f_%h-d -4hours ' backup;; *) echo "not backup time";; Esac

Shell Multi-process instances

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.