51

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
jimminybob
  • 1,392
  • 2
  • 24
  • 60
  • 1
    Does it work if you use your user credentials rather than system? That will be a good test to see if it is functioning as a scheduled task. Maybe rem out the `net start` line and verify that the service has actually stopped - to see if permissions is an issue. – foxidrive Oct 11 '13 at 13:04
  • I've edited it so it only has the stop line and I have tried using my credentials, and those of another user but it didnt stop the service. I can run the batch file by itself, not with the task, and it works fine – jimminybob Oct 14 '13 at 07:58
  • Maybe it's not being launched correctly or there is still a permissions issue - or there is more in the batch file that we haven't yet seen. – foxidrive Oct 14 '13 at 08:22
  • The batch only contains the bit i've copied in above. I've just read elsewhere that if the batch contains quotes then it wont run from a task. Is this the case? If so how can I get around it? – jimminybob Oct 14 '13 at 08:29
  • You've misunderstood the quote comment, or it's plain wrong. On the command line you need to double quote any terms that contain spaces and other poison characters, but they will work fine. In task scheduler you may need to launch the batch file like this: `cmd /c "c:\myfolder\mybatch.bat"` – foxidrive Oct 14 '13 at 08:33

21 Answers21

127

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.

ninjaPixel
  • 6,122
  • 3
  • 36
  • 47
  • 7
    This worked. Just had to set the '''start in''' parameter to the directory the bat file was in. – Kellen Stuart Mar 06 '16 at 01:38
  • 2
    Yes, even though it says that start in is optional, just adding that to match the path to my batch file worked. Perhaps it is stripping the path from the batch filename? – Luke Briner Mar 11 '21 at 13:27
28

Wasted a lot of time on this silly issue!

add a cd command to where your batch file resides at the first line of your batch file and see if it resolves the issue.

cd D:\wherever\yourBatch\fileIs

TIP: please use absolute paths, relative paths ideally should not be an issue, but scheduler has an difficult time understanding them.

sdsc81
  • 570
  • 8
  • 18
MoMo
  • 440
  • 5
  • 4
  • This was the fix for me (it seems). By adding the CD to go to the folder containing the EXE run by the script, it seems to have got it. Answer upvoted. I wonder if by adding "Start In" would also work, but I'm not going to tempt fate! – Gazman Development Gosford Oct 24 '18 at 06:27
  • A little late to the party, but wouldn't another option be: pushd "%~dp0" Then you wouldn't have to worry about getting the path right, and/or changing it when moving the script. Of course you would also need to end your script with: popd – Anders Jensen Oct 31 '19 at 10:42
15

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

MackM
  • 2,906
  • 5
  • 31
  • 45
smitraa
  • 151
  • 1
  • 2
  • 1
    Hello Simitraa and welcome to the site. I tried to edit your answer to make it a little prettier and more concise, feel free to edit it again. – MackM Feb 24 '17 at 23:13
  • This didn't work for me. A dos window briefly flashes on the screen but that's it, it doesn't run. – Matthew Oct 10 '18 at 00:03
  • i tried many this worked for me. Start In : path to myscript.bat is without double quotes "" ie(D:\Projects\IOT\scripts) – Ein2012 Jan 12 '20 at 18:35
9

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.

Suhaib Ahmed
  • 91
  • 1
  • 1
  • Changing the option from `Run whether user is logged in or not` to `Run only when user is logged on`, worked for me. It's also important to point out that just because you have the machine locked does not alter the fact that you're logged on. So, **the task will still run, even if you locked your machine**. – Bobbie E. Ray Nov 21 '22 at 21:52
9

I was running this on a Windows Server OS. I worked for hours, only to find that the problem was that I had checked the "Run with highest privileges" checkbox. When checked on, it removes all drive mappings. And my .bat file was on the network.

enter image description here

MrMohr
  • 117
  • 1
  • 6
7

Had the same issue, make sure you check "Run only when user is logged on" at least that is what made my bat file alive again.

Gabo
  • 226
  • 3
  • 11
4

Set 'Program/script' -- > file.bat set 'Start in' the rest of path (file.bat)

widoz
  • 49
  • 1
4

On a Windows system which supports runas. First, independently run your program by launching it from a command line which was run as that user, like following

runas /user:<domain\username> cmd

Then, in that new command line, cd to the path from where you expect the task launcher to launch your program and type the full arguments, for example.

cd D:\Scripts\, then execute

C:\python27\pthon.exe script.py

Any errors that are being suppressed by task scheduler should come out to command line output and will make things easier to debug.

madballoonist
  • 626
  • 6
  • 6
3

One solution is you can run your '.bat' file with '.vbs' file and you can run this vbs file in windows scheduler.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("cron_jobs.bat"), 0, True

You can do like this and i hope it will fix your issue.

