热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Somecygwincommandsare.exefiles,soyoucanrunthemwiththestandardWindowsScheduler,butot

Some cygwin commands are .exe files, so you can run them with the standard Windows Scheduler, but others don't have an .exe extension so can't be run from DOS (it seems like).

有些cygwin命令是.exe文件,因此可以使用标准的Windows调度器运行它们,但有些命令没有.exe扩展名,因此不能从DOS运行它们(看起来是这样的)。

For example I want updatedb to run nightly.

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

How do I make cron work?

我如何让克伦工作?

7 个解决方案

#1


80  

You need to also install cygrunsrv so you can set cron up as a windows service, then run cron-config.

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

If you want the cron jobs to send email of any output you'll also need to install either exim or ssmtp (before running cron-config.)

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

See /usr/share/doc/Cygwin/cron-*.README for more details.

看到/usr/share/doc/Cygwin/cron-*.自述为更多的细节。

Regarding programs without a .exe extension, they are probably shell scripts of some type. If you look at the first line of the file you could see what program you need to use to run them (e.g., "#!/bin/sh"), so you could perhaps execute them from the windows scheduler by calling the shell program (e.g., "C:\cygwin\bin\sh.exe -l /my/cygwin/path/to/prog".)

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

#2


63  

You have two options:

你有两个选择:

  1. Install cron as a windows service, using cygrunsrv:

    使用cygrunsrv将cron安装为windows服务:

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

    cygrunsrv - cron -p /usr/sbin/cron -a -D

    net start cron

    净启动cron

  2. The 'non .exe' files are probably bash scripts, so you can run them via the windows scheduler by invoking bash to run the script, e.g.:

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

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

    C:\ cygwin \ bin \ bash。exe - l - c”。/完整路径/ / script.sh”

#3


18  

hat tip http://linux.subogero.com/894/cron-on-cygwin/

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

Start the cygwin-setup and add the “cron” package from the “Admin” category.

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

We’ll run cron as a service by user SYSTEM. Poor SYSTEM therefore needs a home directory and a shell. The “/etc/passwd” file will define them.

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

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

The start the service:

启动服务:

$ 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

Local users can now define their scheduled tasks like this (crontab will start your favourite editor):

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

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

Domain users: it does not work. Poor cron is unable to run scheduled tasks on behalf of domain users on the machine. But there is another way: cron also runs stuff found in the system level cron table in “/etc/crontab”. So insert your suff there, so that SYSTEM does it on its own behalf:

域用户:它不工作。可怜的cron无法代表机器上的域用户运行调度任务。但是还有另一种方法:cron还运行在“/etc/crontab”中的系统级cron表中的内容。所以,插入你的suff,让系统代表它自己:

$ 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*

Finally a few words about crontab entries. They are either environment settings or scheduled commands. As seen above, on Cygwin it’s best to create a usable PATH. Home dir and shell are normally taken from “/etc/passwd”.

最后是关于crontab条目的几句话。它们要么是环境设置,要么是预定的命令。如上所示,在Cygwin上,最好创建一个可用的路径。Home dir和shell通常取自" /etc/passwd "。

As to the columns of scheduled commands see the manual page.

关于计划命令的列,请参阅手册页。

If certain crontab entries do not run, the best diagnostic tool is this:

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

$ cronevents

#4


7  

Just wanted to add that the options to cron seem to have changed. Need to pass -n rather than -D.

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

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

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

#5


3  

Applied the instructions from this answer and it worked Just to point out a more copy paste like answer ( because cygwin installation procedure is kind of anti-copy-paste wise implemented )
Click WinLogo button , type cmd.exe , right click it , choose "Start As Administrator". In cmd prompt:

应用了这个答案的说明,它只需要指出更多的复制粘贴就可以了(因为cygwin安装过程是一种防复制粘贴的实现)点击WinLogo按钮,输入cmd。exe,右键单击,选择“Start As Administrator”。在cmd提示:

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

Ensure the installer does not throw any errors in the prompt ... If it has - you probably have some cygwin binaries running or you are not an Windows admin, or some freaky bug ...

确保安装程序不会在提示中抛出任何错误…如果有的话——你可能有一些cygwin二进制文件在运行,或者你不是Windows管理员,或者是某个古怪的bug……

Now in cmd promt:

现在在cmd promt:

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

or whatever full file path you might have to the cygrunsrv.exe and start the cron as windows service in the cmd prompt

或者任何你可能有到cygrunsrv的完整文件路径。exe并在cmd提示符中启动cron作为windows服务

 net start cron

Now in bash terminal run crontab -e

现在在bash终端运行crontab -e

set up you cron entry an example bellow:

为你树立一个榜样,下面是一个例子:

        #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


3  

I figured out how to get the Cygwin cron service running automatically when I logged on to Windows 7. Here's what worked for me:

当我登录到Windows 7时,我知道如何让Cygwin cron服务自动运行。这就是我的工作:

Using Notepad, create file C:\cygwin\bin\Cygwin_launch_crontab_service_input.txt with content no on the first line and yes on the second line (without the quotes). These are your two responses to prompts for cron-config.

使用记事本,创建文件C:\cygwin\bin\Cygwin_launch_crontab_service_input。txt,第一行的内容为no,第二行为yes(没有引号)。这是您对cron-config提示符的两个响应。

Create file C:\cygwin\Cygwin_launch_crontab_service.bat with content:

创建文件C:\ cygwin \ Cygwin_launch_crontab_service。蝙蝠与内容:

@echo off
C:
chdir C:\cygwin\bin
bash  cron-config 

Add a Shortcut to the following in the Windows Startup folder: Cygwin_launch_crontab_service.bat

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

See http://www.sevenforums.com/tutorials/1401-startup-programs-change.html if you need help on how to add to Startup. BTW, you can optionally add these in Startup if you would like:

如果您需要关于如何添加到Startup的帮助,请参见http://www.sevenforums.com/tutorials/1401-startup-programs-change.html。顺便说一句,如果你想:

Cygwin

Cygwin

XWin Server

XWin服务器

The first one executes

第一个执行

C:\cygwin\Cygwin.bat

and the second one executes

第二个执行

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

#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.

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文介绍如何在现有网络中部署基于Linux系统的透明防火墙(网桥模式),以实现灵活的时间段控制、流量限制等功能。通过详细的步骤和配置说明,确保内部网络的安全性和稳定性。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 本文深入探讨了 PHP 实现计划任务的方法,包括其原理、具体实现方式以及在不同操作系统中的应用。通过详细示例和代码片段,帮助开发者理解和掌握如何高效地设置和管理定时任务。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍了如何规划和部署一个高可用的Etcd集群,包括主机配置、软件安装、防火墙设置及集群健康检查等内容。通过合理的硬件配置和网络规划,确保Etcd集群在生产环境中的稳定运行。 ... [详细]
  • 本文介绍了如何在Django项目中使用django-crontab库来设置和管理定时任务,包括安装、配置、编写定时任务以及常见问题的解决方案。通过具体实例,帮助开发者快速掌握在Django中实现自动化任务的方法。 ... [详细]
  • MySQL 'Too Many Connections' 错误处理及优化方案
    本文详细介绍了如何诊断和解决MySQL数据库中出现的‘Too Many Connections’错误,包括查看当前连接状态、调整配置文件以及优化应用代码等方法。 ... [详细]
  • 本文详细介绍了在Linux环境下如何有效地管理任务,包括撤销操作、挂起与恢复任务、终止后台程序以及取消定时任务的方法。这些技巧对于提高日常工作效率和系统维护至关重要。 ... [详细]
author-avatar
双子宝贝鱼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有