Ftp bash script appends to file using cron instead of coping new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ftp bash script appends to file using cron instead of coping new file
# 1  
Old 07-10-2015
Ftp bash script appends to file using cron instead of coping new file

I have a bash script that is running a mysql query and creating a csv file with a time stamp. It then uploads that to a ftp server. Everything works great when I manually run it. But then I have a cron job set to run every monday, wednesday and friday at 5am est. When the cron job runs, it appends to the last file. For example, the file that ran wednesday is called 15-07-08_report.csv. On Friday the cron job runs and instead of creating a new file called 15-07-10_report.csv, it just appends to the file called 15-07-08_report.csv. If I run it manually without the cron job, the bash script will create a new file on the ftp server called 15-07-10_report.csv and leave the file from wednesday. That what I want to happen when the cron job runs.

bash script:
Code:
#!/bin/bash
#DB server details

server_ip='mysql_server.com';

db_user='user';
db_pass='password';
db_name='db_name';


#Delete all previous csv files

rm *.csv

#create new file

csv_file="$(date -d now +%y-%m-%d)_report.csv";

touch $csv_file

#Query to be execute in MySql and save data in csv file

mysql -u$db_user -p$db_pass -h$server_ip < /var/www/html/tools/report-script/report.sql $db_name | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' >> $csv_file

#Open FTP and put files there
echo "start ftp"

#Your FTP IP or URL
HOST="ftp.upload.com"

#FTP user name
USER="user_me"
echo "pass"
#FTP user password
PASSWD="my_pass"

echo "change"

#Change to directory where the original csv file is located

cd /var/www/html/tools/report-script

echo "transfer file"
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD

!#Folder on the FTP server you want to save to.  By default it will save to the user's home directory
cd /folder

!# Turn off prompting yes or no to transfer file
prompt

!#copies the cvs file to ftp server
mput *.csv

quit
END_SCRIPT
exit 0

cron job:
Code:
0 5 * * 1,3,5 /var/www/html/tools/report-script/test_report.sh

Any help is much appreciated. I can't figure out why the behavior would be different by using cron to run the script vs running it manually. Thanks!
# 2  
Old 07-10-2015
That's a bit hard to understand. You remove all .csv files, and then define and touch "todays" .csv file:
Code:
rm *.csv
csv_file="$(date -d now +%y-%m-%d)_report.csv"
touch $csv_file

Where is the old file that is being appended to?
Please post a directory listing after above commands.

Why do you cd /var/www/html/tools/report-script ? There shouldn't be any new file to be transferred!?
# 3  
Old 07-10-2015
on the server that i am running the script on, i delete any previous cvs files and then create a new one with todays date. I then dump the sql query into the csv file with todays date. I don't really need the touch command in there as the query dumps it into a csv file anyways. i was just trying to see if it made a difference, it doesn't.

The file that it is appending too is on the ftp server.

I guess I don't need the
Code:
cd /var/www/html/tools/report-script

I was under the impression i had to tell it where to go but i guess it's already working from that directory.

---------- Post updated at 10:38 AM ---------- Previous update was at 10:16 AM ----------

Code:
-rw-r--r-- 1 root root 1479688 Jul 10 14:14 15-07-10_report.csv

# 4  
Old 07-10-2015
So you're saying that ftp's mput does the append? Even harder to believe. man ftp:
Quote:
put local-file [remote-file]
Store a local file on the remote machine. If remote-file is left unspecified, the local file name is used after processing according to any ntrans or nmap settings in naming the remote file. File transfer uses the current settings for type, format, mode, and structure.
Please post a listing of the remote server directory before and after the transfer.
# 5  
Old 07-10-2015
I don't know what's doing the append. if i run it manually using
Code:
sh test_report.sh

it runs fine. the script runs and transfers a file called 15-07-10_report.csv. So when I look at the ftp server I see two files. One called 15-07-08_report.csv and one called 15-07-10_report.csv.

When the cron job runs, I check the ftp server and see that the last file is still there of 15-07-08_report.csv but the date modified is 7/10/2015 5:00:05 AM. It didn't craete a new file with the name 15-07-10_report.cvs.
# 6  
Old 07-10-2015
I've been reading this thread with some interest and curiosity so this is just "thinking out loud" without any real experience to draw on.

However, I would be asking whether the cron ftp job actually closed off the ftp session plus whether the next cron run picks up the old session. Certainly, running the script interactively would definitely create a new ftp connection. I would initially check via a process listing whether, when the cron job has supposedly finished, that the associated processes are gone and the ftp session is no longer there. I currently can't think of any other way the cron job could append to an old file unless it's still connected to it and has it held open.

I'll carry on thinking about this one.
# 7  
Old 07-10-2015
Code:
rm *.csv

Code:
0 5 * * 1,3,5 /var/www/html/tools/report-script/test_report.sh

When the job is run by the cronjob the current directory is not /var/www/html/tools/report-script/

cd into the directory first.
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ftp with bash, append file where left off

I'm working on a bash script to finish uploading a file. I need a way to get $filesize so that "restart $filesize" will work. Here is my script: ftp -n -v <<END_SCRIPT open ftp.$domain user $user@$domain $password size $file restart $filesize put $file quit END_SCRIPTWayne Sallee... (9 Replies)
Discussion started by: WayneSallee
9 Replies

2. Shell Programming and Scripting

Needed script to FTP a File and generate a quality checksum file

hi all i want a script to FTP a file and should generate a quality checksum file means when I FTP a file from one server to another server it should generate a QC file which should contain timestamp,no.of records in that file Thanks in advance saikumar (3 Replies)
Discussion started by: hemanthsaikumar
3 Replies

3. UNIX for Advanced & Expert Users

cron job to extact lines from files to another file and ftp to new server

i have a text file in this format: which creates a new one everyday in the form of filename _zing__r200_2012_8_10_log.txt Fri Aug 10 07:29:17 EDT 2012, usera(192.168.0.245) to anotheruser: hey top, this is a private test Fri Aug 10 07:29:28 EDT 2012, anotheruser(192.168.0.245) to usera: got... (2 Replies)
Discussion started by: bkkid
2 Replies

4. Shell Programming and Scripting

Bash script for new file in ftp folder

Hello, I'm trying to make a bash script that send me e-mail if there is any new file in my ftp folder. cat inotify.sh #!/bin/sh /usr/bin/inotifywait -e create \ -mrq /home/mrowcp | while read line; do echo -n "$line " >> /var/log/inotify.log echo `date | cut -d " " -f1-4` >>... (3 Replies)
Discussion started by: mrowcp
3 Replies

5. Shell Programming and Scripting

Problems coping a file in a shl script

I have the following in a shl script: SCRIPT_PATH="/u01/app/banner/test/skid/plus/"; FILE_PATH="/nfs/mercury/u03/banner/test/skid/log"; LIST_FILE_PATH="/u01/banjobs/TEST"; SCRIPT_NAME="szpcal1.sql"; FILE_NAME='new_applicant_list'; I want to copy the file FILE_NAME to LIST_FILE_PATH ... (10 Replies)
Discussion started by: rechever
10 Replies

6. Shell Programming and Scripting

end of file error bash ftp script

Hello kind programmers :) I am a newbie and running into an error "line 28: syntax error: unexpected end of file" on the script shown below. Any help would be greatly appreciated. Thanks! #! /bin/bash if ($#argv <3) then echo 'Usage get_modis_snow ' echo 'ftp script for MYD10A2... (2 Replies)
Discussion started by: cmshreve
2 Replies

7. UNIX for Dummies Questions & Answers

echo appends to a read-only file

echo command can append to a read-only file too. If the file is read-only, with w flag removed, then even echo should not be able to append to the file. But it is possible. Can anybody explain the below behaviour? /root > echo 'sample text' > file /root > cat file sample text /root >... (2 Replies)
Discussion started by: anand_bh
2 Replies

8. Shell Programming and Scripting

Progress bar while coping a file needed.

Hi guys I want to create a script which will show the amount of data that is been copied from one location to another location. For example: Say i have a file called abc.img which is approximately 4 gb of size therfore everytime i copy the file from one location to another i want a progress bar... (3 Replies)
Discussion started by: pinga123
3 Replies

9. Shell Programming and Scripting

Not able to FTP a file with root cron

Hi All, I am facing a difficulty with root cron. There is a script which is running in root's cron for every 3 minutes. I am having a different but i have the access to change the script which is running in root's cron.In the script the output is already going to a file in /tmp.I have just added... (1 Reply)
Discussion started by: usha rao
1 Replies

10. UNIX for Dummies Questions & Answers

Script That Appends

I am trying to write a bash script that takes a file as an argument and appends my name if this file ends in .txt... I am not sure how to do this though. Any help is greatly appreciated! (4 Replies)
Discussion started by: GTRocker8824
4 Replies
Login or Register to Ask a Question