2015-11-25 44 views
0

我有几个脚本计划在我的crontab中运行,但只能看到它们作为根(或使用sudo)。我需要一个PHP脚本(它以nginx的形式运行)能够将新行添加到crontab文件中。为此,我创建了一个shell脚本(由root拥有),并通过/etc/sudoers文件将nginx用户的权限授予sudo通过PHP Shell:无法添加新的cron作业到crontab

/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:

[[email protected] 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元信息文件:

[[email protected] nginx]$ pwd 
/etc/nginx 
[[email protected] 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 

我在做什么错误或丢失,为什么?

回答

1

注意:从安全角度来看,在root的帐户下运行与系统管理员无关的cron作业是不良想法。在nginx用户下安装您的cron。

的问题是由您的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 
+0

谢谢,我会考虑收紧安全。删除引号排除了错误信息,但'crontab -e'仍然有相同的内容,这很奇怪;我希望有一个错误或内容被添加,但都没有发生。 – Bing

+0

您是否正在检查正确用户的crontab(如果在sudo下执行,调用脚本或'root')? –

+0

调用脚本的一个是'nginx',当我尝试给他们''su'或者像他们一样运行命令时('runuser -l nginx -c'crontab -l''),我得到消息'This Account is currently不可用.'我试图在'root'下运行它,这就是为什么我把'nginx ALL = NOPASSWD:/ etc/nginx/addcron.sh'加到'/ etc/sudoers'中的原因,但'sudo crontab -l'不显示新条目,这就是为什么我很困惑,因为没有错误信息。 – Bing

0

我最近发布了一个项目,该项目允许PHP获取并与真正的Bash shell进行交互。在这里获得:https://github.com/merlinthemagic/MTS

下载您只需使用下面的代码后:

$custname= "someone"; 
$strCmd = "echo \"* * * * * /usr/bin/php -f /var/www/html/".$custname."\" >> /cron/cron.php"; 

$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true); 
$return1 = $shell->exeCmd($strCmd); 
//assuming your distribution reloads/restarts using 'service' 
$return2 = $shell->exeCmd('service crond restart'); 

这将你的行追加到cron文件。