Linux的cron与%
2016-02-19 13:22
447 查看
这个cron不能执行:
* * * * * /bin/echo `/bin/date +"%Y-%m-%d-%T"` >> /home/adminuser/test.txt 2>&1
需要修改为: 因为%在cron中需要被转意
*/1 * * * * /bin/echo /bin/date +"\%Y-\%m-\%d-\%T" >> /home/adminuser/test.txt 2>&1
检测disk和folder的使用率:
*/15 * * * * echo `date +"\%Y-\%m-\%d-\%T"` /home/adminuser `/bin/df -h /home/adminuser | /bin/grep /dev/sda1 | awk -F' ' '{print "free:"$3" usage:"$4}'` >> /home/adminuser/df.txt
*/30 * * * * echo `date +"\%Y-\%m-\%d-\%T"` `/usr/bin/du -h --max-depth=0 /home/adminuser` `date +"\%Y-\%m-\%d-\%T"` >> /home/adminuser/du.txt
详细解释:
The purpose of this document is to explain how to cope with the percentage sign (%) in a crontab entry.
Usually, a % is used to denote a new line in a crontab entry. The first % is special in that it denotes the start of STDIN for the crontab entry's command. A trivial example is:
* * * * * cat - % another minute has passed
This would output the text
After the first %, all other %s in a crontab entry indicate a new line. So a slightly different trivial example is:
* * * * * cat - % another % minute % has % passed
This would output the text
Note how the % has been used to indicate a new line.
The problem is how to use a % in a crontab line to as a % and not as a new line. Many manuals will say escape it with a \. This certainly stops its interpretation as a new line but the shell running the cron job can leave the \ in. For example:
* * * * * echo '\% another \% minute \% has \% passed'
would output the text
Clearly, not what was intended.
A solution is to pass the text through sed. The crontab example now becomes:
* * * * * echo '\% another \% minute \% has \% passed'| sed -e 's|\\||g'
This would output the text
which is what was intended.
This technique is very useful when using a MySQL command within a crontab. MySQL command can often have a % in them. Some example are:
SET @monyy=DATE_FORMAT(NOW(),"%M %Y")
SELECT * FROM table WHERE name LIKE 'fred%'
So, to have a crontab entry to run the MySQL command
mysql -vv -e "SELECT * FROM table WHERE name LIKE Fred%'" member_list
would have to appear in the crontab as
echo "SELECT * FROM table WHERE name LIKE 'Fred\%'" | sed -e 's|\\||g' | mysql -vv member_list
* * * * * /bin/echo `/bin/date +"%Y-%m-%d-%T"` >> /home/adminuser/test.txt 2>&1
需要修改为: 因为%在cron中需要被转意
*/1 * * * * /bin/echo /bin/date +"\%Y-\%m-\%d-\%T" >> /home/adminuser/test.txt 2>&1
检测disk和folder的使用率:
*/15 * * * * echo `date +"\%Y-\%m-\%d-\%T"` /home/adminuser `/bin/df -h /home/adminuser | /bin/grep /dev/sda1 | awk -F' ' '{print "free:"$3" usage:"$4}'` >> /home/adminuser/df.txt
*/30 * * * * echo `date +"\%Y-\%m-\%d-\%T"` `/usr/bin/du -h --max-depth=0 /home/adminuser` `date +"\%Y-\%m-\%d-\%T"` >> /home/adminuser/du.txt
详细解释:
The purpose of this document is to explain how to cope with the percentage sign (%) in a crontab entry.
Usually, a % is used to denote a new line in a crontab entry. The first % is special in that it denotes the start of STDIN for the crontab entry's command. A trivial example is:
* * * * * cat - % another minute has passed
This would output the text
another minute has passed
After the first %, all other %s in a crontab entry indicate a new line. So a slightly different trivial example is:
* * * * * cat - % another % minute % has % passed
This would output the text
another minute has passed
Note how the % has been used to indicate a new line.
The problem is how to use a % in a crontab line to as a % and not as a new line. Many manuals will say escape it with a \. This certainly stops its interpretation as a new line but the shell running the cron job can leave the \ in. For example:
* * * * * echo '\% another \% minute \% has \% passed'
would output the text
\% another \% minute \% has \% passed
Clearly, not what was intended.
A solution is to pass the text through sed. The crontab example now becomes:
* * * * * echo '\% another \% minute \% has \% passed'| sed -e 's|\\||g'
This would output the text
% another % minute % has % passed
which is what was intended.
This technique is very useful when using a MySQL command within a crontab. MySQL command can often have a % in them. Some example are:
SET @monyy=DATE_FORMAT(NOW(),"%M %Y")
SELECT * FROM table WHERE name LIKE 'fred%'
So, to have a crontab entry to run the MySQL command
mysql -vv -e "SELECT * FROM table WHERE name LIKE Fred%'" member_list
would have to appear in the crontab as
echo "SELECT * FROM table WHERE name LIKE 'Fred\%'" | sed -e 's|\\||g' | mysql -vv member_list
相关文章推荐
- Linux流量监控工具 - iftop (最全面的iftop教程)
- linux设备模型-kobject
- UBOOT引导Linux内核及向内核传递参数的方式
- 在linux字符界面中如何查看网页
- OProfile 是Linux内核支持的一种性能分析机制
- Linux查看物理CPU个数、核数、逻辑CPU个数
- Linux 基金会宣布微内核项目 Zephyr
- 祭----------------一次查杀linux木马的经历
- CentOS取消屏幕保护自动锁屏功能
- CentOS提示::unknown filesystem type 'ntfs'.解决
- 服务器重启后没有启动网卡
- linux系统调用和库函数调用的区别
- Linux 更改默认安装路径
- Linux下搭建xmpp ejabberd 服务器
- centos6 搭建ELK
- linux进程时间定时任务命令
- linux下maven的安装
- 【转载】Linux性能评测工具之一:gprof篇
- linux基础
- linux64位系统中g++4.4.7下使用wcsncpy函数有造成段错误的BUG(其它g++版本号未知)