We may encounter scenarios in which a program on a Linux server must be timed out on every weekday. So is there a way to implement this function? The answer is yes. We can use shell scripts to simply implement this function.
The rationale is simple: the shell script queries the current time, date, and day of the week every 1 seconds, when the current week is detected week not 0 or 6 (0 for Sunday, 6 for Saturday) and the time Curtime is greater than the specified time StartTime, executes the program specified. To ensure that the program is specified only once a day, the variable isfirsttime is also referenced. The specific code is as follows:
1#!/bin/SH2 3 #Section Configuration (config section)4#Task Time, Example:203000(Time -: -:xx);190000(Time +:xx:xx);5Starttime=1132006 #the programs you want to execute (program to execute)7program=PS8 9 #Section Promgram (Program execution section)Tenperdate=$ (Date "+%y%m%d") Oneisnewday=1 AIsfirsttime=1 - - Echo 'Task Schedule Time: ('$startTime') Program: ('$program') Waiting ...' the - while true; Do -curtime=$ (Date "+%h%m%s") -curdate=$ (Date "+%y%m%d") + - #Check Week day (not performed on weekends) +Week= 'Date+%W` A if[$week-eq6] || [$week-eq0]; Then atisnewday=0 - Sleep 1 - Continue - - Else - #check and Run script (Business day execution) in if["$isNewDay"-eq"1"]; Then - if["$curTime"-gt"$startTime"]; Then to if["$isFirstTime"-eq"0"]; Then + Echo 'The program ('$program') Running ...' - $program the Echo 'The program ('$program') Stopped ...' * fi $isnewday=0Panax Notoginseng Else - if["$isFirstTime"-eq"1"]; Then the Echo 'New Day: ('$curDate') Task Schedule time: ('$startTime') Waiting ...' +Isfirsttime=0 A fi the + fi - Else $ #new Day Start (Start a new one) $ if["$curDate"-gt"$perDate"]; Then - Echo 'New Day: ('$curDate') Task Schedule time: ('$startTime') Waiting ...' -isnewday=1 thePerdate=$curDate - fiWuyi fi the Sleep 1 - fi Wu Done
The shell script function performs the PS command once every weekday 11:32, as shown in the effect.
Shell scripts implement scheduled execution of specified programs every weekday
Of course, the script is just to demonstrate this timing principle, the actual application can specify other programs or scripts, and use the nohup command to let it run in the background.
Original address: http://ju.outofmemory.cn/entry/2058
Shell scripts implement scheduled execution of specified programs every weekday