3

I have an issue with my program that occurs once or twice every 6 hours. So I was wondering if there is any way I can get the batch script to constantly run in the background, and only execute the command it is given at 6 in the morning, at noon, at 6 in the afternoon and at midnight.

my script is just one command which is

"C:\Program Files\WinSCP\WinSCP.com" /command "open %INPUT%" "get /etc/logs/*" "get /etc/network/interfaces" "bye"

I have been breaking my neck and can't seem to figure out a way to get the program to sleep and not use up cpu until a certain time of day.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Quillion
  • 6,346
  • 11
  • 60
  • 97

3 Answers3

2

The Batch file below execute the command at 6, 12, 18 and 0 hours:

@echo off
:waitNextRun
for /F "delims=:" %%h in ("%time%") do set hour=%%h
set /A mod6=hour %% 6
if not %mod6% == 0 goto waitNextRun

"C:\Program Files\WinSCP\WinSCP.com" /command "open %INPUT%" "get /etc/logs/*" "get /etc/network/interfaces" "bye"

:waitNextHour
for /F "delims=:" %%h in ("%time%") do if %hour% == %%h goto waitNextHour
goto waitNextRun

However, this Batch file does not run "in the background", but as a normal Batch file. You may minimize its CPU use by starting it via this command:

START "Run WinSCP every six hours" /MIN /LOW theBatchFile
Aacini
  • 65,180
  • 12
  • 72
  • 108
1

The right way to do it is probably to set up a cron task, or if you're using Windows and don't have access to cron, e.g. through CygWin, then to use the task scheduler.

Philippe
  • 9,582
  • 4
  • 39
  • 59
0

Use the scheduling built into the operating system. Under Linux, it's cron under windows, search for Scheduler. That will let you run arbitrary scripts on whatever schedule you like.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
  • In Windows the scheduler is accessed via AT command. However, it allows to schedule tasks on specific days and times only (it does not allow to repeat tasks on any time interval), so it is necessary to execute AT four times with 0, 6, 12 and 18 hours and include in each execution all the month days (from 1 up to 31), and repeat this procedure each month. Also, it requires the Windows Schedule service be running. – Aacini Jan 05 '12 at 04:56
  • @Aacini - Thanks for listing the command. I am only familiar with the GUI for configuring the scheduler. I'm not sure what version of windows I was using but I'm pretty sure I could schedule the script to execute multiple times during the day, along with a host of other options. It's possible that, under the hood, it all ended up the same as you describe, but I don't know about it. Also, good point about the service having to run. It _does_ need to, but as a service, it doesn't use _too_ many resources. – cdeszaq Jan 05 '12 at 14:27
  • Why you don't try my Batch solution before try the Scheduler? To do that you only need to copy the Batch file and type a Start command! Also, this would be a good comparison point if you later use the Scheduler. Are you sure that the Scheduler will behave better than the Batch solution? The answer to this question is of general interest! ;-) – Aacini Jan 06 '12 at 20:04