Linux cron vs.%

Source: Internet
Author: User
Tags crontab example

This cron cannot be executed: * * * * * */bin/echo '/bin/date + "%y-%m-%d-%t" ' >>/home/adminuser/test.txt 2>&1 need to be modified to: because% is required in cron To be transferred */1 * * * */bin/echo/bin/date + "\%y-\%m-\%d-\%t" >>/home/adminuser/test.txt 2>&1 detect disk and folder usage: */ * * * * echo ' date + ' \%y-\%m-\%d-\%t ' '/home/adminuser '/bin/df-h/home/adminuser | /bin/grep/dev/sda1 | Awk-f ' {print ' free: ' $ $ ' 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 detailed explanation:

The purpose of this document are to explain how to cope with the percentage sign (%) in a crontab entry.

Usually, a used to denote a new line in a crontab entry. The first is special on 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 percent, 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% have been used to indicate a new line.

The problem is about use a% of a crontab line to as a% and not as a new line. Many manuals would say escape it with a \. This certainly stops it interpretation as a new line and 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 is 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 intended.

This technique was very useful when using a MySQL command within a crontab. MySQL command can often has a% in them. Some example is:

    • SET @monyy =date_format (now (), "%M%Y")
    • SELECT * FROM table WHERE name like ' fred% '

So, to has a crontab entry to run the MySQL command

mysql -vv -e "SELECT * FROM table WHERE name LIKE Fred%‘" member_list

Would has to appear in the crontab as

echo "SELECT * FROM table WHERE name is like ' fred\% '" | Sed-e ' s|\\| | G ' | MYSQL-VV member_list

Linux cron vs.%

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.