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
slm
  • 363,520
  • 117
  • 767
  • 871
yael
  • 12,598
  • 51
  • 169
  • 303
  • 1
    It's not really a "line", is it? – Jeff Schaller Aug 09 '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 09 '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?](https://unix.stackexchange.com/questions/390664/what-is-causing-my-unexpected-eof-error-while-looking-for-error) – steeldriver Aug 09 '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 09 '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 09 '18 at 16:55
  • If I saw a "line" like that in a `crontab` file I'd pull it out into a script. I'd use the opportunity to throw in some comments too. Future maintenance... – roaima Aug 09 '18 at 16:59

1 Answers1

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
RalfFriedl
  • 8,816
  • 6
  • 23
  • 34