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

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

Related

Running shell script on Windows via Cygwin mintty.exe doesn't execute runcoms

Problem summary
I'm trying to launch a .sh script via Windows 10 Cygwin (i.e. mintty.exe > bash.exe) but neither .profile, .bash_profile, or .bashrc are loading, which I need to update PATH env variable with Cygwin's bin directory.
Background
I'm trying to launch a script finder.sh:
#!/bin/bash
find .
read
from C:\Users\Bo\Temp\. It has unix line endings and executable bit set.
I have Cygwin installed at C:\Users\Bo\AppData\Local\Programs\cygwin64\. I do not have this path in either System or User Windows' Environment Variables (and I don't want to!). My runcoms all live in this directory under /home/Bo. My .bash_profile (and ATM .bashrc) have an export PATH="/cygdrive/c/Users/Bo/AppData/Local/Programs/cygwin64/bin":${PATH} in them.
I want to launch the script from Windows Explorer. I tried using the bash.exe and mintty.exe in the cygwin64\bin\ folder via Open > Choose another app > More apps > Look for another app on this PC. In either case the mintty window displays:
FIND: Parameter format not correct
meaning the Windows' find command was used not Cygwin's. So I have my script echo $PATH and the Cygwin/bin directory is not in PATH. If I add the proper export PATH statement from above to my own script it works fine. So, now to debug the launcher and runcoms...
I've put echo ${0} statements in .profile, .bash_profile, and .bashrc, none of which trigger which I run the .sh script, they are never run. I've read SO and the mans. I've tried creating a Shortcut to both mintty.exe and bash.exe passing a variety of -l -i -e - commands to each using Properties > Shortcut > Target and they are never run. E.g. running simply [..]\mintty.exe -h always doesn't even leave the window open.
How do I get my script to run in Windows Explorer via Cygwin's mintty.exe/bash.exe, and to read from a runcom to update PATH (to find Cygwin Linux commands, vs. updating Windows Environment Variable)?
Two part fix:
A) set a Windows Environment Variable for BASH_ENV to a .bash_env under your Cygwin HOME, and export the PATH variable to include the Cygwin/bin directory from that file. I cannot find a decent reference for this in Cygwin documentation because it seems to be simply a bash thing, but this variable is what bash looks for when running non login/interactively. Best reference: Cygwin shell doesn't execute .bashrc.
And B) run the .sh with bash.exe from Cygwin/bin using Open With....
ALSO, annoying Windows bug: when you select a program to Open With... your .sh script, it will always run 1x with a CWD from your C:\Windows\System32 directory(?!) and all other times will run fine with the CWD as the directory from your .sh script.

How to create a .bat file which execute python file

I'm new to bat file and started to implementing it. I have a list of linux application commands which starts my application. I have a windows system to deploy, used to git bash to execute those commands, but in every system restart have to start the application manually so I started implementing bat file which mapped in system start up
#echo off
title ML_autostart_API
start "" "C:\Program Files\Git\git-bash.exe"
using the above script I've opened the git bash. In further need to perform below commands
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
have to execute the above using git bash since it is open source commands and doesn't execute in windows. git bash is opening and further commands is not working.
You will need to make 2 files, one for the windows command line (.bat) another for the bash script (.sh). The reason being, after you start the bash console, it will work on different window and it has no idea what your .bat contains. We shall call our scripts as boot.bat and start.sh respectively.
boot.bat
#echo off
title ML_autostart_API
start "C:\Program Files\Git\git-bash.exe" start.sh
Notice the start.sh is added at the end of the start command as parameter.
start.sh
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
Note
Both scripts are in the same directory.
This answer assumes python is actually recognized in git-bash paths.
Should this is not the case, you can just use the full path to the executable to call it.
A better alternative would be to just execute the bash script directly on start up (using that start "C:\Program Files\Git\git-bash.exe" start.sh), no mixing stuff.

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.

Run Cygwin script on shutdown or startup

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"

cywin bash script command not found when called from batch

#!/bin/bash
echo "Testing"
cd "/cygdrive/x/Internal Communications/Riccardo/"
filename=htdocs-`date +%A`.tar.gz
tar cvzf $filename "/cygdrive/c/Program Files/Zend/Apache2/htdocs"
The above script is working when it is called inside cygwin console, but when I try to call it from a batch file I get "command not found" for date and tar command. I think that bash.exe does not have the PATH set up.
I need to run that script from that batch file because I want to add the script to the task scheduler.
As has already been said, you need to add the Cygwin binaries to your path. To do so, right click on "My Computer", click "Properties", then "Advanced", then "Environment Variables".
Create a new environment variable with name "CYGWIN_HOME" and value "C:\cygwin" (or wherever you installed cygwin. The default location is "C:\cygwin\" so this should probably work for you).
Then edit the environment variable named "PATH", and tack on the following to the end:
;%CYGWIN_HOME%\bin;%CYGWIN_HOME%\sbin;%CYGWIN_HOME%\usr\bin;%CYGWIN_HOME%\usr\sbin;%CYGWIN_HOME%\usr\local\bin;%CYGWIN_HOME%\usr\local\sbin
Close your command prompt, then reopen it. The cygwin binaries should now be available. You can double-check this by typing "which bash". It should report the location of your bash executable.
FWIW, Cygwin has cron.
Are you calling your script like this?
bash --login -i ./myscript.sh
Put your cygwin bin directory (likely C:\cygwin\bin) on your PATH environment variable.
This would also give you the benefit of being able to use commands like tar, ls, rm, etc. from a regular console windows and not just a Cygwin console.
If this script is invoked from a Windows command shell, the first line will result in an error since #!/bin/bash is not a recognized Windows command and # is not a valid comment delimiter in a batch file.
So, the bottom line is that this script runs as a regular batch file rather than from within Cygwin's bash. As noted by matt b, you likely do not have the Cygwin executable path in your PATH environment variable. Without this, the batch file cannot find the Cygwin utilities (tar and date).
I just had this problem.
Editing the environment variable works great. But if you are have no admin rights you canĀ“t do that.
In this case you can execute your commands by using the absolute path like:
/usr/bin/tar cvzf $filename
/usr/bin/cat $filename
If you do so your bash script works even if you call it from a batch file.

Resources