使用PHP创建cron作业 [英] Use PHP to create cron job

查看:88
本文介绍了使用PHP创建cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过网络界面制作一个闹钟(在我的Raspberry Pi上)。
我想用PHP和crontab做到这一点。

这是我的PHP代码:

  shell_exec( ./ wake.sh $ minutes $ hours $ days); 

这是wake.sh脚本:

  echo数字1:$ 1;数字2:$ 2;数字3:$ 3 |壁; 
(crontab -u $ USER -l;回显 $ 0 $ 1 * * $ 2 /var/www/alarm.sh)| crontab -u $ USER-

如果我从bash运行脚本(来自www数据用户) ,我在crontab文件中获得了广播消息和一个条目,但是如果我从PHP脚本运行它,则会发送广播消息,但是crontab文件中没有条目。

$ USER 变量为空,如果我是从PHP执行的话。
现在我使用:

 (crontab -l; echo $ 0 $ 1 * * $ 2 / var / www / alarm .sh)| crontab-

它有效!


I'm trying to make an alarm clock with a webinterface (on my Raspberry Pi). I want to make this with PHP and crontab.
This is my PHP code:

shell_exec("./wake.sh $minutes $hours $days");    

This is the wake.sh script:

echo "number 1: $1; number 2: $2; number 3: $3" | wall;
(crontab -u $USER -l; echo "$0 $1 * * $2 /var/www/alarm.sh") | crontab -u $USER -

If I run the script from bash (from the www-data user), I get the broadcast message and an entry in the crontab file, but if I run it from the PHP script, the broadcast message is sent, but there is no entry in the crontab file.

解决方案

I finally found out the answer: the $USER variable is empty, if I execute it from PHP. Now I use:

(crontab -l; echo "$0 $1 * * $2 /var/www/alarm.sh") | crontab -

and it works!

这篇关于使用PHP创建cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