schedule running a bash shell script in windows
A solution is to associate .sh files with a batch file that runs bash. That way whenever you tell windows to execute an sh file it'll use the correct launcher - whether that's via a double click or a scheduled task. Here's mine:
@echo off
d:
chdir d:\cygwin\bin
bash --login %*
Associating a file type means that when you try to execute a file of that type, windows will use the path to that file as an argument passed to the program you've specified. For example, I have LibreOffice4 associated with .ods files. So if I doubleclick a .ods file, or just enter the path to a .ods file at the command prompt, windows will run open office calc, with the first parameter being the ods file. So if I have Untitled.ods on my desktop. I doubleclick it. That's effectively the same as opening up command prompt, typing
D:\Program Files (x86)\LibreOffice 4\program\scalc.exe" "C:\Users\Adam\Desktop\Untitled.ods".
and hitting enter. Indeed, if I do it, the expected happens: open office calc starts up and loads the file.
You can see how this works if you change the association to echo.exe (which I found in D:\cygwin\bin).
If I change the association to echo, open up the command prompt and type
"C:\Users\Adam\Desktop\Untitled.ods"
I'll just see echo.exe echo the filename back to me.
So what I'm suggesting you do is this:
- create a batch file to run bash scripts using cygwin's bash (or use mine).
- change the association for .sh files to that batch file
- execute those .sh files directly, as though they were .exe or .bat files.
Comments
-
HattrickNZ almost 2 years
Apreciate any help and excuse me if my terminology is incorrect.
this is a script(*.sh file) that:
1-goes to a specific dir A
2-copies files from another dir B to dir A
3-#comented out# it also unzips the files in dir A and its subdirectories
4-#comented out# it also removes rows 1-6 and the last row of all *.csv files in dir A#!/bin/bash # Configure bash so the script will exit if a command fails. set -e #cd to the dir you want to copy to: cd /cygdrive/c/path/I/want/to/copy/to #echo Hello #cp the files i want #include the subdirectories cp -r /cygdrive/c/path/I/want/to/copy/from/* . # This will unzip all .zip files in all subdirectories under this one. # -o is required to overwrite everything that is in there #find -iname '*.zip' -execdir unzip -o {} \; #find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'
Now I can get this script to work in cygwin by going to the dir where the file is stored and giving the following commands:
./filename.sh
or
/cygdrive/c/path/where/the/file/is/filename.sh
or
bash filename.sh
I can also do this in CMD/Windows DOS by doing the following:C:\cygwin\bin\bash.exe -l
to get into a bash terminal and then give the following command:
/cygdrive/c/path/where/the/file/is/filename.sh
In task scheduler(in Windows) I have tried to schedule the following:
C:\cygwin\bin\bash.exe -l /cygdrive/c/path/where/the/file/is/filename.sh
but this does not work, even though the seperate commands work in CMD/Windows DOS as I have said above
Now what I want to do is be able to schedule this script(filename.sh) like I would a .vbs or .bat file in windows using task scheduler? Can anyone advise on this?
Note I have tried to write a Windows batch file(.bat) to do this(see below), but I could not get my unzip and sed commands to work,see here. So I have tried to write the Bash shell script above.
chdir C:\pointA C:\cygwin\bin\cp.exe /cygdrive/v/pointB/* . ::find -iname *.zip -execdir unzip {} \; ::find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'
-
HattrickNZ about 10 yearsthat presents the same problem. I put it at the start of my script and once I double click on the *.bat file I get the CMD/DOS Window with cygwin CLI.does this sound right?
-
HattrickNZ about 10 yearsI have tried Adams suggestion below. but not sure if I am trying it exact right. Could you give a specific example with what I have provided above?
-
Adam R. Grey about 10 yearsNo, associate *.sh with the batch file, then just run your sh file. I'll edit my answer to explain associating a file type.
-
Sebastian Heyn about 10 yearsI'll give it a try in the morning, i have no access to a windows machine right now as all my machines at home are linux ;-)
-
HattrickNZ about 10 yearsgothya tks, also found another way by giving the following command in the run as part of the task scheduler
C:\cygwin\bin\bash.exe -c -l '/cygdrive/c/path/where/the/file/is/filename.sh'
but you cannot double click on the .sh file this way -
HattrickNZ about 10 yearstks Adam but I have just discovered that this does not work for me sometimes, which I have posted here if you would care to comment