这个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

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的cron与%的更多相关文章

  1. 通过Linux系统Cron执行OwnCloud计划任务

    通过Linux系统Cron执行OwnCloud计划任务 02/02/2013 CRON的确是一个非常有用的功能,它有效减少了系统的负载,在将WordPress和StatusNet的任务计划都转换到Cr ...

  2. linux中Cron定时任务系统命令详解

    分类:Linux VPS教程 作者:阿川 发布时间:October 13, 2011 有很多同学在购买VPS之后,需要用到计划任务.但是又对计划任务不太了解,所以.今天我们的帮助中心主要是给大家提供一 ...

  3. Linux 通过cron定期执行 php文件(转)

    Linux 通过cron定期执行 php文件 补充几点: 1. 要在php文件头加上解释器的路径,通常是 #!/usr/bin/php 2. 授予要执行的php文件执行权限   chmod a+x x ...

  4. Linux Schedule Cron All In One

    Linux Schedule Cron All In One 定时任务 / 定时器 GitHub Actions Scheduled events Cron syntax has five field ...

  5. Linux下cron的使用

    cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...

  6. linux的cron服务及应用

    Linux下的Cron用于定时执行设置的周期性指令,是Linux的内置服务,可以用以下的方法启动.关闭这个服务: /sbin/service crond start //启动服务 /sbin/serv ...

  7. linux操作系统cron详解

    Linux操作系统定时任务系统 Cron 入门 cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动 ...

  8. (转载)Linux定时任务cron配置

    (转载)http://blog.csdn.net/jbgtwang/article/details/7995801 实现linux定时任务有:cron.anacron.at等,这里主要介绍cron服务 ...

  9. PHP结合Linux的cron命令实现定时任务

    PHP死循环 来处理定时任务的效率是很低的.(众多网友评价)大家都建议使用Linux内置的定时任务crontab命令来调用php脚本来实现. PHP定时任务的两种方法:1.web方式调用php网页,但 ...

  10. Linux的cron和crontab

    一 cron crond位于/etc/rc.d/init.d/crond 或 /etc/init.d 或 /etc/rc.d /rc5.d/S90crond,最总引用/var/lock/subsys/ ...

随机推荐

  1. 转载:2.2.3 配置项的注释《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19628.html 如果有一个配置项暂时需要注释掉,那么可以加"#"注释掉这一行配置.例如: #pid       ...

  2. HTTP基础知识1

    HTTP 简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准,它是基于TCP/IP ...

  3. 关于报错stale element reference: element is not attached to the page document处理

    1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...

  4. 关于ie7下display:inline-block;不支持的解决方案。

    摘要: 声明:此文章为转载(点击查看原文),如有侵权24小时内删除.联系QQ:1522025433. 今天码的时候遇到这个问题了. 如果本身是内联元素的,把它的display属性设置设置为inline ...

  5. this和super不能同时出现在构造方法中

    package com.bjpowernode.t02inheritance.c09; /* * 使用super调用父类的构造方法 */public class TestSuper02 { publi ...

  6. WaitForMultipleObjects返回0xffffffff

    DWORD ret; ; HANDLE handle[THREAD_NUM]; ; i < THREAD_NUM; i++) handle[i] = (HANDLE)_beginthreadex ...

  7. 《剑指offer》-统计整数二进制表示中1的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 直观思路就是把二进制表示从右往左统计1的个数.直接想到移位操作来迭代处理.坑点在于负数的移位操作会填充1.有人贴出了逻辑移位 ...

  8. ***LANMP镜像手册(Apache&Nginx)-lanmp-oneinstack

    LANMP镜像手册(Apache&Nginx) Version PHP7.0.26  转自:http://docs.websoft9.com/xdocs/lanmp-oneinstack-im ...

  9. 查看Windows系统里的进程已运行的时间

    搜索 ProcessExplorer ,可以去微软下载它.右键点击项类,selcet conlumns...在 Process Performance 里 选择start time.有了进程的启动时间 ...

  10. [九省联考2018]一双木棋chess

    题解: 水题吧 首先很显然的是状压或者搜索 考虑一下能不能状压吧 这个东西一定是长成三角形的样子的 所以是可以状压的 相邻两位之间有几个0代表他们差几 这样最多会有2n 然后就可以转移了 由于之前对博 ...