The Blog

Cygwin

Running Cygwin Scripts as Scheduled Tasks in Windows

Posted on

It isn’t always necessary to run Cron as a service to schedule Cygwin scripts and programs. As an alternative to using cron, you can run cygwin scripts & programs using Windows Scheduled Tasks.

From the Scheduled Tasks Window, right-click on an empty space and select ‘new task’. Enter the path to Cygwin, -l, -c, and then the full command surrounded by quotes in the RUN field. e.g.

c:\cygwin64\bin\bash.exe -l -c "/full/path/to/command/plus-command >> /full/path/to/a/logfile 2>&1"

Then, in the START IN field enter:

c:\cygwin64\bin

Add a description and give your task a meaningful name so that when you come back to it in a year you know what it’s for and you’re done!

 

Breaking this down:

  • -l :Run as if logged on at a shell
  • -c :Run this command
  • >> logfile :Put any output into a text file for troubleshooting
  • 2>&1 :Redirect stderr to stdout (i.e. your logfile – since you’ve already redirected stdout to /full/path/to/a/logfile)

Quick and easy!

Posted in Cygwin Leave a comment

Cygwin cron setup

Posted on

Use Below command to setup cron as service

$ cron-config
Do you want to remove or reinstall it (yes/no) yes
Do you want to install the cron daemon as a service? (yes/no) yes
Enter the value of CYGWIN for the daemon: [ ] ntsec
Do you want the cron daemon to run as yourself? (yes/no) no
Do you want to start the cron daemon as a service now? (yes/no) yes

Next use crontab to create the sceduled task

$ crontab -e
0 0 * * * rsync -avz --delete --password-file /etc/rsync.pwd /cygdrive/g/www/ rsync://[email protected]:/Public/www

This syncs my laptop with my NAS every day at midnight. I also have a task in my windows Task Manager to wake the laptop from sleep at that time to do the sync. So I can sleep, without worry.

Posted in Cygwin Leave a comment