1

We want to execute the following line each time at 00:00 night

Dose this line is valid in crontab or cron job inspite commands with several lines ?

0 0 * * * find . -type f \( -name '*.wsp' -printf WSP -o -printf OTHER \)     -printf ' %T@ %b :%p\0' |    sort -zk 1,1 -k2,2rn |    gawk -v RS='\0' -v ORS='\0' '
     {du += 512 * $3}
     du > 10 * (2^30) && $1 == "WSP" {
       sub("[^:]*:", ""); print
     }' | xargs -r0  rm -f
  • 1
    It's not really a "line", is it? – Jeff Schaller Aug 9 '18 at 16:39
  • Why not , we can put all this lines in one line but this is long and ugly , so it will be more elegant to put all line as described with cpouple lines that are actually one command – yael Aug 9 '18 at 16:41
  • 1
    Aside from other considerations, cron will treat any unescaped % characters as end-of-line markers - see for example What is causing my “Unexpected EOF Error while looking for …” error? – steeldriver Aug 9 '18 at 16:43
  • 2
    For something like this, I'd be inclined to put it into a script and have cron call that. Is this not a viable solution here for some reason? – DopeGhoti Aug 9 '18 at 16:43
  • I will with you honest with you , we need to create additional script on every machine in the cluster and also cron-job under /etc/cron.d , so its little more work , by the way in case cluster is huge let’s say 80 machines then we need to put the script on each machine , and we want to avoid that and manage only the cron job – yael Aug 9 '18 at 16:55
5

No, your example will not work. You have to write the whole command on a single line. Consider writing a script and just calling the script from cron.

For example:

$ cat mycron.bash
#!/bin/bash
find . -type f \( -name '*.wsp' -printf WSP -o -printf OTHER \) \
     -printf ' %T@ %b :%p\0' | \
     sort -zk 1,1 -k2,2rn | \
     gawk -v RS='\0' -v ORS='\0' '
     {du += 512 * $3}
     du > 10 * (2^30) && $1 == "WSP" {
       sub("[^:]*:", ""); print
     }' | xargs -r0  rm -f

Then your crontab entry would be something like this:

0 0 * * * mycron.bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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