你如何在Windows上运行Cygwin中的crontab?

时间:2009-04-01 20:20:50

标签: windows cygwin cron crontab

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

例如,我希望updatedb每晚运行。

如何让cron工作?

8 个答案:

答案 0 :(得分:89)

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

如果您希望cron作业发送任何输出的电子邮件,您还需要安装eximssmtp(在运行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”。)

答案 1 :(得分:69)

您有两种选择:

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

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

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

  2. 'non .exe'文件可能是bash脚本,所以你可以通过调用bash来运行脚本来运行它们,例如:

    C:\cygwin\bin\bash.exe -l -c "./full-path/to/script.sh"
    

答案 2 :(得分:21)

帽子提示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表中找到的东西。所以在那里插入你的后缀,以便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。 Home dir和shell通常取自“/ etc / passwd”。

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

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

$ cronevents

答案 3 :(得分:7)

只是想补充说cron的选项似乎已经改变了。需要传递-n而不是-D。

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

答案 4 :(得分:3)

我知道如何在登录Windows 7时自动运行Cygwin cron服务。这对我有用:

使用记事本,在第一行创建内容为C:\cygwin\bin\Cygwin_launch_crontab_service_input.txt的文件no,在第二行创建yes(不带引号)。这是您对cron-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 Startup文件夹中为以下内容添加快捷方式: Cygwin_launch_crontab_service.bat

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

Cygwin的

XWin服务器

第一个执行

C:\cygwin\Cygwin.bat

,第二个执行

C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe

答案 5 :(得分:3)

应用this answer的说明并且有效 只是指出一个更复制粘贴的答案(因为cygwin安装程序是一种反复制粘贴明智的实施)
单击WinLogo按钮,键入cmd.exe,右键单击它,选择&#34;以管理员身份启动&#34;。在 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 promt:

 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

答案 6 :(得分:1)

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

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

在cygwin中启动 cron 时,

-D 返回使用错误:

$

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

$ cygrunsrv --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 Terminal ,以便安装 cron < / em> 作为Windows服务。

答案 7 :(得分: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.