【发布时间】:2010-10-16 23:17:26
【问题描述】:
一些 cygwin 命令是 .exe 文件,因此您可以使用标准的 Windows 调度程序运行它们,但其他命令没有 .exe 扩展名,因此无法从 DOS 运行(看起来像)。
例如,我希望 updatedb 每晚运行。
如何让 cron 工作?
【问题讨论】:
标签: windows cygwin cron crontab
一些 cygwin 命令是 .exe 文件,因此您可以使用标准的 Windows 调度程序运行它们,但其他命令没有 .exe 扩展名,因此无法从 DOS 运行(看起来像)。
例如,我希望 updatedb 每晚运行。
如何让 cron 工作?
【问题讨论】:
标签: windows cygwin cron crontab
您还需要安装cygrunsrv,以便将cron 设置为Windows 服务,然后运行cron-config。
如果您希望 cron 作业发送任何输出的电子邮件,您还需要安装 exim 或 ssmtp(在运行 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”。)
【讨论】:
can't switch user context 失败,即使明确请求不要使用其他帐户帐户。在 cygwin 上使用 cron 切换帐户显然是强制性的。你必须对自己跑步说不......
cronevents 命令查看事件(来自windows eventgs 日志)。它们永远不会被丢弃,我会每隔几个月修剪一次原木。
你有两个选择:
使用 cygrunsrv 将 cron 安装为 Windows 服务:
cygrunsrv -I cron -p /usr/sbin/cron -a -n
net start cron
注意,在(非常)旧版本的 cron 中,您需要使用 -D 而不是 -n
“非 .exe”文件可能是 bash 脚本,因此您可以通过 windows 调度程序通过调用 bash 来运行脚本来运行它们,例如:
C:\cygwin\bin\bash.exe -l -c "./full-path/to/script.sh"
【讨论】:
-l 选项(登录 shell)有什么意义。脚本通常由非登录、非交互式 shell 运行 - 此类 shell 不提供 /etc/profile、~/.bash_profile、~/.bashrc,这对脚本没有意义。 -c 选项也是不必要的,它用于指定命令,脚本只是一个文件,可以直接馈送到 bash。所以,它变成了C:\cygwin\bin\bash.exe "./path/to/script.sh"。
只是想补充一点,cron 的选项似乎已经改变。需要通过 -n 而不是 -D。
cygrunsrv -I cron -p /usr/sbin/cron -a -n
【讨论】:
帽子提示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 表中的内容。因此,在此处插入您的 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
【讨论】:
当我登录到 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
如果您需要有关如何添加到 Startup 的帮助,请参阅 http://www.sevenforums.com/tutorials/1401-startup-programs-change.html。顺便说一句,如果您愿意,您可以选择在 Startup 中添加这些:
赛格温
XWin 服务器
第一个执行
C:\cygwin\Cygwin.bat
第二个执行
C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
【讨论】:
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.
【讨论】:
应用了来自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 管理员,或者一些怪异的错误......
现在在 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
【讨论】:
在 cygwin 中将 cron 安装为 Windows 服务的正确语法是传递 -n 作为参数和不是-D:
cygrunsrv --install cron --path /usr/sbin/cron --args -n
-D 在 cygwin 中启动 cron 时返回使用错误:
$
$cygrinsrv --install cron --path /usr/sbin/cron --args -D
$cygrinsrv --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 PC 上运行 Cygwin64 Terminal 才能安装 cron 作为 Windows 服务。
【讨论】: