LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-04-2011, 01:25 PM   #1
ellis859
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
Script to move files with log and email and cron


Hi,

I'm a newb and I need help please.

A user will be ftping some files to an upload directory. I need to move those files to another directory. I also need to mail a list of the just moved files to the user. This job will need to run every 10 minutes. I need to keep a log that holds all the files for the day that were moved, renaming it with the date/timestamp.

I have this below but I just can't put it all together. Can someone help me make a workable script out of this?


#!/bin/bash
# test.shl
source="/u03/upload/"
dest="/u01/dataload/"
log="/u03/logs/transfer.log"
arch="/u03/logs/transfer_daily.log"

list="ls /u03/transfer/finaid/upload/$1*"
echo $list > /u03/logs/transfer.log
echo $list >> /u03/logs/transfer_daily.log
mv /u03/upload/*.* /u01/dataload/

echo "$list has been copied to dataload directory successsfully" | mail -s "Transfer Complete" user@work.com


#!/bin/bash
# archlog.shl
mv /u03/logs/transfer_daily.log /u03/logs/transfer_daily_`date +"%Y%m%d%H%M%S"`.log


crontab:
10,20,30,40.50,60 00 * * 0-6 /u03/bin/test.shl # runs every 10 minutes
0 2 * * 0-6 /u03/bin/archlog.shl # runs at 2am

Thanks!
 
Old 01-04-2011, 06:25 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
A couple of comments:

1. you want the results of the ls cmd, so use backquotes (`) not "
Code:
list=`ls /u03/transfer/finaid/upload/$1*`

# or use $(...)
list=$(ls /u03/transfer/finaid/upload/$1*)
2. 1st cron cmd runs at midnight only. To run every hour, use the wildcard
Code:
10,20,30,40.50,60 * * * 0-6 /u03/bin/test.shl
 
Old 01-05-2011, 01:16 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I would also mention that you went to a lot of trouble to create variables in your first script, but then you never use them?
So first script could look like:
Code:
#!/bin/bash
# test.shl

source="/u03/upload/"
dest="/u01/dataload/"
log="/u03/logs/transfer.log"
arch="/u03/logs/transfer_daily.log"

list=( /u03/transfer/finaid/upload/$1* )
echo "${list[*]##*/}" | tee -a $arch > $log

mv ${source}*.* $dest

echo "${list[*]##*/} has been copied to dataload directory successsfully" | mail -s "Transfer Complete" user@work.com
I am also curious as to why the files are being moved from /u03/upload/ but when you echo the files that have been moved you look in /u03/transfer/finaid/upload/$1*
This then raises a second question, you use the parameter $1 but according to your cron job you never pass any arguments to the script:
Code:
10,20,30,40.50,60 00 * * 0-6 /u03/bin/test.shl # runs every 10 minutes
 
Old 01-05-2011, 08:55 AM   #4
ellis859
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
After some work and help I have the script working.....

#!/bin/bash
# test.shl
source=/u03/upload/
dest=/u01/dataload/
log=/u03/logs/transfer.log
arch=/u03/logs/transfer_daily.log
ls $source > $log
ls $source >> $arch
mv $source/* $dest/
filelist="cat $log"
# echo "$filelist" | mail -s "Transfer Complete" user@work.com
exit

Thanks for your help and I'll change the cron as you suggested.
 
Old 01-05-2011, 09:39 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just to be sure, you do know that the following has assigned the string cat along with the data stored in $log:
Code:
filelist="cat $log"
Which would seem to serve no purpose for at least 2 reasons:

1. The same information is already in $source
2. To get the data out of $log and store in variable you need to do the following:
Code:
filelist=$(cat $log)
 
Old 01-05-2011, 06:43 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Also, you have a typo
Code:
10,20,30,40.50,60 00 * * 0-6 /u03/bin/test.shl

# should read

10,20,30,40,50,60 00 * * 0-6 /u03/bin/test.shl

# or even

0,10,20,30,40,50 0 * * 0-6 /u03/bin/test.shl

# or
*/10 0 * * 0-6 /u03/bin/test.shl
Notes:
1. amended '.' to ',' in time list between 40 & 50
2. normally mins are measuerd from the start of the hr, so 0 instead of 60 for every 10 mins
3. single 0 is sufficient instead of 00
4. */x means every x mins (hrs etc) in the current position http://adminschoice.com/crontab-quic...Crontab%20file
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Email Notification - Perl Script jamesyreid Programming 3 07-21-2009 07:56 AM
using cron to send files via email aditaa Linux - Server 5 04-21-2009 04:03 AM
Shell script in cron.daily ;Where does the log go ? jeepescu Linux - General 2 12-03-2007 06:07 PM
Bash script to put log files into single file and email DragonM15 Programming 13 11-08-2007 03:27 AM
Cron is not outputting log files... Clubber Lang Linux - Newbie 2 09-27-2004 04:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration