How to make Linux timed tasks crontab in seconds

Source: Internet
Author: User

How do I make Linux timed tasks crontab in seconds?

Linux scheduled task crontab min execution Time unit is minutes

If you want to execute in seconds, how should you set it?

Ideas

The normal scenario is to define the tasks to be performed directly in crontab, and now we add a script file in the middle

Customize a script file, loop through the tasks you want to perform, and use sleep in the loop to control the number of seconds in intervals

Add a task in Crontab, set to execute this script every minute

is to use crontab to control the minutes, using the loop within the script to control the seconds


Example

(1) Define target tasks

Write a test task in PHP and write a time message to the file

File path/root/test.php

<?php
File_put_contents ('/root/run.log ', date (' y-m-d h:i:s '). " \ r \ n ", file_append);
?>

(2) Writing scripts to perform tasks

File path/root/test.sh

#!/bin/bash

#间隔的秒数, it's going to take a few seconds.

#不能大于60
step=2

for ((i = 0; i <; i= (I+step))); Do

$ (php '/root/test.php ')
Sleep $step
Done

Exit 0

(3) Adding timed tasks

Crontab-e

* * * * * */root/test.sh

Restart Timed Task Service

Service Crond Restart

(4) View effects

Tail-f/root/run.log

You can see that the information is printed by the second

How to make Linux timed tasks crontab in seconds

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.