5

Having issues getting this shell script to run in windows task scheduler.

#!/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:

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

test.sh: line 4: awk: command not found

test.sh: line 7: [: : integer expression expected

My question is, how do I get it to run with bash instead so that it actually knows the grep and awk commands?

In Windows Scheduler, I have Action: Start A Program Details: C:\cygwin64\bin\bash.exe Argument: test.sh Start in: C:\cygwin64\bin

Am I missing something?

moo
  • 1,597
  • 1
  • 14
  • 29
Jimmy
  • 887
  • 1
  • 10
  • 24

3 Answers3

10

I figured it out.

In the Windows Task Scheduler, I had to pass:

Program/script: C:\cygwin64\bin\bash.exe

Add arguments: -c -l test.sh

Start in: C:\cygwin64\bin

Jimmy
  • 887
  • 1
  • 10
  • 24
  • Hi there, Maybe you could help me out with a similar issue I had [here](http://stackoverflow.com/questions/21517909/schedule-running-a-bash-shell-script-in-windows) – HattrickNZ Feb 03 '14 at 04:45
  • More specifically, How do I add arguments in the task scheduler? I have the following in my run as `C:\cygwin\bin\bash.exe -l /cygdrive/c/path/where/the/file/is/filename.sh` And I have Start in: `C:\cygwin\bin` – HattrickNZ Feb 03 '14 at 04:51
0

In correction to what Jimmy found:

Add arguments: -c -l "c:/FileFolder/test.sh"

You don't need the Start in argument anymore.

Amir Za
  • 1
  • 2
0

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.

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). 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.

enter image description here

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68