Shell Development of X Pishu

Source: Internet
Author: User
Tags stop script
Recently, we are busy developing data synchronization for Douban, mainly because the Douban interface is not powerful and the data cannot be captured when the access volume exceeds a certain value. Therefore, we decided to import Douban data to the local server and then use our own data. I have developed the script for importing data. Now the command line call is left. Today I have devoted myself to the shell application. I will write down my thinking process. Prepare the PHP test script to be used (not my online Code ).
<? PHP  If ( $ _ Server ['Argc '] <2 ){  Echo "The call is incorrect! \ N" ;  Echo "Call method :" ;  Echo "Php { $ _ Server ['Argv'] [0]} {rsync_show | rsync_show_by_id 1231123 | rsync_douban} \ n" ;  Exit (-1 );}  Function  Rsync_show (){  Echo "Call program data \ n" ;}  Function Rsync_show_by_id ( $ ID  ){  If ( Empty ( $ ID  )){ Echo "The parameter... \ n is missing" ;  Exit (-1 );}  Echo "Start to import program data \ n Based on the specified ID" ;}  Function  Rsync_douban (){  Echo "Call Douban data layer \ n" ;}  Foreach ( $ _ Server ['Argv'] As   $ K =>$ V  ){  If ( $ K = 0) Continue  ;  If ( $ V = 'Rsync _ show_by_id' ){  Call_user_func ( $ V , $ _ Server ['Argv'] [2 ]);  Break ;}  Else  {  Call_user_func ( $ V  );}}  Exit (0 );

View several call results

Error 1

Error 2

Correct call Method

 

Set a scheduled execution script through crontab-e

 

 

 

Shell script 1. Start programming with shell scripts

Run scripts

 

2. Add a stop script.

 

 

3. Sort Functions

 

There are two major issues with Shell Vulnerability Detection: 1. Do you want to write another method? 2. How do I transmit parameters?

The first problem is simply using the start method. There is no need to write another method! -- Writing one more method is costly.

How to solve the second problem, I suddenly thought that we liked it when installing open-source software, as shown in

So I decided to use a similar method for parameter passing.

For details, refer to the method call below!

Run scripts

 Key Points:

1. function call

Start $2 # This is a function + Parameter

$1 in the function indicates the first parameter!

2. Character Truncation

$}It indicates that the value in the $1 variable starts from 2nd (the subscript starts from 0) subscripts and intercepts 2 characters in length.

Id =1 {1 # -- id =} indicates to delete the substring that has recently matched -- id = from the beginning of $1.

 

 Put in crontab

 

My PHPSource codeConfigure found a good way to output characters.

 Recently, it was found that the script could not be executed in one day. As a result, multiple scripts are executed in the background. The solution is as follows:

 

 Running Effect

 

 

Complete source code
#! /Bin/ Bashwork_dir = "  /Home/www/test/  " Script_name = Index. php  Function  Start (){  If [ "  $1  " ! = "" ] & [$ { 1 : 2 : 2 } = "  ID  " ]  Then  ID =$ { 1 #-- ID = }  Echo   "  Start to import data with ID $ ID  "  Nohup PHP $ work_dir /$ Script_name rsync_show_by_id $ ID & Exit  0      Fi  PID ='PS Aux | Grep $ Script_name | Grep -V Grep | Awk   '  {Print $2}  '  '  If [ "  $ PID  " ! = ""  ]  Then         Echo   "  The script has been started...  "  Exit  0      Fi        Echo   "  Start importing data...  "  Nohup PHP $ work_dir /$ Script_name rsync_show rsync_douban & }  Function  Stop (){  Echo  "  End data import...  "  # Pkill - 9 - F index. php PID =' PS Aux | Grep $ Script_name | Grep -V Grep | Awk   '  {Print $2}  '  ' If [ "  $ PID  " ! = ""  ]  Then          Kill - 9 $ PID &>/dev/ Null      Fi  }  Function  Restart () {stop start}  Case   " $1  "   In  Start) Start $  2  ; Stop) Stop; restart) restart ;; * )  Cat < _ Aceof usage: $  0 {START | stop | Restart} Option configuration $  0 Start -- ID = 1003 _ Aceof ;;  Esac  Exit  0 

 

 

To sum up, this is what I have gained today. I am not very proficient in shell. If you have better suggestions, I hope to inform you of them.

 

Recommendation

 

 

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.