Run Cygwin script on shutdown or startup - windows

I'm extremely new to Cygwin but I am somewhat comfortable in Linux (I can read man files fine).
I want to create a BASH script using Cygwin that deletes the files in a folder on the shutdown signal given by Windows. If this can't be done, I also could try deleting the files in the same folder on startup. I installed CRON but does CRON only works for scheduled tasks, rather than on 'signals'? Answers would be nice but a general idea of how to proceed would be even better!
I can write the script. I just don't know exactly how Cywgin interacts with the Windows OS in order to perform these procedures.
Another question, how do I run CRON on Windows startup?
If it matters, my O.S. is Windows 10 x64 running Cygwin.

Cygwin.bat, a batch file which was installed under cygwin installation folder will give you hint of how to run cygwin script.
The script contains just:
C:
chdir C:\cygwin64\bin
bash --login -i
to run the bash shell interactive.
Make a copy of Cygwin.bat with another name (Startup ?) and change last line in
bash --login path_to_your_script_here
Put the bat file or a link to in in the Startup folder.

Great thread over here: https://serverfault.com/questions/245945/autostart-cygwin-on-windows-boot-and-run-a-cygwin-command
tl;dr
you can put command directly:
#echo off
C:
chdir C:\cygwin64\bin
bash -c "/usr/bin/whatever"

Related

How to run zookeeper.sh file in windows

I am following this tutorial where i have to run this command in order to start the zookeeper server.
./bin/zookeeper-server-start.sh config/zookeeper.properties
But the problem is this command is not working properly. I found that .sh file is bash file that required cygwin. I have installed it and then run command like this
C:\cygwin64\bin\bash.exe ./bin/zookeeper-server-start.sh config/zookeeper.properties
But it is showing:
I can confirm that in bin directory the file is exsits. what i am doing wrong?
Here is my directory snapshot from where i running the command:
Note: I have successfully tested bin/windows zookeeper bat file but i want to run it through .sh file as the kafka security tutorial which i am following using this.
From your screenshot, I conclude that you are using Cygwin. So, please add the cygwin tag to your question.
As you can see from the error message, the command dirname is not found by bash, so assuming that your Cygwin installation is not broken, I assume that the PATH is not set correctly; in your setup, dirname.exe should be in C:/cygwin64/bin (please verify this).
Your usage of bash.exe is a bit unusual in that you run it directly from a Windows cmd prompt. The more common way would be to use it from the 'Cygwin Terminal', which you get created a Windows-link to, when installing Cygwin, or to use another suitable Terminal program; I'm using for instance mintty for this task (also available via the Cygwin installer).
Having said this, it is possible to run bash.exe in the way you are doing it, but you then have to ensure, that at least the PATH is set up correctly. One possibility to do this, is to add C:\cygwin64\bin to your Windows PATH, but this has the drawback, that some commands have the same name in the Windows world and in Cygwin, though they serve a completely different purpose, and this will bite you sooner or later. Another problem is that at some point, you will rely on other bash specific setups besides the PATH.
A better way to accomplish your goal is IMO to ensure, that the system wide bash-initialization files are sourced by bash. If I have to run the script from a Windows cmd prompt, I would run it by
C:\cygwin64\bin\bash.exe --login YOURSCRIPT
This will read the file (in your setup) C:\cygwin64\etc\profile before running YOURSCRIPT, so you can check, that the PATH is correctly set there, by looking at this file. In a default installation, this should be the case.
After having read this file, it will try to read the file .bash_profile in your Cygwin HOME directory, so if you need additional settings for your (non-interactive) bash-scripts, create this file and put your settings there.

How do I write a batch file that opens a msys shell and then run commands in the shell?

