如何在 Windows 上的 Cygwin 中运行 crontab?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/707184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you run a crontab in Cygwin on Windows?
提问by user10306
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?
我如何使 cron 工作?
采纳答案by Evan
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-*.README
更多详细信息,请参阅。
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\sh.exe -l /my/cygwin/path/to/prog
”)从 Windows 调度程序执行它们。 )
回答by edoloughlin
You have two options:
您有两个选择:
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
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"
使用 cygrunsrv 将 cron 安装为 Windows 服务:
cygrunsrv -I cron -p /usr/sbin/cron -a -n net start cron
请注意,在(非常)旧版本的 cron 中,您需要使用 -D 而不是 -n
“非 .exe”文件可能是 bash 脚本,因此您可以通过调用 bash 来运行脚本来通过 Windows 调度程序运行它们,例如:
C:\cygwin\bin\bash.exe -l -c "./full-path/to/script.sh"
回答by rhoracio
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.
我们将通过用户 SYSTEM 将 cron 作为服务运行。因此,可怜的 SYSTEM 需要一个主目录和一个 shell。“/etc/passwd”文件将定义它们。
$ 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 userscan 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 - 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:
域用户:它不起作用。可怜的 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*
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 上最好创建一个可用的 PATH。主目录和 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
回答by Reg Whitton
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
回答by Jerome Fehribach
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.bat
包含内容的文件:
@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
将快捷方式添加到 Windows 启动文件夹中的以下内容:
Cygwin_launch_crontab_service.bat
See http://www.sevenforums.com/tutorials/1401-startup-programs-change.htmlif 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。顺便说一句,如果您愿意,您可以选择在 Startup 中添加这些:
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
回答by Yordan Georgiev
Applied the instructions from this answerand 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 cmdprompt:
应用了这个答案中的说明并且它起作用了只是指出一个更像答案的复制粘贴(因为 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%
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 管理员,或者一些奇怪的错误......
Now in cmdpromt:
现在在cmd提示符下:
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 bashterminal run crontab -e
现在在bash终端运行 crontab -e
set up you cron entry an example bellow:
设置您的 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
回答by Shen
The correct syntax to install cronin cygwin as Windows service is to pass -nas argument and not -D:
在 cygwin 中将cron安装为 Windows 服务的正确语法是将-n作为参数传递而不是-D:
cygrunsrv --install cron --path /usr/sbin/cron --args -n
cygrunsrv --install cron --path /usr/sbin/cron --args -n
-Dreturns usage error when starting cronin cygwin:
-D在 cygwin 中启动cron时返回使用错误:
$
$
$cygrunsrv --install cron --path /usr/sbin/cron --args -D
$cygrunsrv --install cron --path /usr/sbin/cron --args -D
$cygrunsrv --start cron
$cygrunsrv --start cron
cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062:
cygrunsrv:启动服务时出错:QueryServiceStatus:Win32 错误 1062:
The service has not been started.
该服务尚未启动。
$cat /var/log/cron.log
$cat /var/log/cron.log
cron: unknown option -- D
cron:未知选项--D
usage: /usr/sbin/cron [-n] [-x [ext,sch,proc,parc,load,misc,test,bit]]
用法:/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
在 Windows 中安装和配置 Cygwin Cron 服务:https: //www.davidjnice.com/cygwin_cron_service.html
P.S.I had to run Cygwin64 Terminalon my Windows 10 PC as administrator in order to install cronas Windows service.
PS我必须以管理员身份在我的 Windows 10 PC 上运行Cygwin64 终端才能将cron安装为 Windows 服务。
回答by Harry Rockefeller
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.