Linux perform regular tasks to achieve every second


Linux perform regular tasks to achieve every second


The following is only my work, the learning process into contact with the contents of deficiencies like him to bear.

crontab scheduled task minimum cycle of 1 minute, if the scheduling period script requires less than 1 minute, crontab can not be used directly, but can be implemented to schedule period less than one minute using the following method.
date >> /root/result.txtThe timing for the command execution needed (of course, the statement can be replaced by other scripts, if necessary) with a period of 1 second, achieved in two steps:
1, to achieve 1 minute cycle carried out 60;
2, the crontab regular tasks per minute test.sh execute a script
steps 1 and 2 used in conjunction to achieve the date >> /root/result.txtimplementation of demand once per second command.

Script writing /root/test.sh

This method is applicable to the case of the scheduling period can be divisible 60

#!/bin/bash
step=1
for (( i = 0; i < 60; i = (i+step) )); do
    date >> /root/result.txt
    sleep $step
done

exit 0

Configuration regular tasks

The timing task is configured to be performed once every minute
if not to learn how to add a scheduled task, refer to Linux's command crontab

// 编辑crontab
crontab -e
// 添加定时任务
* * * * * sh /root/test.sh
*/1 * * * * sh /root/test.sh

Achieve results

Through the above steps, you can be realized date >> /root/result.txtcommand once every second, the test results are as follows:
pic

Other knowledge involved

Clear file contents mode

method one:

cat /dev/null > file.name

Second way:
This approach will write a blank line in the file

echo "" > file.name

Three ways: in vi / vim edit mode, using %dempty

vim file.name
:%d
:wq

Appended to the end of the input file

// > 添加内容,会删除文件中的原来内容
date > /root/result.txt
// >> 在原文件末尾追加内容
date >> /root/result.txt

reference

Guess you like

Origin www.cnblogs.com/hai-feng/p/12446126.html