sherin.k
  • 63
  • 10
3

I had the same problem and none of the solutions worked. When I checked the history I figured out the issue. I had this warning

Task Scheduler did not launch task "\TASK_NAME" because instance "{34a206d4-7fce-3895-bfcd-2456f6ed6533}" of the same task is already running.

In the settings tab there is a drop down option for "If the task is already running, then the following rule applies:" and the default is "Do not start a new instance". Change that to "Run a new instance in parallel" or "Stop the existing instance" based on what you actually need to be done.

I know it's an old thread and multiple solutions are good here, this is just what worked for me. Hope it helps.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
octafbr
  • 136
  • 1
  • 4
  • Thank you very much! This solved my problem! I had tasks hanging around but not doing anything after my laptop wakes up from sleeping and they were preventing new tasks from starting. I was beating my head on this and never noticed this setting. – Christian Shay Jun 27 '18 at 17:19
  • https://knowledge.broadcom.com/external/article/37223/how-do-i-monitor-windows-scheduled-tasks.html here you can view the logs from event viewr – sudhansu63 May 05 '21 at 17:46
2

For me, the problem was caused by the .bat included a cd to a network drive. This failed, and then the later call to the program in that network drive did nothing.

I figured this out by adding > log.txt in the Add arguments field of the Edit action window for the task.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Gregorio
  • 393
  • 3
  • 9
  • I had to populate the "Start In" option of the task to get the > log.txt file to be created. And even then it was totally empty. – Matthew Oct 09 '18 at 23:56
2

My application failed to start via "Task Scheduler".

The error in "Event Viewer" is: System.IO.DirectoryNotFoundException

The "Task Scheduler" tries to run this application with the "SYSTEM" user. The problem was that a "network drive" was not mapped for the "SYSTEM" user. So what I did was, I created a ".bat" file and mapped the "network drive" before starting the application:

net use T: \\172.20.2.215\images
cd C:\MyApplication
start MyApplication.exe

So check your logs first: "Event Viewer" -> Windows Logs -> Application

Tadej
  • 553
  • 7
  • 16
1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ljuba
  • 19
  • 1
1

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)"
Pang
  • 9,564
  • 146
  • 81
  • 122
1

I have another reason, why script files (or maybe other types, too) are not run in the Task Scheduler: Unsuitable characters in the filename! Some characters are absolutely valid in the scope of a Windows filesystem, but disliked by the Task Scheduler. My filename used brackets like this: "Cert(renew).cmd", which refused to run. "Cert_renew.cmd" did run!

Satria
  • 315
  • 2
  • 9
0

For me it was trigger issue. By default it should On a Schedule in trigger tab. I had selected At log on and then I was waiting to run task. As it's name says at log on, means you have to logout and log on.

Try putting it on a Schedule and fire it every minute.

enter image description here

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50
0

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.

Matthew
  • 1,630
  • 1
  • 14
  • 19
0

Configuration that worked for me:

  • In General tab: mark radio button - "Run only when user is logged on" <= important !
  • Program/script: just the path to script without quotes or nothing: C:/tools/script.bat
  • Add arguments: kept it empty
  • Start in: kept it empty

In settings, only 2 checkboxes marked:

  • Allow task to be run on demand
  • if the running task does not end when requested, force it to stop
Adir Dayan
  • 1,308
  • 13
  • 21
0

This is a old problem and this problem even exists in 2022 in Windows 10 and Windows 11!

I'm using Windows 10 Pro and therefore want to give an easy and a non technical way to all the users who want to go with the no-brainer/easy way to execute batch files on windows startup.

Please note this method is valid only for the users who want to auto execute the batch file(s)/script(s)/executable(s) only after they login to windows.

Please follow the steps given below

  1. Go to Run command and type shell:Startup Windows Shell Startup Command

  2. Now Windows will open the folder of startup. Copy your batch/executable file here in this folder as shown below Windows Startup Folder Location

  3. Give this file all file user persmissions (though this is not required but just to be fail safe!)Give User All File Permissions

  4. Restart the system and you'll now see your batch/executable file(s) executing automatically after you login Sample of Command Line Executing after startup

vibs2006
  • 6,028
  • 3
  • 40
  • 40
0

The running path of the .bat script can be set using the following code at the beginning of the script

pushd "F:\project1"

Ruzaik Nazeer
  • 474
  • 2
  • 12
0

WIN 10: the main cause of the whole damn thing not working is the option "execute even if user is not logged in". It is the main culprit after trying so many solutions...

Once you put it on "only execute when user is logged in" the bat just works... not special hassle. Just do the auto search the file and hop, it works. No special command needed like many mentioned above. Just "C:\yourbat.bat" done.

Ofc I didnt find a solution to start the bat without being loggin in : /

Hara
  • 11
  • 4