Assuming we are going to run a Batch File containing those scripts for daily operations, Windows Scheduled Tasks feature offers useful scheduling capability for backup and cleaning operations on Windows server. However, you never get what you want when you want it on Windows platform.
It takes extra steps in configuring Local Policy to allow the user to have a permission of "Log on as Batch Job" in order to add a scheduled task successfully. That might not always happen in a SysAdmin point of view. This always remind me of the useful tools Cygwin which run linux program within Windows. Of course, you can also call Windows program from there. Combined with Cron in Cygwin, you can schedule daily tasks from Cygwin like Windows Scheduled Tasks.
To install cygwin, please refer to the following article:
http://docs.oracle.com/cd/E24628_01/install.121/e22624/preinstall_req_cygwin_ssh.htm
*Reminder:
- You might need to install extra package for cron within cygwin setup.
- During Cygwin setup, please make sure you have also selected cron by entering 'cron' in search field and mark it as INSTALL.
- Please install Cygwin to the root directory of local drive to get rid of those annoying restrictions.
Once you've got Cygwin setup properly on Windows, you can start installing Cron service on Windows.
Here's the command to install new cron service:
# #Within Command Prompt Terminal C:\>cd c:\cygwin\bin C:\cygwin\bin>cygrunsrv -I Cygwin_CRON_JOBS -p /usr/sbin/cron -a -n #
On Windows desktop, open Control Panel -> Administrative tasks -> Services.
Then look up a service name "Cygwin_CRON_JOBS" which we specified as above.
Make sure this service is started and running properly.
For local user, we might just define the scheduled tasks like this:
# # Within normal opening Cygwin Terminal $ crontab -e # Minute Hour Day of Month Month Day of Week Command # (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) 0 2 12 * 0,6 /cygdrive/c/somewhere/something.bat #
However, this doesn't work in most cases whereas we haven't got sufficient permissions to run CRON tasks in Windows.
For domain user, some extra work needs to be done:
# # Open Cygwin Terminal by right clicking the icon and selecting [run as Administrator] # Within Cygwin Terminal ~ $ touch /etc/crontab # Take ownership for SYSTEM user on this file $ chown SYSTEM /etc/crontab # To avoid famous BAD FILE MODE error in Cygwin, try chmod command # Cron stops working on world editable file due to security reason # To stop error message, let's make it editable ONLY to the file owner $ chmod 0644 /etc/crontab $ crontab -e # Minute Hour Day of Month Month Day of Week Command # (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) 0 2 12 * 0,6 /cygdrive/c/somewhere/something.bat #
To run Windows program within crontab, you can start from the following path:
/cygdrive/c/...
/cygdrive/d/...
which points to the root directory of the local drives where your favourite commands and batch files are locating.
Make sure the path is correct by listing them like:
# # Within Cygwin Terminal $ ls -l /cygdrive/c/Windows #
For additional information about user account created for cron Windows service, please read here:
http://www.davidjnice.com/articles/cygwin_cron-service.html
Hope you can run your favourite tasks in Cron now.
Thanks for the info!
ReplyDeleteI may be mistaken, but I believe the ">" should be swapped for a "\" in the install cron command above.
C:\cygwin\bin\cygrunsrv -I Cygwin_CRON_JOBS -p /usr/sbin/cron -a -n
rather than
C:\cygwin\bin>cygrunsrv -I Cygwin_CRON_JOBS -p /usr/sbin/cron -a -n
cygrunsrv -I Cygwin_CRON_JOBS -p /usr/sbin/cron -a -n
ReplyDelete-bash: cygrunsrv: command not found