104

一些 cygwin 命令是.exe文件,因此您可以使用标准的 Windows 调度程序运行它们,但其他命令没有.exe扩展名,因此无法从 DOS 运行(看起来)。

例如,我想updatedb每晚跑步。

如何让 cron 工作?

4

8 回答 8

95

您还需要安装cygrunsrv,以便可以将 cron 设置为 Windows 服务,然后运行cron-config​​.

如果您希望 cron 作业发送任何输出的电子邮件,您还需要安装eximor ssmtp(在运行之前cron-config。)

有关/usr/share/doc/Cygwin/cron-*.README更多详细信息,请参阅。

对于没有.exe扩展名的程序,它们可能是某种类型的 shell 脚本。如果您查看文件的第一行,您可以看到需要使用什么程序来运行它们(例如,“ #!/bin/sh”),因此您可能可以通过调用 shell 程序从 Windows 调度程序执行它们(例如,“ C:\cygwin\bin\sh.exe -l /my/cygwin/path/to/prog”。 )

于 2009-04-01T20:33:02.303 回答
76

你有两个选择:

  1. 使用 cygrunsrv 将 cron 安装为 Windows 服务:

    cygrunsrv -I cron -p /usr/sbin/cron -a -n
    
    net start cron
    

    请注意,在(非常)旧版本的 cron 中,您需要使用 -D 而不是 -n

  2. “非 .exe”文件可能是 bash 脚本,因此您可以通过 Windows 调度程序通过调用 bash 来运行脚本来运行它们,例如:

    C:\cygwin\bin\bash.exe -l -c "./full-path/to/script.sh"
    
于 2009-04-01T20:34:27.840 回答
25

帽子提示http://linux.subogero.com/894/cron-on-cygwin/

启动 cygwin-setup 并从“Admin”类别添加“cron”包。

我们将由用户 SYSTEM 将 cron 作为服务运行。因此,糟糕的 SYSTEM 需要一个主目录和一个 shell。“/etc/passwd”文件将定义它们。

$ mkdir /root
$ chown SYSTEM:root /root
$ mcedit /etc/passwd
SYSTEM:*:......:/root:/bin/bash

启动服务:

$ 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

本地用户现在可以像这样定义他们的计划任务(crontab 将启动您最喜欢的编辑器):

$ crontab -e  # edit your user specific cron-table HOME=/home/foo
PATH=/usr/local/bin:/usr/bin:/bin:$PATH
# testing - one per line
* * * * *   touch ~/cron
@reboot     ~/foo.sh
45 11 * * * ~/lunch_message_to_mates.sh

域用户:它不起作用。糟糕的 cron 无法代表机器上的域用户运行计划任务。但还有另一种方式:cron 还运行“/etc/crontab”中系统级 cron 表中的内容。因此,在此处插入您的 suff,以便 SYSTEM 代表自己执行此操作:

$ touch /etc/crontab
$ chown SYSTEM /etc/crontab
$ mcedit /etc/crontab
HOME=/root
PATH=/usr/local/bin:/usr/bin:/bin:$PATH
* * * * *   SYSTEM touch ~/cron
@reboot     SYSTEM rm -f /tmp/.ssh*

最后是关于 crontab 条目的几句话。它们是环境设置或预定命令。如上所示,在 Cygwin 上最好创建一个可用的 PATH。主目录和 shell 通常取自“/etc/passwd”。

至于预定命令的列,请参见手册页。

如果某些 crontab 条目没有运行,最好的诊断工具是:

$ cronevents
于 2011-10-26T08:10:53.797 回答
8

只是想补充一点,cron 的选项似乎已经改变。需要通过 -n 而不是 -D。

cygrunsrv -I cron -p /usr/sbin/cron -a -n
于 2011-04-28T12:52:16.333 回答
3

当我登录到 Windows 7 时,我想出了如何让 Cygwin cron 服务自动运行。这对我有用:

使用记事本,在第一行和第二行(不带引号)创建C:\cygwin\bin\Cygwin_launch_crontab_service_input.txt包含内容的文件。这是您对.noyescron-config

创建C:\cygwin\Cygwin_launch_crontab_service.bat包含内容的文件:

@echo off
C:
chdir C:\cygwin\bin
bash  cron-config < Cygwin_launch_crontab_service_input.txt

在 Windows 启动文件夹中添加以下快捷方式: Cygwin_launch_crontab_service.bat

如果您需要有关如何添加到 Startup 的帮助,请参阅http://www.sevenforums.com/tutorials/1401-startup-programs-change.html 。顺便说一句,如果您愿意,您可以选择在 Startup 中添加这些:

赛格温

XWin 服务器

第一个执行

C:\cygwin\Cygwin.bat

第二个执行

C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
于 2011-11-04T19:26:12.767 回答
3

应用了这个答案的说明,它起作用了 只是为了指出一个更像复制粘贴的答案(因为 cygwin 安装程序是一种反复制粘贴明智的实现)
单击 WinLogo 按钮,键入 cmd.exe ,右键单击它,选择“以管理员身份启动”。在cmd提示符下:

 cd <directory_where_i_forgot_the setup-x86_64.exe> cygwin installer:
 set package_name=cygrunsrv cron
 setup-x86_64.exe -n -q -s http://cygwin.mirror.constant.com -P %package_name%

确保安装程序不会在提示符中抛出任何错误......如果它有 - 你可能有一些 cygwin 二进制文件正在运行,或者你不是 Windows 管理员,或者一些怪异的错误......

现在在cmd提示符中:

 C:\cygwin64\bin\cygrunsrv.exe -I cron -p /usr/sbin/cron -a -D   

或您可能对 cygrunsrv.exe 拥有的任何完整文件路径,并在 cmd 提示符下将 cron 作为 Windows 服务启动

 net start cron

现在在bash终端运行 crontab -e

设置您的 cron 条目下面的示例:

        #sync my gdrive each 10th minute
    */10 * * * * /home/Yordan/sync_gdrive.sh

    # * * * * * command to be executed
    # - - - - -
    # | | | | |
    # | | | | +- - - - day of week (0 - 6) (Sunday=0)
    # | | | +- - - - - month (1 - 12)
    # | | +- - - - - - day of month (1 - 31)
    # | +- - - - - - - hour (0 - 23)
    # +--------------- minute
于 2014-03-20T08:36:37.897 回答
2

在 cygwin 中将cron安装为 Windows 服务的正确语法是将-n作为参数而不是-D传递:

cygrinsrv --install cron --path /usr/sbin/cron --args -n

-D在 cygwin 中启动cron时返回使用错误:

$

$cygrunsrv --install cron --path /usr/sbin/cron --args -D

$cygrinsrv --start cron

cygrunsrv:启动服务时出错:QueryServiceStatus:Win32 错误 1062:

该服务尚未启动。

$cat /var/log/cron.log

cron:未知选项--D

用法:/usr/sbin/cron [-n] [-x [ext,sch,proc,parc,load,misc,test,bit]]

$

下面的页面有一个很好的解释。

在 Windows 中安装和配置 Cygwin Cron 服务: https ://www.davidjnice.com/cygwin_cron_service.html

PS我必须以管理员身份在我的 Windows 10 PC 上运行Cygwin64 终端才能将cron安装为 Windows 服务。

于 2018-07-29T12:05:55.837 回答
0
Getting updatedb to work in cron on Cygwin -- debugging steps
1) Make sure cron is installed.
 a) Type 'cron' tab tab and look for completion help.
   You should see crontab.exe, cron-config, etc.  If not install cron using setup.
