[英]How to run a CasperJS script every hour automatically?
I have a CasperJS code that executes some tasks on my website. 我有一个CasperJS代码,可以在我的网站上执行一些任务。 I want the code to run every hour!
我希望代码每小时运行一次!
For the moment being, the code is running locally and manually (using casperjs cas.js
) every time. 目前,代码每次都在本地手动运行(使用
casperjs cas.js
)。 Now I want: 现在我要:
I could achieve the first objective by making a Node server that runs the command casperjs casper/cas.js
every hour (using setInterval
function). 通过使节点服务器每小时运行一次
casperjs casper/cas.js
命令(使用setInterval
函数),可以实现第一个目标。 However, I couldn't deploy it on Heroku since it supports only one type of code (Node.js). 但是,我无法在Heroku上部署它,因为它仅支持一种类型的代码(Node.js)。 The
cas.js
script is written with CasperJS, so it couldn't been executed! cas.js
脚本是用CasperJS编写的,因此无法执行!
What is the best way to achieve these two goals? 实现这两个目标的最佳方法是什么?
Let's assume you are on Linux... 假设您使用的是Linux ...
1. The most elegant solution is to use a cron job , as suggested by Vaviloff . 1.最优雅的解决方案是使用 Varonoff建议的cron作业 。
If you want to execute your CasperJS script every hour, you can edit your crontab with crontab -e
and then add the following entry: 如果要每小时执行一次CasperJS脚本,则可以使用
crontab -e
编辑crontab,然后添加以下条目:
0 * * * * PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs /usr/local/bin/casperjs /absolute/path/to/your/casper/script.js 2>&1
Some remarks: 一些说明:
0
means "0 minute". 0
表示“ 0分钟”。 It is required here, otherwise your script will run every minute. PHANTOMJS_EXECUTABLE
). PHANTOMJS_EXECUTABLE
)指定PhantomJS命令的路径。 /usr/local/bin/phantomjs
and /usr/local/bin/casperjs
respectively, using which
( which phantomjs
& which casperjs
). /usr/local/bin/phantomjs
和/usr/local/bin/casperjs
,使用which
( which phantomjs
和which casperjs
)。 2>&1
redirects stderr to stdout . 2>&1
将stderr重定向到stdout 。
2. Use SpookyJS if you want to run CasperJS scripts with Node.js. 2.如果要通过Node.js运行CasperJS脚本,请使用SpookyJS 。
npm i spooky
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.