I thought of using watch command to delete files in a directory every N seconds with the command running in background.

This is what I was planning to use:

nohup watch -n1200 rm -rf /var/log/radius/radacct/* &

It works, but when I see the running process, it shows me the following output -

[root@Free-Radius]# ps -aef | grep watch
root     27326 10649  0 15:23 pts/0    00:00:00 watch -n1200 rm -rf /var/log/radius/radacct/
root     27404 10649  0 15:24 pts/0    00:00:00 grep watch

Now there will be a new file detail-20130817 tomorrow, which will not get deleted. Is there a way to achieve it using watch command. I know its easily doable using a simple bash script or cron job, but just wanted to know if it can be done using watch.

  • 1
    use rather .../detail-$(date +%Y%m%d) – Valentin Bajrami Aug 16 '13 at 10:27
  • 1
    Or configure the application to not write any logs, if you're just going to delete them anyway. – user Aug 16 '13 at 10:45
  • 1
    You should use watch 'rm -rf something*' to avoid the expansion of * by the shell before the execution of watch. – enzotib Aug 16 '13 at 10:52

If using the watch command from procps (which starts a shell to interpret the command line that's made out of the concatenation of its arguments), use:

nohup watch -n1200 'rm -rf /var/log/radius/radacct/*' &

With some other implementations, you may have to call the shell manually:

nohup watch -n1200 sh -c 'rm -rf /var/log/radius/radacct/*' &

Why not use cron though?

| improve this answer | |

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.