使用php脚本安装cron作业 [英] Install a cron job with a php script

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

问题描述

我正在开发一个需要使用Cron的网络应用程序。我想让它很容易设置与自动安装过程,如Wordpress。我没有问题,直到安装脚本,直到它的时间来设置Cron。请告诉我是否可以这样做。

I'm developing a web application that requires the use of Cron. I'd like to make it easy to setup with an auto install process like Wordpress. I have no problems writing the install script up en till its time to set up Cron. Please tell me if I can do this.

推荐答案

您只需创建cron文件,然后使用exec设置cron:

You just have to create the cron file, then use exec to set up that cron:

$cron_file = 'cron_filename';
// Create the file
touch($cron_file); 
// Make it writable
chmod($cron_file, 0777); 
// Save the cron
file_put_contents($cron_file, '* * * * * your_command'); 
// Install the cron
exec('crontab cron_file');

这要求运行PHP的用户有权创建crontabs。这个cron文件将默认替换该用户的任何其他cron,所以一定要问用户是否他想应用cron。还要确保写入crontab文件的文件夹是可写的。

This requires that the user which PHP is run under has the right to make crontabs. This cron file will by default replace any other crons for that user, so make sure to ask the user if he wants to apply the cron. Also make sure the folder you're writing the crontab file in is writable.

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

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