从任务计划程序运行时,Bat文件没有记录?

时间:2016-01-26 17:08:32

标签: windows batch-file

我制作了一个重启2个服务的bat文件,我让它在任务调度程序中运行。如果我正常运行bat文件,我可以看到它在当前目录中作为bat文件登录。但是,如果我通过任务调度程序运行它,我可以看到bat运行,就像在服务窗口中一样,我可以看到服务重新启动,但它没有记录任何内容....有谁知道问题是什么? / p>

这是在Windows Server 2008 r2 64位上。

由于

@echo off
SET waittimeseconds_sp_fix=15
SET logfilename_sp_fix="SPSearchFix.log"

echo %date% %time% - Restart starting >> %logfilename_sp_fix%


net stop SPTimerV4 >> %logfilename_sp_fix% 2>&1
if ERRORLEVEL 0 (
    echo %date% %time% - WORKED - Stopped timer service >> %logfilename_sp_fix%
) ELSE (
    echo %date% %time% - FAILED - Stopped timer service >> %logfilename_sp_fix%
    exit
)

timeout %waittimeseconds_sp_fix%

net stop osearch14 >> %logfilename_sp_fix% 2>&1
if ERRORLEVEL 0 (
    echo %date% %time% - WORKED - Stopped search service >> %logfilename_sp_fix%
) ELSE (
    echo %date% %time% - FAILED - Stopped search service >> %logfilename_sp_fix%
    exit
)    

timeout %waittimeseconds_sp_fix%

net start SPTimerV4 >> %logfilename_sp_fix% 2>&1
if ERRORLEVEL 0 (
    echo %date% %time% - WORKED - Started timer service >> %logfilename_sp_fix%
) ELSE (
    echo %date% %time% - FAILED - Started timer service >> %logfilename_sp_fix%
    exit
)

timeout %waittimeseconds_sp_fix%

net start osearch14 >> %logfilename_sp_fix% 2>&1
if ERRORLEVEL 0 (
    echo %date% %time% - WORKED - Started search service >> %logfilename_sp_fix%
) ELSE (
    echo %date% %time% - FAILED - Started search service >> %logfilename_sp_fix%
    exit
)

timeout %waittimeseconds_sp_fix%

echo %date% %time% - Restart completed >> %logfilename_sp_fix%

echo. >> %logfilename_sp_fix%
echo. >> %logfilename_sp_fix%
echo. >> %logfilename_sp_fix%
echo. >> %logfilename_sp_fix%
echo. >> %logfilename_sp_fix%
echo. >> %logfilename_sp_fix%

1 个答案:

答案 0 :(得分:1)

我建议确实正在创建该文件,但是在批处理运行的“当前目录”中。

你可以执行

cd "c:\your\desired\directory"

作为@echo off

之后的第一行

将当前目录设置为计划任务的一部分

使用完整的pathanme定义文件名。

顺便说一句

if ERRORLEVEL 0 (
对于所有意图和目的,

始终为真,因为它意味着“如果errorlevel为0或大于0”。

您可以使用

if %errorlevel%==0 (

作为替代品。