Php或者Shell 脚本修改crontab实例

jackxiang 2011-3-10 12:06 | |
在linux上的crontab文件中我已有一行,如10 * * * * root /var/www/html/test.php
现在我要通过编写php程序来修改10 * * * * root /var/www/html/test.php
变成22 10 * * * root /var/www/html/aa.php
问题解决了,不过有点傻瓜,不知谁还有什么更好的方法吗?若有,希贴上来,互相学习
问题解决了,不过有点傻瓜,不知谁还有什么更好的方法吗?若有,希贴上来,互相学习
$fp=fopen("/etc/crontab","rb");
$save=fopen("/home/mysql/crontab","wb");
$i=0;
/*foreach ($f as $v)
{
  $t = preg_replace('13 15 * * * root /usr/bin/php /var/www/html/mysqlbackup/testClient.php', '* * *', $v);
  $result[] = $t;
}*/
while (!feof($fp)) {
  $buffer = fgets($fp);
  fwrite($save,$buffer);
  $i=$i+1;
  if ($i==10){
  fwrite($save,"success");
  break;
  }
  }
fclose($fp);
fclose($save);
$fp=fopen("/etc/crontab","wb");
$save=fopen("/home/mysql/crontab","rb");
$i=0;
while (!feof($save)) {
  $buffer = fgets($save);
  fwrite($fp,$buffer);  
  }
fclose($fp);
fclose($save);


Shell修改Crontab的方法:
脚本一:
#!/bin/bash
#cron_file='/home/zary/b';
cron_tmp='/home/zary/d';
cronpath="/home/zary/b";
cronbak='/home/zary/f';
parameter=;
if [ "$parameter" = "change" ]
then
cp $cronpath $cronbak;
sed -e '/c_check_mysqlstatus\.sh/s/^#//' -e '/c_check_webstatus\.sh/s/^#//' \
-e '/c_syscheck\.sh/s/#//' -e '/c_syscheck\.sh/s/^11/#11/' $cronpath >$cron_tmp;
cp $cron_tmp $cronpath;
crontab $cronpath;
elif [ "$parameter" = "resume" ]
then
sed -e '/c_check_webstatus\.sh/s/^/#/' -e '/c_check_mysqlstatus\.sh/s/^/#/' \
-e '/c_syscheck\.sh/s/^#//' -e '/c_syscheck\.sh/s/^\*\/20/#\*\/20/' $cronpath >$cron_tmp;
cp $cron_tmp $cronpath;
crontab $cronpath;
else
echo "Usage: c_startcron.sh [change/resume]";
fi


脚本二:

#!/bin/bash
cronpath="/var/spool/cron/root";
if [ = 'tw' ]
then
kick="tw_mail.php";
elif [ = "nw" ]
then
kick="nw_mail.php";
elif [ = "pw" ]
then
kick="pw_mail.php";
fi
crontab -l|egrep -v "$kick">/tmp/crontab;
case "" in
daily)
date=" * * *";
;
weekly)
date=" * * 1";
;
monthly)
date=" * 1 *";
;
esac
cat /tmp/crontab > $cronpath;
if [ = "on" ]
then
echo "$date /usr/local/php5/bin/php /home/htdocs/pukicn/new/OM/Cron/$kick">>$cronpath;
fi

来源:
http://topic.csdn.net/u/20090918/14/29cb3abf-6de6-4cc6-a28b-26f234b2a87b.html
http://www.linuxdiyf.com/viewarticle.php?id=14751

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/4098/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]