简体   繁体   English

Cygwin .sh 文件作为 Windows 任务计划程序运行

[英]Cygwin .sh file run as Windows Task Scheduler

Having issues getting this shell script to run in windows task scheduler.让这个 shell 脚本在 Windows 任务调度程序中运行时遇到问题。

#!/bin/bash
# Script to ping the VPN server for testing

RESULT=$(ping 192.168.1.252 | grep "Lost" | awk {' print $10 '})
LOG=/home/admin/results.txt

if [ "$RESULT" -gt 0 ];then

    echo "VPN 192.168.1.252 NOT pinging" >> $LOG
else

echo "VPN Online"

fi

When I run it in cygwin, it runs with no issue, but when I attempt to run it from command prompt, I get the following:当我在 cygwin 中运行它时,它运行没有问题,但是当我尝试从命令提示符运行它时,我得到以下信息:

C:\cygwin64\bin>bash test.sh test.sh: line 4: grep: command not found C:\cygwin64\bin>bash test.sh test.sh: line 4: grep: command not found

test.sh: line 4: awk: command not found test.sh:第 4 行:awk:找不到命令

test.sh: line 7: [: : integer expression expected test.sh:第 7 行:[: : 预期的整数表达式

My question is, how do I get it to run with bash instead so that it actually knows the grep and awk commands?我的问题是,如何让它与 bash 一起运行,以便它真正知道 grep 和 awk 命令?

In Windows Scheduler, I have Action: Start A Program Details: C:\cygwin64\bin\bash.exe Argument: test.sh Start in: C:\cygwin64\bin在 Windows 调度程序中,我有操作:启动 A 程序详细信息:C:\cygwin64\bin\bash.exe 参数:test.sh 开始于:C:\cygwin64\bin

Am I missing something?我错过了什么吗?

I figured it out.我想到了。

In the Windows Task Scheduler, I had to pass:在 Windows 任务计划程序中,我必须通过:

Program/script: C:\cygwin64\bin\bash.exe程序/脚本:C:\cygwin64\bin\bash.exe

Add arguments: -c -l test.sh添加参数:-c -l test.sh

Start in: C:\cygwin64\bin开始于:C:\cygwin64\bin

In correction to what Jimmy found:纠正吉米的发现:

Add arguments: -c -l "c:/FileFolder/test.sh"添加参数:-c -l "c:/FileFolder/test.sh"

You don't need the Start in argument anymore.您不再需要 Start in 参数。

For the longest time I was experiencing the same issue as the OP: command not found errors when trying to run a shell script from Task Scheduler or the Command Prompt, despite the fact that running the same script from a Cygwin terminal worked fine.很长一段时间以来,我遇到了与 OP 相同的问题:尝试从任务计划程序或命令提示符运行 shell 脚本时command not found错误,尽管从 Cygwin 终端运行相同的脚本工作正常。

After some more research I eventually realised that the reason was because my usual Bash PATH ~/.bashprofile wasn't being loaded, and that I needed to use Windows' Environment Variables window to add C:\cygin64\bin to my PATH environment variable (system or user, it doesn't really matter).经过更多研究,我最终意识到原因是因为我通常的 Bash PATH ~/.bashprofile没有被加载,我需要使用 Windows 的环境变量窗口将C:\cygin64\bin添加到我的 PATH 环境变量中(系统或用户,这并不重要)。 This directory contains common system executables like grep and awk , which is why Bash is unable to locate them until the path is added to Windows' PATH.该目录包含常见的系统可执行文件,如grepawk ,这就是为什么 Bash 在将路径添加到 Windows 的 PATH 之前无法找到它们的原因。

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM