简体   繁体   English

Windows 任务计划程序不启动批处理文件任务

[英]Windows Task Scheduler doesn't start batch file task

I have a batch file with the code below to stop and start the SQL Report service:我有一个包含以下代码的批处理文件来停止和启动 SQL 报告服务:

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.我已将计划任务设置为每天运行,它当前以具有最高权限集的 SYSTEM 运行。 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?有人可以指导我了解我所缺少的吗?

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'.如果您的文件地址是:C:\Temp\foo.bat,请将“start in”选项设置为“C:\Temp”,将“Program/script”选项设置为“foo.bat”。

To set the 'Start in' option: Right click task in the task scheduler > Properties > Actions > Edit.要设置“开始于”选项:右键单击任务计划程序中的任务> 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).如果仅此一项不起作用,请尝试将 .bat 文件移动到具有基本权限的目录(例如,可能是共享目录)。

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.我遇到了一个问题,我的.bat 文件位于一个文件夹中,该文件夹具有一些限制性权限,因此只有我的用户帐户才能访问它。 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.将 .bat 文件移动到另一个目录可以解决问题。

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 命令到批处理文件所在的位置,看看它是否解决了问题。

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.提示:请使用绝对路径,理想情况下相对路径不应该成为问题,但调度程序很难理解它们。

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.在程序中添加了C:\Windows\system32\cmd.exe并将/c myscript.bat添加到 arguments 字段中。

This is what worked for me -这对我有用 -

Program/Script Field - cmd程序/脚本字段 - cmd

Add Arguments - /c myscript.bat添加 Arguments - /c myscript.bat

Start In: Path to myscript.bat开始于:myscript.bat 的路径

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.希望您现在已经想通了,但我想在这里为下一个在这方面浪费时间的人注册它。

I was running this on a Windows Server OS.我在 Windows 服务器操作系统上运行它。 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.并且 my.bat 文件在网络上。

在此处输入图像描述

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

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.有同样的问题,请确保您选中“仅在用户登录时运行”,至少这就是让我的 bat 文件再次活跃的原因。

On a Windows system which supports runas .在支持runas的 Windows 系统上。 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到您希望任务启动器启动程序的路径并键入完整的 arguments,例如。

cd D:\Scripts\ , then execute cd D:\Scripts\ ,然后执行

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.任何被任务调度程序抑制的错误都应该出现在命令行 output 中,这将使事情更容易调试。

One solution is you can run your '.bat' file with '.vbs' file and you can run this vbs file in windows scheduler .一种解决方案是您可以使用“.vbs”文件运行“.bat”文件,并且可以在windows 调度程序中运行此 vbs 文件。

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.你可以这样做,我希望它能解决你的问题。

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.任务计划程序未启动任务“\TASK_NAME”,因为同一任务的实例“{34a206d4-7fce-3895-bfcd-2456f6ed6533}”已在运行。

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.希望能帮助到你。

For me, the problem was caused by the .bat included a cd to a network drive.对我来说,问题是由cd网络驱动器的.bat引起的。 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.我通过在任务的编辑操作window 的添加 arguments字段中添加> log.txt来解决这个问题。

My application failed to start via "Task Scheduler".我的应用程序无法通过“任务计划程序”启动。

The error in "Event Viewer" is: System.IO.DirectoryNotFoundException “事件查看器”中的错误是: System.IO.DirectoryNotFoundException

The "Task Scheduler" tries to run this application with the "SYSTEM" user. “任务计划程序”尝试使用“SYSTEM”用户运行此应用程序。 The problem was that a "network drive" was not mapped for the "SYSTEM" user.问题是没有为“SYSTEM”用户映射“网络驱动器”。 So what I did was, I created a ".bat" file and mapped the "network drive" before starting the application:所以我所做的是,在启动应用程序之前,我创建了一个“.bat”文件并映射了“网络驱动器”:

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

So check your logs first: "Event Viewer" -> Windows Logs -> Application所以首先检查您的日志:“事件查看器”-> Windows 日志-> 应用程序

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.更改后,它开始在我的机器上运行。

Try the code below:试试下面的代码:

Batchfile.bat:批处理文件.bat:

cd c:\batchfilepath
net stop "SQL Server Reporting Services (MSSQLSERVER)" 
timeout /t 10
net start "SQL Server Reporting Services (MSSQLSERVER)"

