热门标签 | 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.

推荐阅读
  • 技术日志:Ansible的安装及模块管理详解 ... [详细]
  •   crontab命令用于设置周期性被执行的指令,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执 ... [详细]
  • #!binbash########################################################################## File Name : rsync_nobody.sh#  ... [详细]
  • 1crond服务未启动crontab不是Linux内核的功能,而是依赖一个crond服务,这个服务可以启动当然也可以停止。如果停止了就无法执行任何定时任务了,解决的方法是打开它 ... [详细]
  • 如何在PHP中安装Xdebug扩展
    本文介绍了如何从PECL下载并编译安装Xdebug扩展,以及如何配置PHP和PHPStorm以启用调试功能。 ... [详细]
  • 为了确保iOS应用能够安全地访问网站数据,本文介绍了如何在Nginx服务器上轻松配置CertBot以实现SSL证书的自动化管理。通过这一过程,可以确保应用始终使用HTTPS协议,从而提升数据传输的安全性和可靠性。文章详细阐述了配置步骤和常见问题的解决方法,帮助读者快速上手并成功部署SSL证书。 ... [详细]
  • `chkconfig` 命令主要用于管理和查询系统服务在不同运行级别中的启动状态。该命令不仅能够更新服务的启动配置,还能检查特定服务的当前状态。通过 `chkconfig`,管理员可以轻松地控制服务在系统启动时的行为,确保关键服务正常运行,同时禁用不必要的服务以提高系统性能和安全性。本文将详细介绍 `chkconfig` 的各项参数及其使用方法,帮助读者更好地理解和应用这一强大的系统管理工具。 ... [详细]
  • 分布式开源任务调度框架 TBSchedule 深度解析与应用实践
    本文深入解析了分布式开源任务调度框架 TBSchedule 的核心原理与应用场景,并通过实际案例详细介绍了其部署与使用方法。首先,从源码下载开始,详细阐述了 TBSchedule 的安装步骤和配置要点。接着,探讨了该框架在大规模分布式环境中的性能优化策略,以及如何通过灵活的任务调度机制提升系统效率。最后,结合具体实例,展示了 TBSchedule 在实际项目中的应用效果,为开发者提供了宝贵的实践经验。 ... [详细]
  • 深入解析:RKHunter与AIDE在入侵检测中的应用与优势
    本文深入探讨了RKHunter与AIDE在入侵检测领域的应用及其独特优势。通过对比分析,详细阐述了这两种工具在系统完整性验证、恶意软件检测及日志文件监控等方面的技术特点和实际效果,为安全管理人员提供了有效的防护策略建议。 ... [详细]
  • 如何在Linux系统中实施网络流量监测与分析
    本文详细介绍了在Linux系统中实施网络流量监测与分析的方法。通过使用专业的工具和技术,读者可以有效地监控和分析网络流量,从而提高系统的安全性和性能。希望本文能为读者提供有价值的参考和实践指导。 ... [详细]
  • 用.sh文件来完成任务,但现在有个项目直接用url就行。一般系统不会有crontabShell#安装crontab:yuminstallcronta ... [详细]
  • 前言crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那么任务就是常驻程序,这对你的程序要求比较 ... [详细]
  • 一,crontab服务的简介二、安装cron服务安装cron服务:--yuminstallvixie-cron--yuminstallc ... [详细]
  • 启动服务#servicecrondstart或者#etcinit.dcrondstart格式:*  *  *  *  *  command分 时 日 月 周  ... [详细]
  • Requests库的基本使用方法
    本文介绍了Python中Requests库的基础用法,包括如何安装、GET和POST请求的实现、如何处理Cookies和Headers,以及如何解析JSON响应。相比urllib库,Requests库提供了更为简洁高效的接口来处理HTTP请求。 ... [详细]
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社区 版权所有