crontab in back quotes and $ () to solve invalid

Problem Description

1. Add a crontab, delete this month, two days before the log

10 02  * * * /bin/find /data/logs/php/$(date  +%Y%m)/ -mtime +2 | xargs rm -rf

 

But look above crontab always fail, then check / var / log / cron log packets

Jul 22 02:02:01  localhost CROND[7699]: (root) CMD (/bin/find /data/logs/php/`date +")

Display $ (date +% Y% m) this problem has, running on the terminal is no problem, then the search baidu

problem causes:

Is considered the crontab% character is a newline, so you have time orders% in the crontab to add \ escape
such

30 * * * * date '+%D' >datefile  

it's wrong

The correct one should be

30 * * * * date '+\%D' >datefile

 

So the above crontab is correct:

10 02  * * * /bin/find /data/logs/php/$(date  +\%Y\%m)/ -mtime +2 | xargs rm -rf

 

Spread

Crontab can not function properly resulting in a common cause

 

Problem and the corresponding solution to
execute permissions problem [1] script
after script written, you want to give the script execution permissions avoid permissions issues

<1> given script execute permissions
chmod + x test1.sh

 

[2] script path problems in crontab Lane
<1> View crontab, your place in the script for errors

Note whether the character error

View scheduled tasks:
crontab the -l

Write a scheduled task:
crontab -e


<2> The error, to see whether the error path

To use the Scheduled Tasks in the full path

E.g:

* / * * * * /App/test/test1.sh. 5

<. 3> problem characters or modified path to the

[3] schedule tasks set problem
in question <1> View scheduled task time is set

If the time crontab setting error will cause the task can not be performed as originally planned time

Meaning scheduled tasks * No. 5 in turn represented by:
timeshare weeks sun and the moon

 


<2> In case of problems, the correct time can be modified

[4] problem head script
<1> look at the head of your script, you see the interpreter set for the script

 

<2> to see whether the current system containing this path is the same as an interpreter or script head

Whereis can be used to view, for example
whereis sh


<3> If a script interpreter path error, the head path script is modified to the current path to the system

Run the script problems [5]
In the Linux system, using crontab execute the script, because the crontab is no environment variable, it can not find the command you use, you need to use the command's full path in order to use the command

For example: using python

0 1 * * * / usr / bin / python /App/test/test1.py

[6] environmental variables
This is the most common problem.
When crontab scheduled tasks are executed, it does not read the environment variables from the user's profile document, it will lead to command execution failed.

<1> the script to read the environment variables

Added to the beginning of your script in the following lines, can also be read by other variables file

#!/bin/bash

Source / etc / Profile
Source ~ / .bash_profile

added
[1] crontab environment variables

If you encounter a situation crontab script execution command is not available, you can also add variables by modifying the / etc / crontab in the PATH

[2] crontab special characters
in crontab,% is used to indicate a line break. Therefore, if you are using% needs to be added in front of the \ carried off righteousness.

E.g. date +% F, in crontab, to write date + \% F

 

Guess you like

Origin www.cnblogs.com/wangyh702/p/11243097.html