Skip to content
NYC Tech Tips

NYC Tech Tips

Tech Tips From a NYC System Administrator

  • Home
  • About
  • Downloads
  • JB Network Design
  • Toggle search form

Batch Copy Logs From Server At Multiple Intervals Using Task Scheduler

Posted on June 12, 2015 By ninjatechnyc

I came across a situation where a vendor wanted logs files from a server at around 2am in the morning to troubleshoot why an application wasn’t sending a specific email at that time. Problem was, I wasn’t planning on being up at that early in the morning to grab log files and I didn’t know exactly what time it was supposed to send it. The time was usually around 1:30am but sometimes, when it worked, I’ve seen it sent at 4:30am.

So the plan was to come up with a simple batch file to handle the copying of the logs at every hour between 11PM and 5AM to my local Win 7 desktop,

This required a few things:

  1. A batch file that would a) make a directory based on the current time b) copy the files into that directory
  2. A task to run the batch file, triggered at every hour between 11pm and 5am.
  3. Access to the server where the logs are kept

 

Batch File

My batch file consisted of  the following:

@echo off
SET datestmp=%date:~-4,4%%date:~-10,2%%date:~-7,2%
SET HOUR= %time:~-11,2%
CALL :TRIM %HOUR%
SET timestmp=%HOUR%_%time:~-8,2%
MKDIR logs\servername\%datestmp%_%timestmp%
@echo on
COPY \\servername\logs\email.log  logs\servername\%datestmp%_%timestmp%\ @echo off
GOTO EOF
:TRIM
SET HOUR=%*
:EOF

 

The above batch file first gets the date and time, formats it, and creates a directory with the date/time as part of the directory name. So if the date and time was 6/09/15 and the time was 2am, the directory would be called “20150609_2_00”

Then it takes the email.log file from the \\servername\logs\ folder and copies it to a local folder on my machine called logs\servername\20150609_2_00\, which would be relative to where the batch file is run from.

I named the above batch file copylogs.bat and saved it on my local desktop under the C:\temp folder.

I ran the batch file to make sure it works and instead it failed! The reason being, the batch file ran under my desktop login account. I have a separate admin login for the server. So I mapped to the server using my admin login first. With that connection being made, I reran the batch file, and sure enough, it worked.

Next up was creating the tasks to run the batch at certain intervals…

Task Scheduler

1. Start by Creating a Basic Task:

basic task

2. Give the task a name:

task name

3. Set your trigger. In my case I’m setting to “One time” and will give it a specific time to start (probably about 20 minutes into the future so that I can test to see if it will run properly). Later, I will schedule it to run at particular times throughout the night.

trigger

4. Set your action. In My case I’m starting a program (my batch file):

action

5. Here’s where things start to get tricky.

Since it’s a batch file, the Program/script I’m running is cmd (command prompt). The Additional arguments will be:

/k start “” “c:\temp\copylogs.bat”

And it’s starting in c:\temp (no quotes).

start

6. After saving the task, one last thing I had to do was go back, edit it, and Change User or Group, so that it ran under my server admin account, and Run whether I was logged in or not, saving my password.

final

After I finished, and tested it successfully, I went back and edited the task, adding more Triggers:

more triggers

That’s it! Hopefully someone else will find this useful.

 

 

opinion, tech tips, windows Tags:batch, copy, task scheduler

Post navigation

Previous Post: Internet Explorer 8 Crashes on Manage Add-Ons
Next Post: Convert Unix [Epoch] time to human readible form

Search

Tags

adobe Amazon antispyware antivirus apache Apple AT&T blackberry browser China computer data breach Dell dilbert dogbert email encryption facebook faulting application FB firefox firewall flash drives freeware godaddy google Hack Identity Theft Internet Explorer iPhone linux malware Microsoft paypal phishing privacy recommended reading scam security software tech support ubuntu virus windows Wordpress

Archives

  • April 2021
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • June 2017
  • May 2017
  • December 2016
  • November 2015
  • June 2015
  • June 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • September 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • January 2011
  • December 2010
  • October 2010
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • December 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • February 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • June 2008
  • April 2008
  • March 2008
  • January 2008

Copyright © 2021 NYC Tech Tips

Powered by PressBook Dark WordPress theme