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

6,706

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.

Share:
6,706

Related videos on Youtube

omega
Author by

omega

Updated on September 18, 2022

Comments

  • omega
    omega over 1 year

    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%
    
    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 over 8 years
      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.
    • omega
      omega over 8 years
      I figured it out, it was logging to system32 folder for some reason, but putting absolute path worked.
    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 over 8 years
      That's what I figured, I'll post up an actual answer. ;)