gpt4 book ai didi

php - shell 通过 PHP : Trouble adding new cron job to crontab

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:52 25 4
gpt4 key购买 nike

我计划在我的 crontab 中运行几个脚本,但只能以 root 身份(或使用 sudo)查看它们。我需要一个 PHP 脚本(作为 nginx 运行)能够向 crontab 文件添加一个新行。为此,我创建了一个 shell 脚本(由 root 拥有)并通过 /etc/sudoers 文件授予 nginx 用户 sudo 权限.

/etc/sudoers 文件的最后一行:

nginx ALL=NOPASSWD: /etc/nginx/addcron.sh

执行脚本的 PHP 调用:

chdir("/etc/nginx/");
echo exec("2>&1 ./addcron.sh $custname", $output);
echo "<pre>".print_r($output, true)."</pre>";

我当前的 crontab:

[ec2-user@ip-172-31-xx-xxx nginx]$ sudo crontab -l
* * * * * env > /tmp/env.output
* * * * * /usr/bin/php -f /var/www/html/example/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/demo/cron/cron.php
0 23 * * * rm /tmp/cachegrind.out.*

关于我的 addcron.sh 文件的元信息:

[ec2-user@ip-172-31-xx-xxx nginx]$ pwd
/etc/nginx
[ec2-user@ip-172-31-xx-xxx nginx]$ ls -al addcron.sh
-rwxr-xr-x 1 root root 129 Nov 24 22:16 addcron.sh

addcron.sh的内容:

#!/bin/bash
custname="$1"
(crontab -l; echo \"* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php\" ) | crontab -

当我尝试运行它时,出现以下错误:

errors in crontab file, can't install.
Array
(
[0] => "-":102: bad minute
[1] => errors in crontab file, can't install.
)

它似乎不喜欢 addcron.sh 中的 - 标记,但我的 Google 搜索表明这是正确的。此外,我尝试将 sudo 添加到 PHP 的 exec 命令中,但随后出现以下错误:

sorry, you must have a tty to run sudo

我做错了什么或遗漏了什么,为什么?

最佳答案

旁注:从安全角度来看,在 root 帐户下运行与系统管理员无关的 cron 作业是一个坏主意。在 nginx 用户下安装您的 crons。

问题是由 bash 脚本中的引号转义引起的(顺便说一句,您可以直接在 bash 中逐个检查):

> (crontab -l; echo \"* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php\" )
no crontab for username
"* ... <all kinds of filenames in here> ... /usr/bin/php -f /var/www/html/the_customer/cron/cron.php"

如果没有引号转义,事情会好一点:

> (crontab -l; echo "* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php" )
no crontab for username
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php

您可能想要添加一些保护以防止重复条目,以下是 crontab 在几次调用后的处理方式:

> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:10 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:09 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:09 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:08 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:07 2015)
# (Cronie version 4.2)
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php

关于php - shell 通过 PHP : Trouble adding new cron job to crontab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com