Quotation:I have never written shell scripts before, maybe it is because I am lazy, maybe it is not forced to write shells. However, some time ago, work required that scripts that were rerun for several months were run regularly every day, and several scripts were run together every day. You might say, this is too simple, just write a loop and then let him run by himself. Yes, you can easily write loops in your programming language, such as PHP. But, you know, doing this actually changes the code structure. What will happen? Moreover, I don’t guarantee that all the codes in it are meaningful! So, the question is, how to loop over these months without changing the original code? That's right, that'sSimulate the real runtime situation and pass in the date parameters that need to be received(Presumably, this door is already in your code)! You know, this kind of timed script has an elegant name, crontab, then, it is a shell, all you have to do is write a shell.
Haven't written a shell? It doesn’t matter. In fact, after the requirements are determined, you obviously already know that this is too simple. Isn't it just a grammar problem? Don't tell me that you don't know Google or Baidu!
I will throw a few points that need to be considered first, and then give the code directly!
1. How to get the current time and convert it into the format you need? Keywords: date
2. How to prevent the same content from running multiple times at the same time? Keywords: lock
3. How to cool down and execute after the program is run once? Keywords: sleep
4. How to specify the running time period, counter or start date? Keywords: while, let, expr
5. Additional: How to know the current operating status? Keywords: echo, progress
After considering these issues, you can write them step by step. If you don’t know the syntax, just google or Baidu, and refer to the code as follows:
#/bin/bash # @author: youge # @date: 2015-12-22 startDate="2015-11-24" # when to start startDateTimestamp=`date -d "$startDate" +%s` endDate="2015-12-14" # when to end endDateTimestamp=`date -d "$endDate" +%s` sleepTime=25 # to take a break haveSthUndo=0 # check if there is something undo , if not , exit the program rootDir='/cygdrive/d/wamp/ppi/' dir=$rootDir"cron/" itemArr=("itemA" "itemB" "itemC") # the items you want to run there for item in ${itemArr[@]} do runFile=$rootDir$item".run"; if [ ! -f "$runFile" ] ; then haveSthUndo=1; echo $item" runs on me" $runFile " touched"; echo -e "script startTime: "`date "+%Y-%m-%d %H:%M:%S"` "\nbeginDate:" $startDate "\nendDate:" $endDate "\npath:" $dir >> $runFile touch "$runFile"; break; else echo $item "is runing, skipped. " $runFile; fi done; if [ $haveSthUndo -ne 1 ]; then echo -e "Nothing to do now ...\ncheck it..."; exit; fi echo "haveSthUndo eq: " $haveSthUndo; while [[ $endDateTimestamp -ge $startDateTimestamp ]] do runDate=`date -d @$startDateTimestamp +%Y-%m-%d`; #1987-01-06 params="item=$item&d=$runDate"; msg="[`date "+%Y-%m-%d %H:%M:%S"`] now we run the date: "${runDate}" [params]: "${params}; echo $msg; # to show me ... echo $msg >> $runFile; # run the scripts below cd $dir && /bin/php ./ $params && /bin/php ./ $params && /bin/php ./ $params # run the scripts upon startDateTimestamp=`expr $startDateTimestamp + 86400`; # run the next day ... echo " ___sleeping for $sleepTime seconds ... "; x=''; for ((itime=0; itime<$sleepTime; itime++)); do let itime2=$itime+1; progress=`expr $itime2 \* 100 / $sleepTime`; printf "cooling:[%-"$sleepTime"s]%d%%\r" $x $progress x=#$x sleep 1; # sleep xx seconds to run the next time, show the progress done; echo; done; echo "[`date "+%Y-%m-%d %H:%M:%S"`] the end." >> $runFile; #end of the file
appendix:
According to my first shell interview, the following examples of the problems I encountered are: I hope it can reduce the time for everyone to take detours:
1. The entire shell script is actually equivalent to a series of commands you enter in the terminal. If you want to do something in the shell, think about what you can do in the terminal first. The connection of characters is to directly use "" double quotes, output, and variable definitions without the $ symbol, but you must add the $ symbol when using it.
2. "=" Assign symbols, there must be no spaces on both sides. This is different from other languages. Especially when you have your own beautiful code style, you will pay special attention, otherwise you will report syntax errors!
3. The contents of the array in for are separated by " " spaces, not "," comma
4. Conditional judgment [true] brackets There needs to be a space after it, but there cannot be a space between the two brackets such as [[ true ]]
5. You can use () brackets or [[ ]] brackets for while conditions.
6. If you write a shell in Windows, you must pay attention to the newline format \n instead of \r\n. You need to use some editors (such as notepad++) to change the newline format!
End: In fact, language is just a tool, it will never be too difficult (it will not be used by people). What can really make the tools work is your thoughts!
It's actually very simple, Just do it.
[ root @my-pc ]$ sh #see you next time
The above is the detailed content of this article, I hope it will be helpful to everyone's learning.