Bat file doesn't log when running from task scheduler?

2

I made a bat file which restarts 2 services, and I have it run in task scheduler. If I run the bat file normally, I can see it logging in current directory as the bat file. But if I run it through task scheduler, I can see the bat runs, like in services window, I can see the services restarting, but it doesn't log anything.... Does anyone know what the issue is?

This is on windows server 2008 r2 64-bit.

Thanks

@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%

omega

Posted 2016-01-26T17:07:36.137

Reputation: 183

Try specifying an absolute path in your SET logfilename_sp_fix="SPSearchFix.log" line. E.g.: SET logfilename_sp_fix="c:\temp\SPSearchFix.log", and ensure all users have write access to that folder. – Ƭᴇcʜιᴇ007 – 2016-01-26T17:09:41.033

I figured it out, it was logging to system32 folder for some reason, but putting absolute path worked. – omega – 2016-01-26T17:11:28.520

That's what I figured, I'll post up an actual answer. ;) – Ƭᴇcʜιᴇ007 – 2016-01-26T17:11:57.250

Answers

2

When you run things in the Task Scheduler, the working folder (by default) is %windri%\system32. So any files created by the task, that don't have a path specified, will be written there (or at least it will attempt to write them there).

To avoid this, specify an absolute path in your SET logfilename_sp_fix="SPSearchFix.log" line.

E.g.: SET logfilename_sp_fix="c:\temp\SPSearchFix.log

And ensure all users (or at least the user the task runs as) have write access to that chosen folder.

Ƭᴇcʜιᴇ007

Posted 2016-01-26T17:07:36.137

Reputation: 103 763