1

Sometimes my Windows server reboots at night to install new updates. Then the next day I find out that my cygwin instance has been stopped.

What I want is on Windows start, also run Cygwin AND run a specific command.

so, step 1: Right now I have a cygwin.bat to start cygwin:

@echo off
C:
chdir C:\cygwin\bin
bash --login -i

step 2 is to enter the command in the command windows that appears after running cygwin.bat:

cd /cygdrive/e/solr/apache-solr-4.0-2010-10-12_08-05-48/example/;java -Dsolr.solr.home="./example-DIH/solr/" -jar start.jar

But this command is what I want to have called automatically when i run cygwin.bat

How can I combine step 1 and step 2 into a single bat file which I can run on Windows start?

Ben Pilbrow
  • 12,041
  • 5
  • 36
  • 57

4 Answers4

2

You can create a new .bat file that runs on startup, and use the bash -c option to pass commands to bash when you start it. For example:

@echo off
C:
chdir C:\cygwin\bin
bash -c "echo 'it works'; read -n 1 -p 'Press any key to continue...' "

You could also make the changes to your cygwin.bat, but then the commands would run every time you start a shell.

Joe Internet
  • 1,449
  • 8
  • 6
1

I try to keep my windows experience as UNIX like as possible. The first thing I would reach for would be installing cron and using @REBOOT to run a script on startup.

I found a good writeup on installing cron here: https://stackoverflow.com/a/7900158/1607020

Then, make a file in /etc/cron.d/myservicename with

@REBOOT /path/to/bash/script > /tmp/myservicename.log 2>&1 
Dan Garthwaite
  • 2,962
  • 1
  • 19
  • 31
0

I now have this:

@echo off

C:

chdir C:\cygwin\bin

bash -c cd /cygdrive/e/solr/apache-solr-4.0-2010-10-12_08-05-48/example/;java -Dsolr.solr.home="./example-DIH/solr/" -jar start.jar

bash -c "echo 'it works'; read -n 1 -p 'Press any key to continue...' "

But that doesnt seem to start cygwin with the commands I want to...

Ben Pilbrow
  • 12,041
  • 5
  • 36
  • 57
Peter
  • 1
0

I used this method:

  1. On Windows 7, open Task Scheduler (Control Panel -> Administrative Tools -> Task Scheduler)
  2. On the right "Create Task..."
  3. On "Actions" in "Program/script": <letter>:\<path_to_cygwin>\bin\bash.exe (I have D:\Apps\cygwin64\bin\bash.exe)
  4. On "Add arguments (optional)": -c "<anything you want to execute inside bash>" (I have -c "sleep 60; while true; do /cygdrive/d/xampp/scripts/daily_work_no_wait.sh >> /cygdrive/d/xampp/monitor/daily_work_no_wait.out.txt ; done" with quotes)
Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47