I try to setup a cron task to run a set of basic shell commands.

I need to look to any files created on the last day, then copy to another folder and generate a log stating, line by line, the date and the time of the file copy operation.

The two shell commands run separately but need to create one and schedule via cron. When I try to increase the first command (find) cron does not execute the task and gives errors. If I run manually, it works.

find /dir/ -type f -mtime -1 -exec cp -v -a --parents "{}" /dir2/ \; >> /dir2/LogsCopiaDBs_$(date +%d-%m-%Y).txt
exec &> >(while read line; do echo "$(date +'%h %d %Hh%Mm%Ss') $line" >> /dir2/LogsCopiaDBs.txt; done;)

Any idea ?

  • Is there a reason the sequence of commands needs to be in the cron table? This would be easier to do in a readable way in a script which could then be invoked by cron. – DopeGhoti Nov 13 '15 at 18:21
  • I use crontab only to schedule the script execution with these two lines. These two lines are not within the cron. My crontab looks like this: * 2 * * * /backup/scriptdb.sh – Edson S Freitas Nov 16 '15 at 14:12
  • The cron log does not contain any errors. – Edson S Freitas Nov 16 '15 at 15:57
  • You want this to run every minute during the 2AM hour of the day? – DopeGhoti Nov 16 '15 at 18:08
  • DopeGhoti, It is just an example to facilitate testing. – Edson S Freitas Nov 17 '15 at 16:28

The lines should be in a different order:

exec &> >(while read line; do echo "$(date +'%h %d %Hh%Mm%Ss') $line" >> /dir2/LogsCopiaDBs.txt; done;)
find /dir/ -type f -mtime -1 -exec cp -v -a --parents "{}" /dir2/ \; >> /dir2/LogsCopiaDBs_$(date +%d-%m-%Y).txt

The exec line will replace STDOUT with the command specified, and the find line will perform the copy actions and output to STDOUT which is replaced by your logging command.

I think that you want to achieve that you fill two logfiles at once. One with the output of the copy commands, another with the date prefixed. To do that you can use the tee command. Without the tee command the output of the find/cp command is redirected to another logfile and is not captured by the exec command. The tee command solves this:

exec &> >(while read line; do echo "$(date +'%h %d %Hh%Mm%Ss') $line" >> /tmp/LogsCopiaDBs.txt; done;)
find /tmp/ -type f -mtime -1 -exec echo cp -v -a --parents "{}" /tmp/ \; |tee -a /tmp/LogsCopiaDBs_$(date +%d-%m-%Y).txt
| improve this answer | |
  • Sorry, I had not said I tried this mode (first exec then find) and works only when run manually if I schedule via Cron does not. The logs in cron proves that the script was executed but does not occur any copy or log generation. – Edson S Freitas Nov 16 '15 at 14:07
  • Works only manually, via cron does not work and does not generate error. – Edson S Freitas Nov 17 '15 at 11:09

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.