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.

Getting Cron to Run in Cygwin

I had problems getting cron to run under cygwin. I could crontab -e and install scripts, but they never actually get run.

The first step was to install cygrunsrv, which appears under the admin section of cywin’s setup.exe package list.

A little searching revealed a utility that will check your set up and install crontab for you. Here’s the complete steps I found by runnning this utility repeatedly and researching the errors it reported:

kato@devbox  ~$ chmod a+r /etc/passwd
 
kato@devbox  ~$ chmod a+r /etc/group
 
kato@devbox  ~$ chmod a+x /var
 
kato@devbox  ~$ cron_diagnose.sh
Do you want to install the cron daemon as a service? (yes/no) yes
 
Enter the value of CYGWIN for the daemon: [ntsec smbntsec] ntsec
 
The service can run either as yourself or under a privileged account.
Running as yourself allows better access to network drives,
but does not allow to run the crontab of other users.
Do you want to the cron daemon to run as yourself? (yes/no) no
 
Running cron_diagnose ...
... no problem found.
 
Do you want to start the cron daemon as a service now? (yes/no) yes
OK. The cron daemon is now running.

In my particular case, I also had to run the following, though take care with these since they replace your /etc/passwd file:

kato@devbox ~$ mkpasswd -l > /etc/passwd
 
kato@devbox ~$ mkgroup -l > /etc/group
 
kato@devbox  ~$ mkpasswd -d -u kato >> /etc/passwd