inicio mail me! sindicaci;ón

How to Run a Cygwin Command From Windows Scheduler (Scheduled Tasks)

After repeated problems setting up crond to run in cygwin (it just doesn’t like the user accounts, no matter how enthusiastically I argue that I’m me), I spent some time figuring out how to run a Cygwin command as a scheduled task from Windows.

Based on this mail archive post, I created the following cygrun.bat file:

    @echo off
    rem set HOME=c:\
    if "%DEF_PATH%"=="" set DEF_PATH=%PATH%
    set PATH=c:\cygwin\bin;%DEF_PATH%
    set myargs=%*
    if "%myargs%" == "" goto noarg
    rem echo %myargs%
    bash --rcfile "%HOME%/.bashrc" -i -c "%myargs%"
    c:
    rem pause
    sleep 1
    goto exit
    :noarg
 
    rxvt -e /usr/bin/bash --login -i
 
    :exit
    exit

Then I tested the script from the command line as follows, until I had the syntax just so:

c:
cd \cygwin
cygrun.bat cygwin_script arg1 arg2

Once I was able to run it happily, I added the scheduled task as follows:

    Run: C:\WINDOWS\system32\CMD.EXE /x /c start "Some title" /min
c:\cygwin\cygrun.bat cygwin_script arg1 arg2
    Start In: c:\cygwin     <-- must be a real disk drive and path
    Run as: domain\username

Unfortunately, I was never able to figure out how to redirect stdout and stderr. I tried plenty of variations on “>> /some/path/to/log.txt 2>&1″ with no joy. Instead, I just changed all the commands in the script and added that line on to each echo statement. Sad, but functional.

JP said,

June 16, 2009 @ 12:09 pm

Thank you. This saved me a bit of time trying to figure it out otherwise.

exec >> /pathto/logfile 2>&1 worked for me even using this method.

Ray said,

February 2, 2011 @ 7:39 pm

An easier way is to call bash.exe and pass the script as an argument:

Example I want a scheduled task that backs-up my scheduled tasks…

Create a one-liner in cygwin /home/brightblueray/bin/schedTasksBackup.sh
schtasks.exe /QUERY /XML > /cygdrive/c/Users/rryjew/Documents/ScheduledTasksBackup/schedTasks.xml

Then create a scheduled task calling:
C:\cygwin\bin\bash.exe
… with an argument of my shell script:
/home/brightblueray/bin/schedTasksBackup.sh

XML to import into task scheduler …

2011-02-02T21:02:10.9135301

bbr-laptop\brightblueray

2011-02-02T09:00:00

true

1

LeastPrivilege

bbr-laptop\brightblueray

InteractiveToken

IgnoreNew

true

true

true

false

false

true

false

true

true

false

false

false

P3D

7

C:\cygwin\bin\bash.exe

/home/brightblueray/bin/schedTasksBackup.sh

Dan said,

April 13, 2011 @ 1:32 pm

Thanks, this was very useful and saved time :)

Ocamler said,

April 27, 2011 @ 6:12 am

If your %HOME% directory has spaces in it, you’ll want to modify line 8 to look like this to avoid an error:

bash –rcfile “%HOME%/.bashrc” -i -c “%myargs%”

Awesome bat file, use it every day for my scheduled tasks.

Michael said,

April 27, 2011 @ 6:39 am

@Ocamler – nice! I’ve updated the bash script in the post accordingly.

Diorno said,

November 7, 2011 @ 7:32 pm

Thanks for this, works perfectly to resolve my Windows shell headaches :)

Michael said,

November 7, 2011 @ 9:49 pm

Glad to hear it : )

RSS feed for comments on this post · TrackBack URI

Leave a Comment