Windows Task Scheduler doesn't start batch fil

2019-03-12 10:12发布

I have a batch file with the code below to stop and start the SQL Report service:

net stop "SQL Server Reporting Services (MSSQLSERVER)" 

timeout /t 10

net start "SQL Server Reporting Services (MSSQLSERVER)"

I have set up the scheduled task to run daily, it currently runs as SYSTEM with the highest privileges set. I have set up the start in folder option on the action, and everything generally seems to be set up correctly. But when I run the task nothing seems to happen, it says the task has run but I cant see that the service has been restarted as it is meant to.

Can someone direct me to what I am missing? Thanks

15条回答
smile是对你的礼貌
2楼-- · 2019-03-12 10:25

Make sure you set the 'Start in' and 'Program/script' options correctly. If your file address is: C:\Temp\foo.bat, set the 'start in' option to 'C:\Temp' and the 'Program/script' option to 'foo.bat'.

To set the 'Start in' option: Right click task in the task scheduler > Properties > Actions > Edit.

If this alone doesn't work then try moving the .bat file to a directory with basic permissions (maybe a shared directory for example).

I had a problem where my .bat file was located in a folder with some restrictive permissions on it, so that only my user account could access it. Even though I had set up the task scheduler to use my credentials it still failed. Moving the .bat file to another directory sorted the issue.

查看更多
爷、活的狠高调
3楼-- · 2019-03-12 10:28

I had the same problem. I believe it's a privilege problem. If you have "Run only when user is logged on" selected, then it won't happen.

You've hopefully figured it out by now, but I wanted to register it here for the next person who has wasted hours on this.

查看更多
走好不送
4楼-- · 2019-03-12 10:30

This is a pretty old thread but the problem is still the same -

I tried multiple things, none of them worked -

  1. Added a Start In Path (without quotes)
  2. Removed the complete path of the batch file in the Program/Script field etc
  3. Added C:\Windows\system32\cmd.exe to the Program and added /c myscript.bat to the arguments field.

This is what worked for me -

Program/Script Field - cmd

Add Arguments - /c myscript.bat

Start In : Path to myscript.bat

查看更多
我只想做你的唯一
5楼-- · 2019-03-12 10:30

The solution is that you should uncheck (deactivate) option "Run only if user is logged on".

After that change, it starts to work on my machine.

查看更多
祖国的老花朵
6楼-- · 2019-03-12 10:33

My problem was caused by OneDrive. OneDrive was syncing the folder my batch file lived in, and that seems to prevent Task Scheduler from executing it. (Doesn't anyone at MS test this kind of thing?)

Anyway by moving my batch file to a folder that wasn't in OneDrive the batch file could be started by Task Scheduler.

查看更多
倾城 Initia
7楼-- · 2019-03-12 10:34

Try the code below:

Batchfile.bat:

cd c:\batchfilepath
net stop "SQL Server Reporting Services (MSSQLSERVER)" 
timeout /t 10
net start "SQL Server Reporting Services (MSSQLSERVER)"
查看更多
登录 后发表回答