脚本添加Linux定时任务

#!/bin/bash
#./addcrontab "00 18 * * *"  或者 ./addcrontab 00 18 "*" "*" "*"
if [ "$#" -eq "1" ];then
  t0=$1
  str="$t0"
elif [ "$#" -eq "5" ];then
  t1=$1
  t2=$2
  t3=$3
  t4=$4
  t5=$5
  str="$t1 $t2 $t3 $t4 $t5"
else
 echo "出错了"
 exit 1
fi
myscript=sayhi.sh  
filePath=$(cd "$(dirname "$0")";pwd)  
crontab -l > cron  
number=`grep -n "$myscript" cron | cut -d ":" -f 1`  
aa=$number  
#echo $aa  
if [ -z $aa ];  
then  
echo "该任务不存在,将添加"  
echo "$str "$filePath'/'$myscript'' >> cron  
crontab cron  
rm -f cron  
else  
echo "该任务已经存在,将会先删除再添加"  
sed -i '/'$myscript'/d' cron  
echo "$str "$filePath'/'$myscript'' >> cron  
crontab cron  
rm -f cron  
fi

打算用Java调用调用脚本,前台配置定时的Linux任务,留做记录

来源 http://blog.csdn.net/javaweiming/article/details/8722525

 

改一下,生成带日期重定向日志:echo "$str flock -xn /tmp/auto$key.lock -c '"$filePath'/'$myscript''" >> /opt/dingshi/automation/log/runtime-$key-$""(date +\%Y-\%m-\%d-\%H.\%M.\%S).log 2>&1'" >> cron 

 

posted on 2017-05-17 10:19  云卷云舒灬  阅读(192)  评论(0)    收藏  举报

导航