Using shell scripts to implement the Scheduled Tasks feature V1.2

Source: Internet
Author: User

2013.05.10 MyTask 1.2

The main program sleep time is divided into several small fragments of time to sleep, to avoid prolonged hibernation can not respond to the system signal in time.

2013.05.07 MyTask 1.1

After releasing the mytask1.0 version yesterday (using shell scripts to implement the Scheduled Tasks feature), I was thinking about how to solve the last remaining problem "the function name declared in each task script must be unique and cannot be duplicated, otherwise it will cause the task function to overwrite." "Inadvertently asked yourself why can't you just call scripts and commands like crontab?" this asks to recall, the reason is to use Function encapsulation task because the earlier version of the task and the main program is written in a script, and later changed the task from the main program to separate out, But the design that writes to function has not changed, unreasonable design also unconsciously become reasonable.

When the problem is clear, begin to readjust the program, first write all the task code directly into each script, cancel the function wrapper. Then the task initialization declaration is stripped out to form a separate task profile, so that the coupling between programs is reduced and the structure is much clearer. Finally, the function of dynamically overloading the task configuration file is added. Make the MyTask program more convenient to use.

First, MyTask realization function:

1, multi-task concurrent execution, does not affect each other, using lock mechanism to avoid overlapping execution of individual tasks.

2. Each task is saved in a separate script, independent of each other.

3. Support Start run time, such as "2013/05/08", "13:30" or "now". and support to the start run time correction value, such as "now+5m" for the current time of 5 minutes after the execution (also implemented a negative correction value, such as -1H, now think this function is very boring).

4, support various types of operation cycle settings, including seconds, minutes, hours, days, weeks, months, years and a one-time task.

5, according to the task execution interval, automatically set the sleep time, the main program occupies minimal resources.

6. The task can be a shell script or a direct Linux command.

7. Separate task profile, centrally define task content, start time and run interval. (Personal feeling is more intuitive than crontab setting)

8. Support the runtime to reload the task configuration file, you can add the adjusted task to the plan without restarting the main program.

Ii. Description of the task configuration file:

Default task configuration file mytask.conf under Main program directory

Each line configuration corresponds to a scheduled task, and the configuration items are separated by commas, and the task configuration is defined as follows:
< task content >,< start run time >,< run cycle >

1, the task content, can be script files, commands and so on.
Note that you can use the global variable $basedir to represent the directory where the main program is written, with the full file or command path.
Complex command suggestions are saved in the script file.

2. The starting run time is divided into two parts.
The first part is the initial time, the format "Yyyy/mm/dd Hh:mm:ss" can also be a time value fragment, for example: "2013/03/05", "03/05", "03/05 21:30", "21:30" or the current time "now".
The second part is the correction time, the format is "+ Time unit" or "-time unit", meaning to make a further time revision on the basis of the initial time.
For example: "+5s", " -10m" and so on.
The units of time are case-sensitive and are defined as follows:
Y= year, m= month, d= Day, h= time, m= minute, s= second, w= week

3, the Operation cycle is the task function run interval time, the value is similar to the correction time, just cancel the +-number, if the value is not with the unit of 0 means only run once.

The task definition example is as follows:
#在凌晨零点开始执行, run every 1 days.
$BASEDIR/tasks/backdb.sh,00:00,1d

#在当前时间的2分钟后开始执行, run every 1 months.
$BASEDIR/tasks/test1.sh,now+2m,1m

#在5月12日14点30开始执行, run every 1 hours.
/HOME/LYKYL/SP.SH,5/12 14:30,1h

#在15: Execution starts at 50 and runs every 30 seconds.
$BASEDIR/tasks/test3.sh,15:50,30s

#启动即开始执行, run every 1 minutes.
touch/tmp/ohyeah,now,1m

#在当前时间的1小时后开始执行, run only once.
$BASEDIR/tasks/test5.sh,now+1h,0

III. Command of program control

1. Start

Nohup./mytask.sh &

2. Exit

Kill-15 ' Cat./mytask.pid '

3. Reload the task configuration file at run time

Kill-sigusr1 ' Cat./mytask.pid '

Four, off-topic

The program operating principle and 1.0 version of the Basic Agreement, in addition to the source code also added the necessary annotations, so there is no need to repeat the list here. If you are interested, you can click to browse the previous article, "Using shell scripts to implement scheduled Tasks", including development background, ideas and main code.

This program is written because of work needs, and is now in a stable working environment, the code itself is not complex, the shell just the beginning of the friend can be as practiced hand himself try to wildest run the program side to understand the code. Grammatical questions, refer to my other blog post, "A Concise Handbook of Linux Bash script."

mytask1.2 full source point this download

Using shell scripts to implement the Scheduled Tasks feature V1.2

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.