Operating System - HP-UX
1833870 Members
1585 Online
110063 Solutions
New Discussion

cron based find command problems

 
Chris Wilshaw
Honored Contributor

cron based find command problems

OK, Here's one to challenge the old braincells.

I have a script running from cron, on an L3000 (dual CPU) under HP-UX 11.11, which executes the following code

cd $ARCHIVEDIR
for file in `find . -name "*.dbf" -mtime +1 -print`
do
echo "Processing file $file"
/usr/contrib/bin/gzip $file
done

For some reason, the find command does not bring back any files.

Looking in a log, with debugging enabled, I get

+ cd /oradata1/archive/SCP01
+ find . -name *.dbf -mtime +1 -print

so it's resolving the value of $ARCHIVEDIR correctly.

Running the command manually, I get files listed.

./1_1431.dbf
./1_1432.dbf
./1_1433.dbf
./1_1434.dbf
./1_1435.dbf
./1_1436.dbf
./1_1437.dbf
./1_1438.dbf
./1_1439.dbf
./1_1440.dbf
./1_1441.dbf
./1_1442.dbf
./1_1443.dbf

Initially, I was using find . -name "*.dbf" -mtime +1 -exec /usr/contrib/bin/gzip {} \;

but that was also not working
( I switched to the loop to try to get to the bottom of the issue)

Trying the find without the -mtime option (not gzipping this time as I don't want to risk damaging my current files) I get the files displayed - aha you may think, something wrong with the mtime option, however, when using -mtime +1 without giving a filename, the files that have already been gzipped in that directory are displayed.

The files are not in use by another process (at least fuser reports nothing) and can be manually zipped with no errors.

So what can it be that causes find to work correctly with either a name, or an mtime option with no problems, but to only partially work when both are specified?
13 REPLIES 13
Chris Wilshaw
Honored Contributor

Re: cron based find command problems

I've tried using both /usr/bin/sh and /usr/bin/ksh to no avail, and I've deleted and re-typed the line several times in vi (using :set list too) to make sure that I've not inadvertantly added any control characters
David_246
Trusted Contributor

Re: cron based find command problems

Hi,

I have done the following :

#!/usr/bin/ksh

cd /tmp
for i in `find . -name \*.dbf -mtime +1 -print`
do
echo "Hello $i"
done

created two files 1.dbf and 2.dbf. Nothing got printed until I removed the mtime option. So everything works fine.
I must strongly advise to not use any variables in a cron driven script. These vars should be known my the executor from cron. Please use a realdir-name instead of a var. It should definitly work than.

Regs David

B.T.W. know that you are displaying files older than 1 day. I asume the files are older than one day ? :)
@yourservice
Ian Dennison_1
Honored Contributor

Re: cron based find command problems

You probably do not want the search string in double quotes.

This will evaluate the search template "*.dbf". What I do in this case is twofold,...

No quotes around the search criteria
Escape Char (\) before every occurence of a search character like $*[]() and .

Share and Enjoy! Ian
Building a dumber user
Pete Randall
Outstanding Contributor

Re: cron based find command problems

Chris,

I suspect it's the old cron's environment answer. Your $ARCHIVEDIR is not set. Either source an environment script when setting up cron jobs, or use full path names and explicitly set any variables.


Pete

Pete
Jean-Louis Phelix
Honored Contributor

Re: cron based find command problems

Hi,

I tried it on my system with -name AND -mtime +1 and I can't make it fail ... I even tried from cron and it still works. I also looked for a patch but nothing concerns your problem.

Do you really launch exacly this script from your crontab ?

Regards.
It works for me (© Bill McNAMARA ...)
Pete Randall
Outstanding Contributor

Re: cron based find command problems

Chris,

Please disregard my ill-advised and uninformed prior answer. I'm still working on my reading skills.


Pete

Pete
Bill Douglass
Esteemed Contributor

Re: cron based find command problems

Not sure why this is not working. A possible work-around for the time-being is to filter the list produced by the find command:

for file in `find . -mtime +1 -print | grep \.dbf$`


For debugging, try echoing $ARCHIVEDIR to a file, along with the output of the find command by itself.
john korterman
Honored Contributor

Re: cron based find command problems

Hi,
your code works on my system as well!
But I do not personally like the use of the word "file", as I have the idea it could conflict with the built-in function of the same name. However, it apparently does not disturb my shell - could it be disturbing yours?
Far out, but...

regards,
John K.
it would be nice if you always got a second chance
John Meissner
Esteemed Contributor

Re: cron based find command problems

have you tried the command list this:

find . -name *.dbf -mtime +1 -print {} \;
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: cron based find command problems

sorry... nope.. that didn't work.
All paths lead to destiny
Donny Jekels
Respected Contributor

Re: cron based find command problems

escape the star sign.

find . -name \*.bdf -exec gzip {}\;


peace
Donny
"Vision, is the art of seeing the invisible"
Massimo Bianchi
Honored Contributor

Re: cron based find command problems

Hi,
maybe simple, but with which user are you executing the cron command ? the same as in interative mode ?

Is there any customization in the /var/adm/cron/.proto that may prevent the proper work ?

HTH,
Massimo

F. X. de Montgolfier
Valued Contributor

Re: cron based find command problems

Hi,

three points:

1. is your script using find, or /usr/bin/find?
2. Did you think of putting the mandatory #/usr/bin/ksh at the beginning of the script?
3. did you actually put the double quotes around "*.dbf" ?

I have run some tests on my system, and the script ran fine as long as I had thought of the three points above...

On the other hand, trying to launch the command on the crontab without putting it in a script failed, because cron uses the posix shell and not the kron shell...

Cheers,

FiX