You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

计划任务运行批处理文件失败,终端执行正常的问题排查

问题:计划任务执行Git拉取脚本失败,终端运行正常

我编写了一个批处理文件,用于登录时自动从多个目录执行git pull,确保本地代码最新。批处理内容如下:

@echo off
"C:\Program Files\Python311\python.exe" "C:\Users\azure_repo\MIR\MIR2734\file.py"  >> "C:\Users\azure_repo\MIR\log_files\log.txt" 2>&1 

终端运行批处理时一切正常,但设置为登录触发的计划任务后执行失败,日志log.txt报错:

Traceback (most recent call last):
  File "C:\Users\azure_repo\MIR\MIR2734\mir2734_2.py", line 37, in <module>
    print(g.pull())
          ^^^^^^^^
  File "C:\Users\AppData\Roaming\Python\Python311\site-packages\git\cmd.py", line 1003, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AppData\Roaming\Python\Python311\site-packages\git\cmd.py", line 1616, in _call_process
    return self.execute(call, **exec_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AppData\Roaming\Python\Python311\site-packages\git\cmd.py", line 1406, in execute
    raise GitCommandError(redacted_command, status, stderr_value, stdout_value)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git pull
  stderr: 'fatal: not a git repository (or any of the parent directories): .git'

错误提示当前目录不是Git仓库,且计划任务执行时第一个仓库可正常拉取,第二个仓库报错不存在(实际目录存在)。调用的Python脚本内容如下:

# update Stored Procedure and deployed git repos locally
repos = ['ADP_python_scripts', 'ADP_WH_SPs', 'daily_tasks', 'debt_sales', 'deployed_stored_procedures', 'MIR', 'monthly_tasks', 'pbi','sql_views', 'Stored_Procedure']
parent_dir = 'C:/Users/azure_repo/'
for git_dir in repos:
    print(f'{pd.Timestamp.now()}| git pull for {parent_dir + git_dir}')
    g = git.cmd.Git(git_dir)
    print(g.pull())

原因与解决方案

核心原因

  1. 工作目录不一致:终端运行批处理时,你的当前工作目录是C:\Users\azure_repo\,脚本中用相对路径git_dir能正确指向仓库子目录;但计划任务默认工作目录是系统目录(如C:\Windows\System32)或脚本所在目录(C:\Users\azure_repo\MIR\MIR2734),导致相对路径失效。
  2. 工作目录被修改:git.cmd.Git(git_dir)执行pull操作时,会将Python进程的当前工作目录切换到目标仓库目录。第一个仓库执行后,后续仓库的相对路径会基于新的工作目录查找,自然找不到原本同级的其他仓库。

解决方案

方案1:修改Python脚本使用绝对路径

直接将仓库的绝对路径传给git.cmd.Git,避免工作目录变化带来的影响:

# update Stored Procedure and deployed git repos locally
import pandas as pd
import git

repos = ['ADP_python_scripts', 'ADP_WH_SPs', 'daily_tasks', 'debt_sales', 'deployed_stored_procedures', 'MIR', 'monthly_tasks', 'pbi','sql_views', 'Stored_Procedure']
parent_dir = 'C:/Users/azure_repo/'
for git_dir in repos:
    # 拼接绝对路径
    repo_full_path = f"{parent_dir}{git_dir}"
    print(f'{pd.Timestamp.now()}| git pull for {repo_full_path}')
    # 使用绝对路径初始化Git对象
    g = git.cmd.Git(repo_full_path)
    print(g.pull())

方案2:修改批处理文件,先切换到仓库父目录

在执行Python脚本前,先切换到C:\Users\azure_repo\,确保脚本运行时的初始工作目录正确:

@echo off
# 切换到仓库父目录,/d参数确保跨驱动器切换生效
cd /d "C:\Users\azure_repo\"
"C:\Program Files\Python311\python.exe" "C:\Users\azure_repo\MIR\MIR2734\file.py"  >> "C:\Users\azure_repo\MIR\log_files\log.txt" 2>&1 

方案3:配置计划任务的工作目录

在计划任务的「操作」设置中,找到「起始于(可选)」字段,填入C:\Users\azure_repo\,强制计划任务在该目录下执行脚本,确保相对路径有效。


内容的提问来源于stack exchange,提问作者frank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 06:54:51