Quillion
Quillion

Reputation: 6476

batch script, put to sleep until certain time

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.

Upvotes: 3

Views: 6046

Answers (3)

Aacini
Aacini

Reputation: 67216

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

Upvotes: 2

Philippe
Philippe

Reputation: 9752

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.

Upvotes: 1

cdeszaq
cdeszaq

Reputation: 31280

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.

Upvotes: 0

Related Questions