I'm trying to automate the process of building ffmpeg on Windows 10. I'm following the guide here: https://trac.ffmpeg.org/wiki/CompilationGuide/MSVC
Everything works fine when I do it manually, however I want to write a batch file that I can run to go through the entire process automatically.
Building requires me to set up the Visual Studio environment and the MSYS environment. This is where I'm having trouble, since running the MSYS environment opens up a new shell. I want to pass the configure/make/make install commands to the MSYS shell after it is opened.
I've tried the solution here: How to open a new shell in cmd,then run script in a new shell?
The problem they had looks similar to mine, but the solutions posted there didn't work for me.
Here is the bat file currently:
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\vcvars64.bat"
call "C:\workspace\windows\mingw-get\msys\1.0\msys.bat" start cmd.exe /k bscript
pause
and bscript:
./configure --enable-shared --toolchain=msvc --arch=amd64
make
make install
I've tried all sorts of variations like:
call "C:\workspace\windows\mingw-get\msys\1.0\msys.bat" /k bscript
call "C:\workspace\windows\mingw-get\msys\1.0\msys.bat" bscript
start "C:\workspace\windows\mingw-get\msys\1.0\msys.bat" /k bscript
start "C:\workspace\windows\mingw-get\msys\1.0\msys.bat" bscript
And I've also tried leaving the bscript code in the original batch file.
The configure/make commands will either run in the original cmd window, a new cmd window or wont run at all.
Is there a way to pass commands to the MSYS shell like that?
This might be considered somewhat of a late answer but in the spirit of helping out those who come along here in the future:
The MSYS2 documentation contains a page specific to launching MSYS2: https://www.msys2.org/wiki/Launchers/
From there, we learn that one can launch an MSYS2 environment from a Windows shell like this:
C:\\msys64\\usr\\bin\\env MSYSTEM=MSYS /usr/bin/bash -li
If you place this in a *.bat file and execute the script, a new terminal window will spawn with bash running under the MSYS2 environment.
The documentation further illustrates how to run something within that bash shell:
C:\\msys64\\usr\\bin\\env MSYSTEM=MSYS /usr/bin/bash -lc python
The above would again spawn a new terminal window, load the MSYS2 environment, launch bash and then run the python executable in that bash instance.
From here, either directly run the program you want to (instead of python) or create a bash script and pass the parameter to that script to the bash invocation to execute a regular bash script within the MSYS2 environment from a Windows Batch file :)
I'm not sure if you get the result.
you can run command in windows console:
"C:\msys64\msys2_shell.cmd -mingw32 -shell test-script"
Command & paramerter comment:
C:\msys64\msys2_shell.cmd : msys launching bat
-mingw32 : arch, I use the 32bit
test-script : the startup script in /usr/bin
And you need to set the $PATH in startup script to launch your command

bash -c from bat file spawned from exe not a command

When I run a batch file that runs bash -c (part of windows subsystem for linux/Bash on ubuntu on windows) as a child process of an exe (pidgin), even if the exe is elevated/administrator in Windows the batch file errors:
'bash' is not recognized as an internal or external command,
operable program or batch file.
The .bat file that I am running is:
bash -c "curl --silent -u '''my api key'':' -d type='note' -d body='My Message' -d title='My Subject' 'https://api.pushbullet.com/v2/pushes'"
pause #disable after debugging
What I intend to do is have it as a 'buddy pounce' in pidgin so that when I get a message from Nickserv, it will notify me everywhere.
I also tried to execute the command from pidgin directly rather than running as a bash file, but the curl never happens, and I don't get a notification through pushbullet. But if I run the exact same command in CMD or in Run, it will use bash and execute the curl successfully.
I also found this question: Calling Windows subsystem for Linux apps through PowerShell/cmd but I'm not sure if it answers that this cannot be done because you clearly can put a bash command in a bat file.
Have you tried fully specifying the path to the bash shell in your batch file?
If you replace the bash command in your curl.bat file with c:\Windows\System32\bash.exe things may work a little better. Seems like pidgin doesn't have your full path environment variable available to the process it spawns.
Change the path to C:\Windows\sysnative\bash.exe. Pidgin is 32-bit so it aliases C:\Windows\System32 to C:\Windows\SysWOW64 and C:\Windows\sysnative to C:\Windows\System32. It would probably make sense for MS to symlink a bash.exe in SysWOW64 to the real one.

executing a custom init script for bash --login -i for example to change to a custom directory from a shortcut

Right now I'm using MSysGit on Windows 7, which is started from a .bat file, which itself calls bash.exe --login -i to start a shell. At which point it executes the .bashrc file (among others) in the user's home directory. I use this script to setup the environment and cd to a starting directory.
This all works fine. What I would like is to change the .bat file in some way so that bash will execute a custom script at startup so that in that script i could perform a different initialization and cd to a different starting directory. Then I could have two separate .bat files calling each script, then I could make a shortcut to both on the desktop and start whichever I want.
The thing that I'm not sure how to do, is get bash to run a custom init script on startup. Currently the command MSysGit uses is bash.exe --login -i. Is there any way I can modify this to get it to use a custom file? I tried bash.exe --login --rcfile 01.txt -i but that didn't work. Likewise nothing else I tried worked either.
Try it without --login:
bash.exe --rcfile 01.txt -i

Why is it that Cygwin can run .bat scripts?

When I execute a .bat script from bash in Cygwin, by what mechanism is it running? I understand that if I run a .EXE it will launch, regardless of whether the .EXE is from Cygwin or from a more traditional environment. I understand that when I execute an executable script with #! at the beginning that Cygwin supplies the magic for it to run.
But why does a .bat script work? Is there some component inside of Cygwin that is aware of what a Windows .bat script is and what to do with it? Or is it that it is somehow impossible under Windows to execute a call to launch a .EXE file that won't automatically also work for a .bat script instead?
Running
./test.bat params
from bash seems to be equivalent to
cmd /c test.bat params
I believe that bash in cygwin sees the bat extension as being flagged executable (a cygwin hit-tip to windows convention). As such it loads and executes the file with it's associated interpreter (cmd.exe, per os configuration), much as it creates a new instance of bash to run your #! scripts (per posix standard).
And if you want to fork an *.cmd file execution like a ShellScript process and append his log to an file:
cmd /c test.bat > nohup.out &
Enjoy!

Resources