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

发布于 2024-07-16 09:11:09 字数 175 浏览 4 评论 0原文

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

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

如何让 cron 工作?

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

For example I want updatedb to run nightly.

How do I make cron work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

错々过的事 2024-07-23 09:11:09

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

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

请参阅 /usr/share/doc/Cygwin/cron-*.README 了解更多详细信息。

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

You need to also install cygrunsrv so you can set cron up as a windows service, then run 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.)

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

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".)

箹锭⒈辈孓 2024-07-23 09:11:09

您有两个选择:

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

    cygrunsrv -I cron -p /usr/sbin/cron -a -n 
    
      网络启动 cron 
      

    <块引用>

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

    使用cygrunsrv

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

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

You have two options:

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

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

    Note, in (very) old versions of cron you need to use -D instead of -n

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

    C:\cygwin\bin\bash.exe -l -c "./full-path/to/script.sh"
    
你的往事 2024-07-23 09:11:09

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

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

我们将由用户 SYSTEM 将 cron 作为服务运行。 因此,可怜的系统需要一个主目录和一个 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 表中找到的内容。 因此,在此处插入您的 suff,以便 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。 主目录和 shell 通常取自“/etc/passwd”。

至于计划命令的列,请参见手册页。

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

$ cronevents

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

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

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.

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

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:

$ 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”.

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

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

$ cronevents
尘世孤行 2024-07-23 09:11:09

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

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

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

cygrunsrv -I cron -p /usr/sbin/cron -a -n
感性不性感 2024-07-23 09:11:09

应用此答案中的说明并且有效
只是为了指出一个更像复制粘贴的答案(因为 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 管理员,或者出现一些奇怪的错误...现在

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

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:

 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%

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

Now in 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

 net start cron

Now in bash terminal run
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
萌︼了一个春 2024-07-23 09:11:09

我弄清楚了如何在登录 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 启动文件夹中添加以下内容的快捷方式:
Cygwin_launch_crontab_service.bat

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

Cygwin

XWin Server

第一个执行

C:\cygwin\Cygwin.bat

,第二个执行

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

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:

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.

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

@echo off
C:
chdir C:\cygwin\bin
bash  cron-config < Cygwin_launch_crontab_service_input.txt

Add a Shortcut to the following in the Windows Startup folder:
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:

Cygwin

XWin Server

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
兔小萌 2024-07-23 09:11:09

在 cygwin 中将 cron 安装为 Windows 服务的正确语法是将 -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

PS 我必须以管理员身份在 Windows 10 电脑上运行 Cygwin64 Terminal 才能将 cron 安装为 Windows 服务。

The correct syntax to install cron in cygwin as Windows service is to pass -n as argument and not -D:

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

-D returns usage error when starting cron in cygwin:

$

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

$cygrunsrv --start cron

cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062:

The service has not been started.

$cat /var/log/cron.log

cron: unknown option -- D

usage: /usr/sbin/cron [-n] [-x [ext,sch,proc,parc,load,misc,test,bit]]

$

Below page has a good explanation.

Installing & Configuring the Cygwin Cron Service in Windows:
https://www.davidjnice.com/cygwin_cron_service.html

P.S. I had to run Cygwin64 Terminal on my Windows 10 PC as administrator in order to install cron as Windows service.

故人爱我别走 2024-07-23 09:11:09
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.
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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文