使用 PHP 中的守护进程在 Unix 中编写 MySQL 查询脚本

发布于 2024-09-29 20:08:11 字数 750 浏览 3 评论 0原文

我试图让“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 技术交流群。

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

发布评论

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

评论(4

木格 2024-10-06 20:08:11

我不太熟悉 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 ;)

谷夏 2024-10-06 20:08:11

您是否处于 safe_mode?如果是,那么您的 | 就会自动转义。根据手册

启用安全模式后,该命令
字符串被转义为
escapeshellcmd()。因此,回显 y |回声
x 变为 echo y \|回显x。

Are you perhaps in safe_mode? If yes then your | is getting escaped automatically. Per the manual:

With safe mode enabled, the command
string is escaped with
escapeshellcmd(). Thus, echo y | echo
x becomes echo y \| echo x.

何处潇湘 2024-10-06 20:08:11

您可以通过使用 exec 命令的其他参数来获取更多信息。尝试运行它并查看输出。

$output = array();
$return = false;
$last_line = exec("\"mysql -e 'UPDATE admin SET row1=row2 WHERE id=1'\" | at now", $output, $return);

var_dump($last_line);
var_dump($output);
var_dump($return);

另外,看起来当您在命令行运行它时,您echo'ed了MySQL命令,但是当您将其放入PHP代码中时,它并没有执行echo > 不再了。我不确定这是否会产生影响,因为我不太熟悉 at,但我认为我可以提供一些故障排除帮助。

$last_line = exec("echo \"mysql -e 'UPDATE admin SET row1=row2 WHERE id=1'\" | at now", $output, $return);

You can get more information by using the other parameters for the exec command. Try running this and see the output.

$output = array();
$return = false;
$last_line = exec("\"mysql -e 'UPDATE admin SET row1=row2 WHERE id=1'\" | at now", $output, $return);

var_dump($last_line);
var_dump($output);
var_dump($return);

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 the echo anymore. I'm not sure if that makes a difference since I'm not to familiar with at, but I thought that I could offer some troubleshooting help.

$last_line = exec("echo \"mysql -e 'UPDATE admin SET row1=row2 WHERE id=1'\" | at now", $output, $return);
手心的海 2024-10-06 20:08:11

第二个语句 / exec 中缺少 echo? (我宁愿使用 popen / proc_open at $timefwrite at 命令执行(之后关闭输入流,然后关闭程序。使用atq来验证它是否有效,并注意当前使用可能被禁止at作业(通常发现在 /etc/at.allow/etc/at.deny 等文件中)

A missing echo in your second statement / exec? (and I'd rather use popen / proc_open the at $time, and fwrite the command for at to execute (after which you close the input stream, then the program. Use atq to verify wether it worked, and be aware the current use may be disallowed at jobs (normally found in files like /etc/at.allow or /etc/at.deny)

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