How to use crond for second-level execution

Source: Internet
Author: User
When setting the website directory security yesterday, files in a directory need to be cleared once every 3 seconds for security considerations. The first response is solved through cron, crontab seems to support only minutes. what should I do? After some efforts, I finally solved the problem. I 'd like to share some ideas and hope to help... "/> <scripttype =" text/javascript "src =" http: // www. when setting the website directory security yesterday, files in a directory need to be cleared once every 3 seconds for security considerations. The first response is solved through cron, crontab seems to support only minutes. what should I do? After some efforts, I finally solved the problem and shared some methods, hoping to help students with similar needs. Here is a brief introduction to what cron is. Crontab commands are common in Unix and Unix-like operating systems and are used to set periodically executed commands. This command reads commands from the standard input device and stores them in the crontab file for later reading and execution. The following are some parameters and descriptions of this command: crontab-u // sets a user's cron service, generally, when executing this command, the root user needs this parameter crontab-l // to list the details of a user's cron service. crontab-r // delete the cron service crontab-e of no user. // edit a user's cron service # Configuration File Format Description # -- minute (0-59) # | -- hour (0-23) # | -- Day (1-31) # | -- month (1-12) # | -- Week (0-7) (Sunday = 0 or 7) # | # * You can also find that cron can be set to minutes at most through the configuration file. So our demand is every second. how should we solve this problem? Method 1: Of course, the first thing that comes to mind is to write a trigger script and use an endless loop in the trigger script to solve this problem, as shown below: cat kick. sh
#!/bin/bashwhile : ;do        /home/somedir/scripts.sh 2>/dev/null &        sleep 3done
Note: Do not use bash kick. sh & this background running method during the first running. it will be stiff. You can put it into a scheduled task to run it, and then delete this entry from the scheduled task. Finally, put the script in/etc/rc. local so that it can be run every time it is started. The second method is similar to the first method, but it feels more convenient than the first method. Cat cron-seconds.sh
#!/bin/bash#For excuting the scripts every 3 seconds in crond.#20100124.WXGfor((i=1;i<=20;i++));do        /home/somedir/scripts.sh 2>/dev/null &        sleep 3done
Then write the crontab every minute, the following crontab-e ***/bin/bash/home/somedir/cron-seconds.sh third method: so how can we directly implement it using scheduled tasks? The final solution is as follows. after verification, the script runs very stably. (Script author: findingcc) crontab-e
## For excuting scripts.sh every 3 seconds##on 2010-01-22* * * * *  /home/somedir/scripts.sh* * * * * sleep 3 &&  /home/somedir/scripts.sh* * * * * sleep 6 &&  /home/somedir/scripts.sh* * * * * sleep 9 &&  /home/somedir/scripts.sh* * * * * sleep 12 &&  /home/somedir/scripts.sh* * * * * sleep 15 &&  /home/somedir/scripts.sh* * * * * sleep 18 &&  /home/somedir/scripts.sh* * * * * sleep 21 &&  /home/somedir/scripts.sh* * * * * sleep 24 &&  /home/somedir/scripts.sh* * * * * sleep 27 &&  /home/somedir/scripts.sh* * * * * sleep 30 &&  /home/somedir/scripts.sh* * * * * sleep 33 &&  /home/somedir/scripts.sh* * * * * sleep 36 &&  /home/somedir/scripts.sh* * * * * sleep 39 &&  /home/somedir/scripts.sh* * * * * sleep 42 &&  /home/somedir/scripts.sh* * * * * sleep 45 &&  /home/somedir/scripts.sh* * * * * sleep 48 &&  /home/somedir/scripts.sh* * * * * sleep 51 &&  /home/somedir/scripts.sh* * * * * sleep 54 &&  /home/somedir/scripts.sh* * * * * sleep 57 &&  /home/somedir/scripts.sh

 

Sleep 57 &/home/somedir/scripts. sh is a complete command. if we want to clear the files under/home/91ri.org/log/, we can do this: * *** sleep 3 & rm/home/91ri.org/log/#****** sleep 6 & rm/home/91ri.org/log/#****** sleep 9 & rm/home/91ri.org/log /* and so on, I prefer to use the third method, although it is more troublesome. Because the first method and the second method are not executed at a strict interval of 3 seconds, it will be greater than 3 seconds because the script scripts is executed. sh also takes some time, even if the & symbol has been added to the background for execution, there will be some errors. If the accuracy requirement is not high, the second method is recommended. [Appendix] there is another way of writing on the network: *: 10 * rm/home/91ri.org/log/* meaning: execute rm/home/91ri.org/log/* every 10 seconds, but I wonder whether it is a personal issue or the command itself is incorrect. after testing in centos, this is not acceptable. If you are interested, you can test it on your own. For more information about cron, see 《 Use cron for linux rsync + ssh Image BackupFrom: www.91ri.org
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.