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

推荐阅读
  • 1crond服务未启动crontab不是Linux内核的功能,而是依赖一个crond服务,这个服务可以启动当然也可以停止。如果停止了就无法执行任何定时任务了,解决的方法是打开它 ... [详细]
  •   crontab命令用于设置周期性被执行的指令,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执 ... [详细]
  • 对于一般的扩展包,我们一般直接pipinstallxxx即可安装,但是unrar直接安装后,发现并不能通过Python程序实现解压的功能& ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • 本文介绍了Linux Shell中括号和整数扩展的使用方法,包括命令组、命令替换、初始化数组以及算术表达式和逻辑判断的相关内容。括号中的命令将会在新开的子shell中顺序执行,括号中的变量不能被脚本余下的部分使用。命令替换可以用于将命令的标准输出作为另一个命令的输入。括号中的运算符和表达式符合C语言运算规则,可以用在整数扩展中进行算术计算和逻辑判断。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • Vagrant虚拟化工具的安装和使用教程
    本文介绍了Vagrant虚拟化工具的安装和使用教程。首先介绍了安装virtualBox和Vagrant的步骤。然后详细说明了Vagrant的安装和使用方法,包括如何检查安装是否成功。最后介绍了下载虚拟机镜像的步骤,以及Vagrant镜像网站的相关信息。 ... [详细]
  • 程序员如何选择机械键盘轴体?红轴和茶轴对比
    本文介绍了程序员如何选择机械键盘轴体,特别是红轴和茶轴的对比。同时还介绍了U盘安装Linux镜像的步骤,以及在Linux系统中安装软件的命令行操作。此外,还介绍了nodejs和npm的安装方法,以及在VSCode中安装和配置常用插件的方法。最后,还介绍了如何在GitHub上配置SSH密钥和git的基本配置。 ... [详细]
  • 其实之前也有下载过完整的android源码,但是从来没有对这个做过一些总结,在加上最近需要经常去看,索性就在从新下载,编译一下,其实这些东西官网上面都有。http:sou ... [详细]
  • MySQL5.6.40在CentOS764下安装过程 ... [详细]
  • Linux下怎么使用crontab命令
    本篇内容主要讲解“Linux下怎么使用crontab命令”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Li ... [详细]
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社区 版权所有