I'm trying to copy a file from Linux based server to my Windows PC. The file I want to copy is not readily available ie I got to run the following command in putty
tail -n 100000 conveyor2.log | grep -P 'curingresult OK' | sed 's/FT\ /FT/g' |awk '{print $5 $13}' |sed 's/\"//g' | uniq | sort -n |uniq >> myfile.txt
to move the data to myfile. Now I have to move myfile from server to PC for every 30 mins. I could run the code in pscp
pscp -pw PASS user@IPADDRESS:/var/mp/95910/log/myfile.txt C:\Users\Administrator\Desktop\Myfolder
for moving the file to my PC but I need it to be done automatically. Also "myfile" need to be updated automatically every 30mins.
`
There are numerous ways to accomplish this. Personally I would set up an SSH server (such as FreeSSHd http://www.freesshd.com/) on the Windows PC so that everything could be accomplished in one step using the Linux server. If you are not able to install software on your PC, you can still make this work by scheduling scripts on both the Windows PC and Linux server.
First configure SSH public-key authentication to connect from the Windows PC to the Linux server. This is a more secure alternative to having the password in clear text in your script, and a better configuration for scheduled tasks.
Perform these steps on the Windows PC:
Perform these steps on the Linux Server:
ssh into the Linux server, and using your favorite editor open the ~/.ssh/authorized_keys file. If it doesn't exist, you may need to execute these commands
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
vi ~/.ssh/authorized_keys
Paste the contents of your Windows public key into this file, then save and close the file
Next, create a scheduled task on the Linux server to keep "myfile" updated every 30 minutes.
Open a text editor and place the contents of your script in the file e.g. "vi update_myfile.sh"
#!/bin/bash
tail -n 100000 conveyor2.log | grep -P 'curingresult OK' | sed 's/FT\ /FT/g' |awk '{print $5 $13}' |sed 's/\"//g' | uniq | sort -n | uniq >> /var/mp/95910/log/myfile.txt
(edit the path as needed)
Schedule the task to run every 30 minutes (at 15 and 45 minutes past every hour) using cron. Run the command "crontab -e", and add this line, changing the path as needed:
15,45 * * * * /home/<username>/update_myfile.sh
Finally, create a script on the Windows PC to copy over the file, at a time offset from when the Linux script runs.
In notepad create a script called copy_myfile.bat containing:
pscp user@IPADDRESS:/var/mp/95910/log/myfile.txt C:\Users\Administrator\Desktop\Myfo
You didn't mention what version of windows you are running on the PC, but if Windows 7, click Start and under Control Panel click "System and Security", then "Administrative Tools", then "Task Scheduler". If Windows 8, just click Start then type "Task Scheduler".
Within Task Scheduler, click Action, then "Create Task" and the Create Task dialog should appear
Under the General tab, give the task a Name and Description
Under Triggers, click New, and in the resulting dialog select Daily, click the "Repeat task every" checkbox and select "30 minutes" and click OK
Under Actions, click New, leave Action set to "Start a program" and browse to the copy_myfile.bat script created in step 1. Click OK
Click OK and the task should be scheduled to run automatically
So every :15 and :45 the Linux server file will be updated, and it should get copied to your Windows PC every :00 and :30. These times can be adjusted as you see fit. If the clocks on the two systems are in sync, you may not need 15 minutes of wiggle room.
Hope this helps!