使用 PHP 中的守护进程在 Unix 中编写 MySQL 查询脚本
我试图让“at”作业在给定时间工作,出于测试目的,我使用 $time,但这将是我从单独的 MySQL 查询中获取的日期时间。在命令行中,我是这样的:
echo "mysql -e 'UPDATE admin SET row1=row2 WHERE id=1;'" | at now
我得到一个“job 36 at 2010-10-28 15:05”。在 PHP 中我尝试这样做:
exec("\"mysql -e 'UPDATE admin SET row1=row2 WHERE id=1'\" | at $time");
但是查询没有运行。更糟糕的是,我不知道发生了什么。
echo exec('whoami');
但返回“守护进程”。如何回显从 exec 命令得到的任何响应?理想情况下,我想它会说“job 36 at 2010-10-28 15:05”或类似的内容。
另外,我的目录中有一个 .my.cnf 文件,它指定要使用的数据库、登录名和密码,守护进程是否也需要有一个文件才能使这些文件正常工作?
[从答案中我可以看出我不清楚我想要做什么。我需要
- A. 运行 mySQL 查询来获取日期/时间和 id
- 的日期/时间的 id 匹配的行进行更新
B. 安排对与我已经完成“A”并使用“1” " 表示 id,“now” 表示测试时的时间。我会研究一下 PDO。
I'm trying to make an "at" job work at a given time, for testing purposes I'm using $time but this will be a datetime that I get from a separate MySQL query. In the command line I go like this:
echo "mysql -e 'UPDATE admin SET row1=row2 WHERE id=1;'" | at now
And I get a "job 36 at 2010-10-28 15:05". in PHP I tried going like this:
exec("\"mysql -e 'UPDATE admin SET row1=row2 WHERE id=1'\" | at $time");
But the query doesn't run. Worse, I have no idea what is happening.
echo exec('whoami');
returns "daemon" though. How can I echo whatever response I'm getting from the exec command? ideally I guess it would say "job 36 at 2010-10-28 15:05" or something similar.
Also, I have a .my.cnf file in my dir that specifies the db, login and password to use, does the daemon need to have one also for these to work?
[from the answers I can tell I wasn't clear about what I am trying to do. I need to
- A. Run a mySQL query to get a date/time and an id
- B. Schedule an update to take place to rows that match the id at the date/time
I'd already done "A" and was using "1" for the id and "now" for the time while testing. I'll look into PDO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太熟悉 at 命令,但它似乎有助于在特定时间运行命令。
如果您尝试用 PHP 编写计划脚本,有更好的方法可以实现。只需编写一个 CLI 脚本并使用 cron 来安排它。正如 Svisstack 所指出的,如果您正在运行 MySQL 查询,请使用内置函数(例如 PDO)而不是系统命令。
如果您只是运行系统命令,我不确定您为什么使用 PHP ;)
I'm not that familiar with the at command, but it seems to help run commands at a certain time.
If you're trying to write a scheduled script in PHP there are better ways to do it. Just write a CLI script and use the cron to schedule it. As Svisstack notes, if you're running MySQL queries use an in-built function such as PDO rather than system commands.
If you're just running systems commands, I'm not sure why you're using PHP ;)
您是否处于
safe_mode
?如果是,那么您的|
就会自动转义。根据手册:Are you perhaps in
safe_mode
? If yes then your|
is getting escaped automatically. Per the manual:您可以通过使用
exec
命令的其他参数来获取更多信息。尝试运行它并查看输出。另外,看起来当您在命令行运行它时,您
echo
'ed了MySQL命令,但是当您将其放入PHP代码中时,它并没有执行echo
> 不再了。我不确定这是否会产生影响,因为我不太熟悉at
,但我认为我可以提供一些故障排除帮助。You can get more information by using the other parameters for the
exec
command. Try running this and see the output.Also, it looks like when you ran it at the command line, you
echo
'ed the MySQL command, but when you put it in the PHP code, it's not doing theecho
anymore. I'm not sure if that makes a difference since I'm not to familiar withat
, but I thought that I could offer some troubleshooting help.第二个语句 / exec 中缺少
echo
? (我宁愿使用popen
/proc_open
at $time
和fwrite
at 命令执行(之后关闭输入流,然后关闭程序。使用atq
来验证它是否有效,并注意当前使用可能被禁止at
作业(通常发现在/etc/at.allow
或/etc/at.deny
等文件中)A missing
echo
in your second statement / exec? (and I'd rather usepopen
/proc_open
theat $time
, andfwrite
the command for at to execute (after which you close the input stream, then the program. Useatq
to verify wether it worked, and be aware the current use may be disallowedat
jobs (normally found in files like/etc/at.allow
or/etc/at.deny
)