crontab

Simple script that removed some files successfully as script sh -x
But not working in crontab, however it fires and writes log files even some count of the files
I set all env and user interaction
The crontab runs in the same user where the files are there.

Script:

## Set the Environment
##. $HOME/.profile

#!/usr/bin/ksh

ORACLE_HOME=$ORACLE_HOME
ORASID=xyz
ORAUSER=xyz
ORAPASS=xyz
LD_LIBRARY_PATH=/usr/lib:/...

FILE_FOLDER=/u01/temp/
RPT_FOLDER=/u02/temp/
export FILE_FOLDER RPT_FOLDER

lc_new=`ls -lrt $RPT_FOLDER/ | grep "invoice"|wc -l`
export lc_new

echo "Its time to cleanup" $lc_new "files"
ls -lrt $RPT_FOLDER |grep " invoice "|awk '{print $9}'|xargs rm -f

In log file we can see the count i.e: "Its time to cleanup 48 files"

IT doesnt work when invoked by cron because cron knows nothing of your environment:
starting by the path.. this explains why it works so well when you (env loaded...) execute the same script...
Add in the script all cron needs and it will work fine...

The "rm" will try to delete files from the home directory not from $RPT_FOLDER because the filename "$9" does not contain the directory name.

OK, but how come it correctly count the files from that dir
and writes in the log file.
(Have did a double check)

Thank you,

$lc has been invoked the some special "new files". so we trying to insert the new file.

Have modified ($lc) and still its not working.

Simple ques:
why the script cant remove the file
same time how can it read that file.

Note:The crontab runs in the same user where the files are there.

Thanks,

The script may work from the command prompt if the current directory happens to be $RPT_FOLDER and none of the filenames contain a space character.

On closer examination, your first grep looks for "invoice", and your second grep looks for "<space>invoice<space>".
Is "invoice" part of the filename or something else like the unix user or unix group name>?

You can see what parameter xargs generates by substituting "echo" for "rm -f".

ls -lrt $RPT_FOLDER |grep " invoice "|awk '{print $9}'|xargs echo

Note: As you will be aware, when a script is run from cron any output to screen will be seen in the user account unix mail.

Try this simple example from the command prompt: In the first example the second command fails because the xargs parameter does not contain the directory name. In the second example the command works because we are in the right directory.

cd /usr
ls bin | grep "false" | xargs echo
ls bin | grep "false" | xargs ls

cd /usr/bin
ls | grep "false" | xargs echo
ls | grep "false" | xargs ls

Let's agree:

lc_new=`ls -lrt $RPT_FOLDER/ | grep "invoice"|wc -l`
export lc_new
# This will give you an output acording to your post

echo "Its time to cleanup" $lc_new "files"
# As you mention it find a total of concerned files
# But
ls -lrt $RPT_FOLDER |grep " invoice "|awk '{print $9}'|xargs rm -f
# Doesnt rm anything...
# Quite normal as already mentionned ITS NOT THE SAME grep patern

Demonstration:
ant:/ant $ ls -lrt .|grep "sas"|awk '{print $9}'
sasauth.shadow
setinit.sas
sasobjspawner_diag.howto
sas9_inst_validate
sasobjspawner_diag.howto.txt
sasdatfiles
sasdatfiles.txt
at_sas_expect_interactive.howto
ant-sas
sas_patches
sas_patches_install_howto.txt
ant:/ant $ ls -lrt .|grep " sas "|awk '{print $9}'
ant:/ant $
So replace " invoice " by "invoice"

I agree with "vbe", the grep commands differ. Also at the time of the "rm" the current working directory is wrong.

Perhaps post the directory tree and a sample directory listing? We would normally use "find" to generate a file list for a cleanup but it highly depends on whether there are directories beneath the directory to be cleaned.

Check the file permission

You may give the by this command

chmod 777 filename

:b:

Hi Its working now,

`ls -lrt $RPT_FOLDER |grep "invoice"|awk '{print $9}'|xargs rm -f $RPT_FOLDER/`

In the above cmd �print $9� gives only the file name i.e invoice and "rm -f" cmd just tried to remove that file in the home directory itself from there the CRON is executed.
It is modified to print "/u03/main/path/"$9 and now the "rm -f" cmd is something like �rm u03/main/path/invoice....htm�

Thanks to every one.

:wink: