Perl script, replace file with newer file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script, replace file with newer file
# 1  
Old 06-27-2012
Perl script, replace file with newer file

Hello,

Can you please help me one this:

I have two servers: Server A and server B.
Every day on 03.00AM in only one on these two servers (randomly)is generated one file, lets say file.txt.
I want to copy this file also to the other server.

I want to create a perl script that does this:
To see if the file file .txt that is generated is up to date.
If so, to copy this file to the other server.

Thank you in advance!!
Ervin
# 2  
Old 06-27-2012
call the following script as a cron job @ 3:10 on each host:
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @stat=stat('path/to/file.txt');
if ($stat[9] > (time() - (60 * 60) ) ){ # last modified within an hour
   exec ('scp path/to/file.txt other.server:/absolute/path/to/file.txt'); #assuming you wish to replace the copy on the other node in the cluster
}


Last edited by Skrynesaver; 06-27-2012 at 07:18 AM.. Reason: updated code to reflect requirments and mentioned that it should be a cron job
# 3  
Old 06-27-2012
Thank you very much for replying!

Would you be so kind, and include also headers...also scp command?
Key exchanged is not a problem, servers are in cluster, and doesn need password to communicate.

Thank you!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy file only if newer

I only want the file copied if it is newer. But it still copies the file? zip -u Ubuntu_Documents.zip ./* cp -u Ubuntu_Documents.zip $DOCS_Backup/Ubuntu_Documents_`date +"%Y-%m-%d-%H-%M"`.zip (5 Replies)
Discussion started by: drew77
5 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. UNIX for Dummies Questions & Answers

If condition to check one file newer than the other

Dear All, I'm new to unix scripting. I'm trying to write an utility script which has to check if one file is newer than another one. $3 $4 $5 $6 are files .txt. Help me please. for i in $3 $4 $5 $6 do if then echo "$1 is newer than $i" else echo "$i is newer than $1" fi (9 Replies)
Discussion started by: Manueldo
9 Replies

4. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. UNIX for Dummies Questions & Answers

Replace line via perl script of file

Hi All, I wanted to do following. 1) If file exit then open file for reading and check if my string is there then i wanted to replace the entire line with that string + something else then close the file. 2) If file does not exit then i need to open the file and write to it. I am done with... (0 Replies)
Discussion started by: Tarun24
0 Replies

7. Shell Programming and Scripting

how to find a file then overwrite with a newer version

This should be a simple script, but can't find one with google search. I just need to find the file that is in many directories, then overwrite that file with a newer version i.e. find file.jar then overwrite with /root/file.jar All I get in searches is substitute text with new test inside... (1 Reply)
Discussion started by: haircat
1 Replies

8. Shell Programming and Scripting

How to replace a value in a file in perl?

Hi, I have a file with the following contents and i want to replace that value in a file with some other value. #file.txt /local/Disk/data n 192.168.55.98 52035 3 1 2 1 1 192.168.55.98 Here is my code. (2 Replies)
Discussion started by: vanitham
2 Replies

9. Shell Programming and Scripting

How to script to find the newer date in a text file?

Hi, I have a text file, foo.txt, it looks something like below. In the file there is a line that gives the date in the form of: Mon Jun 15 11:09:31 2008. I need to find which date is the newest and then store certain details of that list data to another file. So, in this sample text file, I... (6 Replies)
Discussion started by: boolean2222
6 Replies

10. Shell Programming and Scripting

If condition to check one file newer than the other - first file name uncertain

Dear All, I'm new to unix scripting. I'm trying to write an utility script which has to check if one file is newer than another one. The condition is I dont know the full name of the first file. I searched google and this forum for any such examples, but couldn't find any or may be I would have... (9 Replies)
Discussion started by: achilles5
9 Replies
Login or Register to Ask a Question