无需客户端请求即可在服务器上运行的 php 脚本

发布于 2024-08-03 07:01:54 字数 108 浏览 11 评论 0原文

我正在开发一个需要在服务器上运行 php 脚本而无需任何请求的网站, 它是一个机器人脚本,可以(不是全职但每天至少一次)检查客户帐户并在发生情况时向客户发送警报消息。

任何想法表示赞赏。

I am working on a site that require a php script running on a server without any request,
it is a bot script that keeps (not full time but at least once a day) checking client accounts and send alert messages to clients when something happens.

any ideas are appreciated.

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

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

发布评论

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

评论(6

风情万种。 2024-08-10 07:01:54

假设您需要在 Linux 上执行此操作,您也可以从浏览器和 CLI 运行任何 php 脚本。

您可以运行一个简单的 php 脚本:
<代码>

像这样:
php -f ./index.php

请注意文件权限,任何可能在代码中蔓延的错误、内存泄漏或未分配的变量现在都将变得非常明显,因为该进程将连续运行。

如果您不希望它一直在后台运行,请查看 crontab (http://unixgeeks.org/security/newbie/unix/cron-1.html)能够定期启动作业。

-- 编辑 --

看一下 php 执行后台进程PHP:如何将信息返回到等待的脚本并继续处理

基本上你想启动一个后台进程,如果没有访问权限,你可以通过在你自己的脚本上使用 exec() 或 fsockopen() 或 file_get_contents() 来完成此操作,可能按此顺序exec 或套接字函数。
另请查看 https://www.php.net /manual/en/function.session-write-close.php 因此“后台脚本”不会“阻止”请求,并且 https://www.php.net/manual/en/function.ignore-user-abort.php

Assuming you need to do this on linux, you may run any php script from the browser and from the CLI as well.

You may run a simple php script:
<? echo "Ana are mere"; ?>

like this:
php -f ./index.php

Be careful about file-permissions, and any bug that may creep inside your code, memory leaks or unallocated variables will become VERY visible now, as the process will run continuously.

If you dont want it running in the background all the time, take a look at crontab (http://unixgeeks.org/security/newbie/unix/cron-1.html) to be able to start jobs regularly.

-- edit--

take a look at php execute a background process and PHP: How to return information to a waiting script and continue processing

Basically you want to start a background process, and you may do this by either using exec() or fsockopen() or a file_get_contents() on your own script probably in this order, if don't have access to exec, or socket functions.
Also take a look at https://www.php.net/manual/en/function.session-write-close.php so the "background script" won't "block" the request and https://www.php.net/manual/en/function.ignore-user-abort.php

堇年纸鸢 2024-08-10 07:01:54

使用 cron job 来完成 http://www.cronjobs.org/

可以自动调用脚本无限期地以您喜欢的任何间隔进行。如果他们很好的话,你的托管提供商应该支持他们。

您还应该考虑在页面末尾放置一个唯一的键,

即。 www.yoursite.com/cronjob.php?key=randomstring

然后仅在密钥正确时运行脚本,以防止机器人和其他用户在您不希望脚本运行时运行该脚本。

Use a cron job to do it http://www.cronjobs.org/

You can automatically call a script at any interval you like indefinitely. Your hosting provider should support them if they are good.

You should also consider putting a unique key on the end of the page

ie. www.yoursite.com/cronjob.php?key=randomstring

and then only run the script if the key is correct, to prevent bots and other users from running the script when you don't want it run.

层林尽染 2024-08-10 07:01:54

如果您无法创建 cron 作业,则创建一个页面来执行您想要的操作,并在另一台计算机(也许是您的 PC?)上创建一个计划任务,该任务每天都会在特定时间出去并点击该页面。

这确实是一个 hack,但如果你绝对无法设置 cron 作业,那么这将是一个选择。

If you can't create a cron job, then create a page that does what you want and create a scheduled task on another machine (maybe your PC?) that just goes out and hits that page at a certain time every day.

It's really a hack, but if you absolutely can't set up a cron job, it would be an option.

滥情空心 2024-08-10 07:01:54

As Evernoob and Quamis said, you want to have a cron job (UNIX/Linux/Mac OS) or a scheduled task (MS Windows). Furthermore, you can either have the PHP script run using the PHP command line interface (CLI), in which case you can invoke the PHP executable and then your script name. As an alternate, you can use a tool like wget (availble on all platforms) to invoke the PHP script as if someone had typed the URL in the location bar of a web browser.

百善笑为先 2024-08-10 07:01:54

php 脚本不能像您想象的那样使用。因为它是在某个地方发出请求后通过apache执行的。

即使您在脚本中执行 while(1),apache/php 也会自动停止您的脚本。
回应您的评论,是的,您需要 ssh 访问权限才能执行此操作,除非您的 Web 界面允许您添加 cronjob。

也许您可以编写一个可以使用另一台服务器上的程序执行的服务并完成这项工作。

A php script could not be used like you imagine here. Because it's executed through apache after a request from somewhere.

Even if you do while(1) in your script, apache/php will automaticly stop your script.
Responding to your comment, yes you'll need ssh access to do this, except if your web interface allow you to add cronjob.

Maybe you can write a service which can be executed with a program on another server and do the job.

浪荡不羁 2024-08-10 07:01:54

如果您无法访问服务器,最简单的方法可能是通过浏览器访问它,但这需要您或外部脚本每天在您想要的时间间隔内以相同的时间间隔访问 URL。您还可以设置一个 Selenium 测试套件,该套件按计划在本地运行并点击页面。我不是 100% 相信 Selenium 是否可以实现这一点,但您可能需要一些第三方应用程序才能实现这一点。

您可以尝试的其他方法是了解如何使用 PHP 的进程控制函数(链接< /a>)。这些将让您创建一个作为守护进程并在后台运行的脚本。您可以执行此操作以保持脚本在服务器上运行并按编程的时间间隔发出命令。不过,您仍然需要某种方法来让它第一次运行(浏览器请求或通过命令行)。

If you have no access to the server the easiest way would probably be to hit it through the browser, but that would require you or an external script hitting the URL at the same interval each day when you wanted it to one. You may also be able to setup a Selenium test suite that runs locally on a schedule and hits the page. I'm not 100% if that's possible with Selenium though, you may need some 3rd-party apps to make it happen.

Something else you could try would be to see about using PHP's Process Control Functions (link). These will let you create a script that is a deamon and runs in the background. You may be able to do this to keep the script running on the server and firing off commands at programmed intervals. You will still need some way to get it running the first time (browser request or via command line) though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文