Cygwin .sh file run as Windows Task Scheduler - windows

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?

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

In correction to what Jimmy found:
Add arguments: -c -l "c:/FileFolder/test.sh"
You don't need the Start in argument anymore.

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.

Related

How to run bash commands from pwsh?

We have a powershell script which is 99% cross-platform but occasionally we need an IF LINUX THEN branch because of how different windows and linux service management is.
We would like to run the kill command from bash but this is an alias of the powershell Stop-Process.
How do we run native bash commands like ps, kill and ls from Powershell.
Note sh ps or bash ps do not work.
PS > bash ps
/usr/bin/ps: /usr/bin/ps: cannot execute binary file
Assuming that running bash runs bash from pwsh, you would want bash -c "ps". Normally the argument to bash would be a script that it tries to execute, hence the error "cannot exxecute binary file". ps is not a bash script, but an executable binary. The -c on the other hand runs arbitrary bash code provided as a command line argument, which can obviously run programs like ps.
If you know the location of the command you can just run it:
/usr/bin/kill
Usage:
kill [options] <pid|name]...
...
Otherwise which can find it and run it with Invoke-Expression or iex:
which kill | iex
This can get tricky since which could return multiple lines which you then would have to guess and just take the first one. You also need to somehow add parameters (e.g. 123) to your command:
which kill | select -first 1 | % {iex "$_ 123"}
kill: sending signal to 123 failed: No such process
Had lots of trouble running ant -version from pwsh but this works:
Invoke-Expression "/bin/bash ant -version"
Cross-Platform Function
function RunCommand($Command) {
if($env:OS -eq 'Windows_NT') {
CMD /c $Command
} else {
Invoke-Expression "/bin/bash $Command"
}
}

Shell- not able to produce log output of script

I am trying to run following command
ssh xxx#99.99.99.99 ". ./.profile; myscript 2&1 >> /tmp/2244455.log"
But it comes up with following error
sh: 1: execute permission denied
When i run myscript 2&1 >> /tmp/2244455.log on my remote server it works perfectly.
Also when i run ssh xxx#99.99.99.99 ". ./.profile; myscript it works perfectly.
Please can yo help me with this issue
myscript is not owned by xxx, permissions are
ls -ltrh myscript
-rwxr-xr-x 1 yyy other 11K May 18 15:04 myscript
The 2&1 syntax is wrong; if you want to redirect stderr to stdout, you need to:
2>&1
You also have quotes that are not nested properly; the backtick `` is overlapped with the double quotes. EDIT: I see you've edited the question, but there's now an unpaired backtick at the beginning of your command
So, I'm guessing what you're after with the quotes, but your whole command might be:
ssh xxx#99.99.99.99 ". ./.profile; myscript 2>&1 >> /tmp/2244455.log"
This would create /tmp/2244455.log on the remote 99.99.99.99 machine.
Putting backpacks around the ssh command will cause your local shell to attempt to run its output as a new command line. Get rid of those, if that's not just a typo from formatting your question.
The real problem is that you're missing the > on the redirect. 2>&1, not 2&1. The >less version just adds 2 as an argument to the command and then attempts to run that command in the background while running a command named 1 in the foreground.

How to run shell script as a client side hook script for tortoiseSVN?

I have written the shell script and i am trying to put that script as a client side hook script but not getting the script engine which one i should be using to run .sh file.Usually as i have seen .js file will be used as hook script for SVN unfortunately i don't know much about jscript so please help me how to add and run the script in SVN as client side hook script.I have tried using WScipt and CScirpt but both of them are not working for my shell script.
#!/bin/bash
MAIN_DIR="/cygdrive/e/Trunk/COMMON"
FILE_NAME="/cygdrive/e/Trunk_PRE_COMMIT_HOOK/long_path.txt"
lengthy_path=`find ${MAIN_DIR} -regextype posix-extended -regex '.{500,}'| awk -F'Trunk/' '{print $2}' > ${FILE_NAME}`
if [ -f ${FILE_NAME} ]
then
if [ -s ${FILE_NAME} ]
then
echo -e "\n\n\nSorry the path of a file exceeds 256 charectors, please make it shorten and try commiting again.You can see the path in $FILE_NAME"
else
echo -e "\n\n\nPath is perfect code can be committed..........."
fi
else
echo -e "\n\n\nFile not exists............"
fi
You're trying to execute a bash script on Windows, which means you either need Cygwin installed or can use the new bash shell functionality in Windows 10. I have little experience with either, but hopefully I can get you pointed in the right direction.
If you're using Cygwin, use the following command in the Tortoise hook script configuration dialog (Fig. 4.87 in the documentation):
C:\cygwin\bin\bash C:\path\to\your_script.sh
(Sourced from this answer)
If you're using the Windows 10 bash shell, use this command:
bash -c "/mnt/c/path/to/your_script.sh"
(Sourced from this page under "Run Linux Commands From Outside Bash")
Disclaimer: I haven't tested either of these because I don't have the time or means. Try it out, and leave some feedback either way.

How do I call Ruby script from a shell script?

I am trying to write a watchdog for a Ruby application. So far, I have a cron job which is successfully calling a shell script:
#!/bin/sh
if ps -ef | grep -v grep | grep adpc.rb ; then
exit 0
else
NOW=$(date +"%m-%d-%Y"+"%T" )
echo "$NOW - CRITIC: ADPC service is down! Trying to initialize..." >> che.log
cd lib
nohup ruby adpc.rb &
exit 0
fi
This code runs correctly from command line, but I am not able to make the shell script execute the Ruby script when called from a cron job.
Please any help would be appreciated.
The Ruby file has +x permissions.
The nohup.out file is empty.
Solution: replace bare "ruby" command with full path (which ruby output).
Thanks to all for the replies =)
This is usually caused by an incorrect environment. Check the Ruby output in the created nohup.out file and log the stderr of nohup itself to a file.
It's frequently solved by starting the script with:
#!/bin/bash
source ~/.bash_profile
source ~/.bashrc
This will ensure that you run with bash instead of sh, and that any settings like PATH you've configured in your init files will be set.

running linux executables from linux shell scripts

I want to run a executable from my shell script. The executable is located at /usr/bin/to_run.
My shell script(which is calling the above executable) is in the /usr/bin folder.
The shell script is :
#!/bin/bash
#kill all existing instances of synergy
killall synergys
sh "/usr/bin/synergys"
if [ $? -eq 1 ]; then
echo "synergy server started"
else
echo "error in starting"
fi
I am getting an error saying : "synergys : no process found".
When I run the same thing - /usr/bin/synergys directly from the terminal it runs fine, but from within a script there are problems. I don't understand why.
Thank you in advance.
That error is from the killall command, it's saying there are no candidate processes matching your argument.
If you don't want to be notified where no processes match, just use the quiet option:
killall -q synergys
From the killall man page:
-q, --quiet
Do not complain if no processes were killed.
If /usr/bin/synergys is an executable and not a shell script, you will run it directly, not via the shell:
/usr/bin/synergys
Or, since /usr/bin is on the $PATH of most people, you could simply write:
synergys
If /usr/bin/synergys is actually a shell script, it should be executable (for example, 555 or -r-xr-xr-x permissions), and you can still write just synergys to execute it. You only need to use an explicit sh if the file /usr/bin/synergys is not executable and is a shell script.

Resources