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

问题描述 投票:95回答:8

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

例如,我希望updatedb每晚跑步。

我如何让cron工作?

windows cygwin cron crontab
8个回答
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” ”。)


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来运行脚本,通过windows调度程序运行它们,例如: C:\cygwin\bin\bash.exe -l -c "./full-path/to/script.sh"

22
投票

帽子提示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

7
投票

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

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

3
投票

应用this answer的说明并且工作只是为了指出更多的复制粘贴,如答案(因为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管理员,或者是一些怪异的bug ...

现在在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

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

1
投票

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

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

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

$

$ 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

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


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.
© www.soinside.com 2019 - 2024. All rights reserved.