2) Run cron-config.  Be sure to read all the ways to diagnose cron.
3) Run crontab -e
 a) Create a test entry of something simple, e.g.,
   "* * * * * echo $HOME >> /tmp/mycron.log" and save it.
4) cat /tmp/mycron.log.  Does it show cron environment variable HOME
   every minute?
5) Is HOME correct?  By default mine was /home/myusername; not what I wanted.
   So, I added the entry
   "HOME='/cygdrive/c/documents and settings/myusername'" to crontab.
6) Once assured the test entry works I moved on to 'updatedb' by
   adding an entry in crontab.
7) Since updatedb is a script, errors of sed and find showed up in
   my cron.log file.  In the error line, the absolute path of sed referenced
   an old version of sed.exe and not the one in /usr/bin.  I tried changing my
   cron PATH environment variable but because it was so long crontab
   considered the (otherwise valid) change to be an error.  I tried an
   explicit much-shorter PATH command, including what I thought were the essential
   WINDOWS paths but my cron.log file was empty.  Eventually I left PATH alone and
   replaced the old sed.exe in the other path with sed.exe from /usr/bin.
   After that updatedb ran to completion.  To reduce the number of
   permission error lines I eventually ended up with this:
   "# Run updatedb at 2:10am once per day skipping Sat and Sun'
   "10 2  *  *  1-5  /usr/bin/updatedb --localpaths='/cygdrive/c' --prunepaths='/cygdrive/c/WINDOWS'"

Notes: I ran cron-config several times throughout this process
       to restart the cygwin cron daemon.
于 2013-12-11T17:03:54.610 回答