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.
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 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
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.
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