Linux 如何设置 wget cron 作业命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8063704/
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 to setup a wget cron job command
提问by clarkk
How to setup a cron job command to execute an URL?
如何设置 cron 作业命令来执行 URL?
/usr/bin/wget -q http://www.domain.com/cron_jobs/job1.php >/dev/null 2>&1
Why can't I make this work!? Have tried everything.. The PHP script should send an email and create some files, but none is done
为什么我不能让这个工作!?已经尝试了一切.. PHP 脚本应该发送一封电子邮件并创建一些文件,但没有完成
The command returns this:
该命令返回:
Output from command /usr/bin/wget -q http://www.domain.com/cron_jobs/job1.php ..
No output generated
... but it still creates an empty file in /root
on each execute!? Why?
...但它仍然/root
在每次执行时创建一个空文件!?为什么?
采纳答案by Ivan
Use curl like this:
像这样使用卷曲:
/usr/bin/curl http://domain.com/page.php
Don't worry about the output, it will be ignored
不要担心输出,它会被忽略
回答by Zac Imboden
I had the same problem. The solution is understanding that wget is outputting two things: the results of the url request AND activity messages about what it's doing.
我有同样的问题。解决方案是理解 wget 正在输出两件事:url 请求的结果和关于它正在做什么的活动消息。
By default, if you do not specify an output file, it will create one, seemingly named after the file in your url, in the current folder where wget is run.
默认情况下,如果您不指定输出文件,它将在运行 wget 的当前文件夹中创建一个,看似以您的 url 中的文件命名。
If you want to specify a different output file:
如果要指定不同的输出文件:
-O outputfile.txt
will output the url results to outputfile.txt, overrwriting what's there.
将 url 结果输出到 outputfile.txt,覆盖那里的内容。
If you wish to append to that file, write to std out and then append to the file from there: and here's the trick: to write to std out use:
如果您希望附加到该文件,请写入标准输出,然后从那里附加到文件:这是技巧:写入标准输出使用:
-O-
the second dash is in lieu of a filename and tells wget to write the url results to std out.
第二个破折号代替文件名,并告诉 wget 将 url 结果写入标准输出。
then use the append syntax, >>, to append to a file of your choice:
然后使用附加语法 >> 附加到您选择的文件中:
wget -O- http://www.invisibility.com >>/var/log/invisibility.log
The lower case o, specifies the location of the activity log, so if you wish to log activity for the url request, you can:
小写 o,指定活动日志的位置,因此如果您希望记录 url 请求的活动,您可以:
wget -o http://someurl.com /var/log/activity.log
-q suppresses output of activity messages
-q 抑制活动消息的输出
wget -q http://someurl.com /var/log/activity.log
will not log any activity to the specified file, and I think that is the crux where people get confused.
不会将任何活动记录到指定的文件中,我认为这是人们感到困惑的症结所在。
Remember: -O is shorthand for --output-document -o is shorthand for --output-file, which is the activity log.
请记住:-O 是 --output-document 的简写 -o 是 --output-file 的简写,即活动日志。
回答by Nabi K.A.Z.
For use wget to display HTML:
使用 wget 显示 HTML:
wget -qO- http://www.example.com
回答by Guest
Took me hours to get it working. Thank you for people writing down solutions. One also needs to make sure to check whether single or double quotes are needed, otherwise it will parse the url wrong leading to error messages:
我花了几个小时才让它工作。感谢人们写下解决方案。还需要确保检查是否需要单引号或双引号,否则会解析url错误导致错误消息:
This worked (using single quotes):
这有效(使用单引号):
/usr/bin/wget -O -q 'http://domain.com/cron-file.php'
This gave errors (using double quotes):
这给出了错误(使用双引号):
/usr/bin/wget -O -q "http://domain.com/cron-file.php"
Don't know if the /usr/bin/ is needed. Read about different ways of how to do the order of the -O -q. It is hard to find a reliable definitive source on the web for this subject. So many different examples.
不知道是否需要 /usr/bin/ 。阅读有关如何执行 -O -q 顺序的不同方法。很难在网络上找到有关此主题的可靠权威来源。这么多不同的例子。
An online wget manual can be found here, for the available options (but check with the Linux distro one is using for an up to date version): http://unixhelp.ed.ac.uk/CGI/man-cgi?wget
可以在此处找到在线 wget 手册,了解可用选项(但请查看正在使用的 Linux 发行版以获取最新版本):http: //unixhelp.ed.ac.uk/CGI/man-cgi?wget