Search code examples
cronvpscpcd

How can I use a cronjob when another program makes the commands in the cronjob fail?


I have a cron job which cd into a directory and performs actions.

For example:

0 12,00 * * * cd /var/lib/test/0001 && cp *.zip /home/bobby/

However, the program that creates the .zip files in /var/lib/test/0001 changes the directory name every day. So on the second day, the directory is /var/lib/test/0002 and on the third day /var/lib/test/0003 and so on. This model cannot be changed.

Of course, when the directory migrates from 0001 to 0002, the cronjob fails.

Is there a way to use cron to cd into 000* and then 001* and so on so that the cp command will be run? Perhaps there is an alternative way? Thank you.

EDIT MARCH 13:

There is another issue that I am finding hard to solve.

I only want to cp files that are above a certain filesize. I want to copy .zip files to /home/bobby/ which are more than 28,000 bytes. If they are less than 28,000 bytes, then they don't get copied. How would I do this, thanks?

As before, this would happen in /var/lib/test/**** (where **** goes from 0000 to FFFF and increments every day).


Solution

  • You can do this with sample script:

    dir=$(ls -tr1 /var/lib/test|tail -1)
    cd /var/lib/test/$dir && cp *.zip /home/bobby/
    

    ls get the list of files sort by time in reverse order so the last one is the last directory. And then we use it further.