crontab实现每秒执行

时间:2023-03-09 16:05:24
crontab实现每秒执行

crontab:

#!/bin/bash

step=$   #每多少秒执行一次

for (( i = ; i < ; i++ )); do
date +%Y%m%d' '%H:%M:%S >> s.log
curl https://car.etaiping.com:6004/ecms2/portal/gwzt/carType &> s.log
echo >> s.log
sleep $step
done

log backup:

#!/bin/bash
#author:xiluhua
#since: #####################################################################
# $:the frequency to do log backup
# $:only if the number of log files reach v_count,log files will be backuped
# $:target directory that needs to do log backup
# $:the directory to put bakuped logs
##################################################################### v_step=$
v_count=$
v_targDir=$
v_histDir=$ if [ -z $v_targDir ]
then
echo "exception: argu 'v_targDir' is empty!"
exit
fi if [ -d $v_targDir ]
then
# do nothing
echo > /dev/null
else
echo "exception: argu 'v_targDir' is not exist!"
exit
fi if [ -z $v_histDir ]
then
echo "exception: argu 'v_histDir' is empty!"
exit
fi if [ -d $v_histDir ]
then
# this is the key step to make the .gz file without directory
cd $v_histDir
else
echo "exception: argu 'v_histDir' is not exist!"
exit
fi if [ -z $v_step ]
then
echo "v_step is empty!"
exit
fi if [ -z $v_count ]
then
echo "exception: argu 'v_count' is empty!"
exit
fi let isProcExist=
# recycle endlessly
while [ "" = "" ]
do
# if same job process already exists,sleep sometime
while [ "" = "" ]
do
if [ $isProcExist -gt ]
then
sleep $v_step
else
break
fi
done let isProcExist= files=$(find $v_targDir -name "*.log")
count=$(ls $files | wc -l) #echo "count:"$count
#echo "v_count:"$v_count if [ $count -ge $v_count ]
then
dir=$(date +%Y%m%d'_'%H%M%S)
# give the value to bakDir
bakDir=$dir mkdir $bakDir
mv $files $bakDir
#echo $bakDir'.tar.gz'
tar -Pczf $bakDir'.tar.gz' $bakDir if [ $bakDir = "/" ]
then
clear
else
rm -r $bakDir
fi
fi
sleep $v_step let isProcExist=
done

autoCreateFile:

#!/bin/bash
#author:xiluhua
#since: v_step=$
if [ -z $v_step ]
then
echo "exception: argu 'v_step' is empty!"
exit
fi cd ~/auto for (( i = ; i < ; i=(i+step) )); do
file1=$(date +%Y%m%d'_'%H%M%S)'_1.log'
file2=$(date +%Y%m%d'_'%H%M%S)'_2.log'
file3=$(date +%Y%m%d'_'%H%M%S)'_3.log' touch $file1 $file2 $file3
sleep $v_step
done

batchRename:

#!/bin/bash
#author:xiluhua
#since:
#####################################################################
# $:the dir to do batch file rename
# $:name before rename
# $:name after rename
##################################################################### dir=$
targ=$
repl=$ #check arguments is not empty and valid
if [ -z $dir ]
then
echo "exception: argu 'dir' is empty!"
exit
fi if [ $dir = "/" ]
then
echo "exception: argu 'dir' is invalid!"
exit
fi if [ -d $dir ]
then
cd $dir
else
echo "exception: argu 'dir' is not exist!"
exit
fi if [ -z $targ ]
then
echo "exception: argu 'targ' is empty!"
exit
fi if [ -z $repl ]
then
echo "exception: argu repl is empty!"
exit
fi # begin the main business
for i in $(ls *."$targ");do
# echo $i
tmp=${i/"$targ"/"$repl"}
# echo $tmp
mv -f $i $tmp
done