For me it was trigger issue.对我来说,这是触发问题。 By default it should On a Schedule in trigger tab.默认情况下,它应该在触发器选项卡中On a Schedule上。 I had selected At log on and then I was waiting to run task.我选择了At log on ,然后我正在等待运行任务。 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.试着把它放在一个时间表上,每分钟触发一次。

在此处输入图像描述

My problem was caused by OneDrive.我的问题是由 OneDrive 引起的。 OneDrive was syncing the folder my batch file lived in, and that seems to prevent Task Scheduler from executing it. OneDrive 正在同步我的批处理文件所在的文件夹,这似乎阻止了任务计划程序执行它。 (Doesn't anyone at MS test this kind of thing?) (MS 没有人测试这种东西吗?)

Anyway by moving my batch file to a folder that wasn't in OneDrive the batch file could be started by Task Scheduler.无论如何,通过将我的批处理文件移动到不在 OneDrive 中的文件夹,批处理文件可以由任务计划程序启动。

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程序/脚本:只是脚本的路径,不带引号或什么都没有:C:/tools/script.bat
  • Add arguments: kept it empty添加 arguments:保持为空
  • Start in: kept it empty开始于:保持为空

In settings, only 2 checkboxes marked:在设置中,仅标记了 2 个复选框:

  • Allow task to be run on demand允许任务按需运行
  • if the running task does not end when requested, force it to stop如果正在运行的任务在请求时没有结束,则强制它停止

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.我还有另一个原因,为什么脚本文件(或者其他类型)没有在任务计划程序中运行:文件名中的字符不合适,某些字符在 Windows 文件系统的 scope 中绝对有效。 but disliked by the Task Scheduler: My filename used brackets like this.但任务计划程序不喜欢:我的文件名使用了这样的括号。 "Cert(renew),cmd". “证书(更新),cmd”。 which refused to run.拒绝运行。 "Cert_renew cmd" did run “Cert_renew cmd”确实运行了

This is a old problem and this problem even exists in 2022 in Windows 10 and Windows 11这是一个老问题,这个问题甚至存在于 2022 年的 Windows 10 和 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.我正在使用 Windows 10 Pro,因此希望为所有想要 go 的用户提供一种简单且非技术性的方法,并在 Z0F4137ED1502B5045D6083CAA2 启动时执行批处理文件。

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.请注意,此方法仅对希望在登录 windows 后自动执行批处理文件/脚本/可执行文件的用户有效。

Please follow the steps given below请按照下面给出的步骤

  1. Go to Run command and type shell:Startup Go 运行命令并输入shell:Startup Windows Shell 启动命令

  2. Now Windows will open the folder of startup.现在 Windows 将打开启动文件夹。 Copy your batch/executable file here in this folder as shown below将您的批处理/可执行文件复制到此文件夹中,如下所示Windows 启动文件夹位置

  3. Give this file all file user persmissions (though this is not required but just to be fail safe )给这个文件所有文件用户权限(虽然这不是必需的,但只是为了安全起见) 授予用户所有文件权限

  4. Restart the system and you'll now see your batch/executable file(s) executing automatically after you login重新启动系统,您现在将看到您的批处理/可执行文件在您登录后自动执行启动后执行的命令行示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 任务计划程序不会正确执行批处理文件 - Task Scheduler doesn't execute batch file properly Chrome无法从Windows Task Scheduler中的批处理文件打开 - Chrome won't open from batch file in Windows Task Scheduler 批处理文件在Windows Task Scheduler中不起作用 - Batch File not working in windows task scheduler 运行Windows批处理文件的任务计划程序 - Task Scheduler running a Windows Batch file 批处理文件未通过Windows 7 Task Scheduler运行 - Batch file is not running through Windows 7 Task Scheduler 使用 Windows 任务计划程序运行批处理文件 - Run a batch file with Windows task scheduler Windows Service-在Windows 2012 Server R2上使用任务计划程序使用批处理文件启动服务 - windows service - start service using batch file using task scheduler on windows 2012 server R2 如何在 Windows 8 中启动使用任务计划程序最小化的批处理文件? - %comspec% 方法在 Windows 7 之后不再起作用 - How to start a batch file minimized with task scheduler in Windows 8? - %comspec% method not working anymore after Windows 7 如何使用批处理文件设置Task Scheduler Start in(可选)属性 - How to set task scheduler Start in(Optional) property using Batch file 使用任务计划程序运行包含start / w命令的批处理文件 - Using Task Scheduler to run a batch file that contains the start /w command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM