@Echo Off
color 1e
cls
Echo.
Echo 某某服务全自动启停控制.
Echo --------------------------------------------------------------------
:zidong
@Echo OFF
Set /a thisTime=%Time:~0,2%
If %thisTime% GTR 8 Goto :start
If %thisTime% GTR 17 Goto :stop
Echo Time is between 8:00-17:00
:: 全自动启停某某服务 ...
GOTO :zidong
:start
@echo 正在启动某某服务
net start Messenger
Goto zidong
:stop
@echo 正在停止某某服务
net stop Messenger
Goto zidong
:End
Exit
[ Last edited by boyhong on 2007-4-26 at 08:45 PM ]作者: boyhong 时间: 2007-4-26 22:00 好像运行不了~~经过我想,对于1:00-9:00,偏移量是2的话一定会错的。但不知怎么解决已便实现我的这个需求~~
盼高手指点一下作者: flyinspace 时间: 2007-4-26 22:07 使用 AT 或自己建立计划任务咯。。
at 08:00 "net start ***"
at 17:00 "net stop ***"
若是xp。
at 08:00 "sc start ***"
at 17:00 "net stop ***"作者: boyhong 时间: 2007-4-26 22:20 谢谢楼上的~~呵呵。。在那台电脑上不敢开 AT 服务~~怕怕安全性...呵呵。几乎24小时对公网服务的,操作系统还是个2000呢~~~
谢谢您的回复
要是用计划任务的话。还有一个问题,怎么让这个脚本循环执行呢?
[ Last edited by boyhong on 2007-4-26 at 09:28 AM ]作者: bjsh 时间: 2007-4-26 22:29 我不大理解你的意思;
Originally posted by bjsh at 2007-4-26 01:06 PM:
to:everest79
他要求 在 不能启用schedule的情况下;
运行脚本后自动在规定时间外停用指定的服务;
也就是说:
如果现在时间13点钟 运行该脚本;
...
谢谢各位的关心~~
对的,我的电脑很少关机,充当服务器角色。。。
这种情况下,电脑上有个服务是定时开启定时关闭的。
我想用批处理来实现。可以吗?作者: boyhong 时间: 2007-4-27 06:56
Quote:
Originally posted by everest79 at 2007-4-26 12:57 PM:
if %time% geq 8 (
if %time% lss 17 (
net start service
) else (net stop service)
) else (net stop service)
不过这样可能更简单
set /a a=%time:~0,2%,1/(a/8),1/(16/a)&&net sta ...
[ Last edited by bjsh on 2007-4-26 at 07:35 PM ]作者: bjsh 时间: 2007-4-27 08:34 set /a at=%time:~0,2%,1/(at/8),1/(16/at)&&net start service||net stop service
@echo off
echo wscript.sleep 600>delay.vbs
:continue
set /a at=%time:~0,2%,1/(at/8),1/(16/at)&&net start 服务1 net start 服务2 net start 服务3||net stop 服务1 net stop 服务2 net stop 服务3
cscript /nologo delay.vbs
goto continue
[ Last edited by boyhong on 2007-4-26 at 07:40 PM ]作者: bjsh 时间: 2007-4-27 08:42 做成系统服务看这里:
'************************************************************
' 第一次写 vbs 。写得不好,还请高手指点。。
' 使用时,把StopServerName = 后面的名称替换自己需要的名称就行了。
' 脚本误差 +/- 一分钟。
' 而且也解决了内存占用问题。4M 内存应该影响不大吧?
'************************************************************
Dim NHour,NMin,SleepTime
Dim RunState
strComputer = "."
Const StopServerName = "Dnscache"
Do
Nhour = Hour(Now)
Nmin = Minute(Now)
SleepTime = ( 60 - NMin ) * 60 * 1000
if Nhour > 8 and Nhour < 17 Then
RunState = SetServerState(StopServerName,"Running")
Else
RunState = SetServerState(StopServerName,"Stopped")
End If
‘WScript.Echo "" & SleepTime
WScript.Sleep SleepTime
Loop
Function SetServerState(SName,State)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each Item in colItems
If Item.Name = Sname And Item.State = "Running" Then
If Item.State <> State Then
Item.StopService()
End if
ElseIf Item.Name = Sname And Item.State = "Stopped" Then
If Item.State <> State Then
Item.StartService()
End if
End if
Next
End Function
[ Last edited by flyinspace on 2007-4-28 at 11:39 AM ]