0

This question already has an answer here:

I set the following line in cron job under /etc/cron.d/find_old_file

* * * * * root  [[ -d /var/log/ambari-metrics-collector ]] && find  /var/log/ambari-metrics-collector   -type f -mtime +10 -regex '.*\.log.*[0-9]$' -printf '%TY %Tb %Td %TH:%TM %P\n' >> /var/log/find_old_file

this cron should find the logs that older then 10 day ( but only if /var/log/ambari-metrics-collector folder exists )

from some unclear reason we notice that /var/log/find_old_file not created

and when we test it this line works fine on shell bash but not from cron

we also add 2>&1 in the end of the file but this not work

please advice what is wrong in my cron job ?

more /etc/cron.d/find_old_file

* * * * * root  [[ -d /var/log/ambari-metrics-collector ]] && find  /var/log/ambari-metrics-collector   -type f -mtime +10 -regex '.*\.log.*[0-9]$' -printf '%TY %Tb %Td %TH:%TM %P\n'   >>    /var/log/find_old_file 2>&1

example when we test it on shell bash

 [[ -d /var/log/ambari-metrics-collector ]] && find  /var/log/ambari-metrics-collector   -type f -mtime +10 -regex '.*\.log.*[0-9]$' -printf '%TY %Tb %Td %TH:%TM %P\n'
2018 Aug 13 12:54 collector-gc.log-201808130951
2018 Aug 13 04:22 collector-gc.log-201808130403
2018 Aug 01 12:40 gc.log-201808011229
2018 Aug 01 12:40 collector-gc.log-201808011229
2018 Aug 09 15:36 gc.log-201808091332
2018 Aug 09 10:50 gc.log-201808090825
2018 Aug 13 04:02 collector-gc.log-201808130346
2018 Aug 13 16:51 gc.log-201808131358
2018 Aug 01 13:35 gc.log-201808011241
2018 Aug 01 13:35 collector-gc.log-201808011241
2018 Aug 09 15:39 collector-gc.log-201808091332
2018 Aug 02 23:06 gc.log-201808022256

when I put this

* * * * * root echo test  >> /var/log/test

then its works , but not my line as described

so what are happens here?

marked as duplicate by Jeff Schaller, αғsнιη, msp9011, Isaac, roaima Aug 30 '18 at 7:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

The % sign in crontab see man (5) crontab has special meaning (newline) and to get your command work you need to escape them.

A "%" character in the
   command, unless escaped with a backslash (\), will be changed into
   newline characters, and all data after the first % will be sent to
   the command as standard input.

so, your command should be:

* * * * * root  [[ -d /var/log/ambari-metrics-collector ]] && find /var/log/ambari-metrics-collector -type f -mtime +10 -regex '.*\.log.*[0-9]$' -printf '\%TY \%Tb \%Td \%TH:\%TM \%P\n' >> /var/log/find_old_file
2

Create an executable shell file

cat /root/bin/find_old_file
    #!/bin/sh
    [[ -d /var/log/ambari-metrics-collector ]] && find  /var/log/ambari-metrics-collector   -type f -mtime +10 -regex '.*\.log.*[0-9]$' -printf '%TY %Tb %Td %TH:%TM %P\n' >> /var/log/find_old_file

which you run via cron

cat /etc/cron.d/find_old_file
    * * * * * root  /root/bin/find_old_file

If a script needs to be reformatted it to suit cron, it is often beneficial move it out of cron.

Not the answer you're looking for? Browse other questions tagged or ask your own question.