Windows 下的 cron 程序


这段时间因为有个网页需要定时更新,但服务器又不支持自定义定时计划任务,因此考虑使用网站程序来更新,本地计算机启用定时计划任务来执行,搜索了一下 windows cron,果然有收获,下载了一个cron.exe 是windows下面的一个自动定时扫描程序,会自动执行crontab里面的命令,试了一下,感觉还不错.
这段时间因为有个网页需要定时更新,但服务器又不支持自定义定时计划任务,因此考虑使用网站程序来更新,本地计算机启用定时计划任务来执行,搜索了一下 windows cron,果然有收获,下载了一个cron.exe 是windows下面的一个自动定时扫描程序,会自动执行crontab里面的命令,试了一下,感觉还不错.
Linux 下非常流行并且好用的定时任务管理程序 cron,比起 windows 下的 计划任务, 不知道好到哪里去了。突然想在 windows 下应该有类似的程序,于是搜索”windows cron”,果然有收获。顺便感叹一下,互联网时代,准确的定位“关键词”是一门学问。换个角度来说,以互联网的思维,用简练的词汇来总结你所要的,互联网信息就能够为你所用了。
使用步骤
- 下载 cron 主程序,并解压缩到某目录,地址 http://www.kalab.com/freeware/cron/cron.zip 
- 运行目录 cron.exe 启动 cron ,当然一般需要拷贝到”启动”以便 cron 每次开机自动启动
- 在 crontab 中按照 linux 一样的格式定义任务即可,每次执行命令会在安装目录下生成一个 cron.log 文件,记录执行情况
试用了一下,发现有些问题:
在命令行下能够执行的某些命令比如 time /T >c:test.txt ,放到 crontab 中执行,log 文件中会提示:Error: The specified file was not found. 也就是找不到 time 命令,估计是 cron.exe 不会根据系统 PATH 去寻找命令。同样执行 dir 这样,但是 xcopy 执行成功。而写一个批处理文件的不好就是屏幕上还是会闪现一下命令行窗口。
更多信息参考 http://www.kalab.com/freeware/cron/cron.htm
顺便将一些简要参数也摘录过来

Setup for first version (Windows 9x/NT)

  1. copy the files cron.exe and crontab to a folder of your choice
  2. edit the file crontab (see the examples as a reference)
  3. create a link to cron.exe and move it to the startup folder

Setup for NT service

  1. create a crontab file in your SYSTEM32 folder
  2. execute the included ntsetup.exe
  3. call "crons.exe /?" in a command line to see the available options of crons

The Crontab file

Each line of CRONTAB has the following structure:

<Minute> <Hour> <Day> <Month> <Day of Week> <Command line>


Position:Values:
Minute0-59
Hour0-23
Day1-31
Month1-12
Day of week0-6 (0=Sunday, 1=Monday, ..., 6=Saturday)

Instead of minute, hour, day, month or day of week it's also possible to specify a *. A * represents all possible values for that position (e.g. a * on 2nd position is the same as specifying all the possible values for hour)

It's also possible to specify several values separated by commas: e.g. if you want a command to be executed every 10th minute so you can specify 0,10,20,30,40,50 for minute. A range of values can be specified with a -: e.g. value 0-12 for hour -> every hour a.m.

Comments

Every line not starting with a number is treated as a comment.

Examples

execute a command every minute

# This is a comment.
  * * * * * c:toolsCheckForNewFiles.exe

execute the program backup.bat every hour

# This is a comment.
  0 * * * * c:testsbackup.bat

do a backup every day at 0.30 p.m.

# This is a comment.
  30 12 * * * c:testsbackup.bat

start a backup every tuesday at midnight

# This is a comment.
  0 0 * * 3 c:testsbackup.bat

scan drive c for viruses on the 1st of every month at 2.45 p.m.

# scan for known viruses
  45 14 1 * * c:path_to_scannerscan c:

copy some files every half hour

# This is a comment.
  0,30 * * * * xcopy c:docs*.* y:users /s

copy a file every hour (a.m.) only on weekdays

# System commands need the command interpreter specified:
0 0-12 * * 1-5 c:...cmd.exe /c "copy c:... e:"
  • 你可能对这些文章感兴趣