Quote: I have not written a shell script before, perhaps because of laziness, perhaps not forced to write to the point of the shell. However, some time ago, the job requirements, required to rerun a few months of the script, these scripts are timed to run every day, and each day is a number of scripts associated with the run. You might say, it's too simple, write a loop, and then let him run on his own. Yes, you can easily use the programming language to write loops, such as PHP, but, you know, this is actually changed the code structure, the ghost know what will lead to the result? And, I do not guarantee the meaning of all the code inside, I understand! So, the question is, without changing the original code, how to cycle these months of time? Yes, that is, simulating the actual runtime, passing in the date parameters that need to be received (provided that the door is already in your code)! You know, this timing script has an elegant name, crontab, then, the shell, all you have to do is write the shell.
Didn't write a shell? It's okay, actually after the demand is determined, you obviously already know that it's too easy. Isn't that a grammatical problem? You do not tell me you will not Google, will not Baidu!
I'll just leave a few points to consider, and then give the code directly!
1. How do I get the current time and convert it into the format you need? Keywords: date
2. How to prevent multiple runs of the same content at the same time? Key words: Lock
3, how to let the program run out once, cooling execution? Key Xu: Sleep
4, how to specify the running time period, counter or start date? Keywords: while, let, expr
5, attach: How to know the current state of operation? Keywords: echo, progress
Consider these problems, you step by step to write, do not know the grammar, direct Google, Baidu, code reference as follows:
#/bin/bash# @author: youge# @date: -- A- AStartDate="2015-11-24"# When to start Startdatetimestamp=`Date-D"$startDate"+%s ' endDate="2015-12-14"# when to Endenddatetimestamp=`Date-D"$endDate"+%s ' sleeptime= -# to take a breakhavesthundo=0# checkifThere is something undo,ifNot , exit the Programrootdir='/cygdrive/d/wamp/ppi/'dir= $rootDir"cron/"Itemarr=("ItemA" "Itemb" "ITEMC") # The items you want to run there forIteminch${itemarr[@]} DoRunfile= $rootDir $item". Run"; if[!-F"$runFile"] ; ThenHavesthundo=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>>$runFileTouch "$runFile"; Break Else Echo$item"is runing, skipped."$runFile; fi Done;if[$haveSthUndo-ne1]; Then Echo-E"Nothing to does now ... \ncheck it ..."; Exit;fiEcho "havesthundo eq:"$haveSthUndo; while[[$endDateTimestamp-GE $startDateTimestamp]] Dorundate=`Date-D @ $startDateTimestamp +%y-%m-%d '; #1987- on- .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./script1.php $params &&/bin/php./script2.php $params &&/bin/php./scriptx.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++)); DoLet itime2= $itime +1; Progress=`Expr$itime 2 \* -/$sleepTime '; printf"cooling:[%-"$sleepTime"s]%d%%\r"$x $progress x=# $xSleep 1; #Sleepxx seconds to run the nextTime , show the Progress Done; Echo; Done;Echo "[' Date"+%y-%m-%d%h:%m:%s"'] the end.">>$runFile; #end of thefile
Appendix:
According to my initial involvement in the shell, the problems encountered are as follows, hoping to reduce the time for everyone to detour:
1, the entire shell script, in fact, the equivalent of you in the terminal input a series of commands, if you want to do in the shell, first think about what can be done in the terminal, the connection of the word Fu, is directly with "" "double quotes, output, variable definition no $ symbol, but use must be added $ symbol.
2, "=" assignment symbol, there must be no space on both sides, which is different from other languages, especially you also have their own code beautiful style special attention, otherwise you will report grammatical errors!
3. For the array contents are separated by "", not "," comma
4. Conditional judgment [true] a space is required after the brackets, but there is no space between the two brackets, such as [[true]]
5, while conditional judgment can be used () brackets, you can also use [[]] Brackets
6, if you write a shell with Windows, be sure to note the line break format \ r \ n, you need to use some editors (such as notepad++) to change the line character format!
The end: In fact, language is just a tool, never too difficult (too difficult and no one to use AH), really can let the tool work, is your mind!
Actually very simple, Just do it.
sh urscripts. SH Time
Writing your shell is actually very simple.