从特定目录运行Cronjob [英] Running Cronjob from a specific directory

查看:92
本文介绍了从特定目录运行Cronjob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于生成RSS提要的php脚本,该脚本最终将通过Cronjob运行.

I created a php script for generating RSS feeds which is to eventually be run via a Cronjob.

所有php文件和生成的RSS xml都将位于网站的子文件夹中.如果我在本地开发计算机上的同一目录中使用终端或浏览器,则php脚本可以在本地开发人员上正常运行.

All the php files and the resulting RSS xml will be within a sub folder in a website. The php script runs fine on my local dev if I use terminal or the browser while within the same directory on my local development machine.

e.g. php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php

工作正常,就像在Chrome中导航到dorss.php文件一样.

works fine as does navigating to the dorss.php file in Chrome.

尽管CronJob已执行,但存在以下错误,该错误与以下事实有关:它找不到与require.once()指定的文件,这些文件位于rss所在的同一文件夹或该文件夹的子文件夹中.

The CronJob has executed though with errors related to the fact that it cannot find the files specified with require_once() which are located in the same folder as rss or in a subfolder of that.

长话短说,我需要Cronjob从与dorss.php文件相同的目录中运行,以便它可以正确地引用包含文件.

Long story short I need to have the Cronjob run from within the same directory as the dorss.php file so it can reference the include files correctly.

我对设置cronjob的知识非常有限,所以我想问一下是否有可能(在运行命令之前更改目录)在crontab的同一命令行上执行此操作,或者是否可以实现请吗?

My knowledge on setting up a cronjob is VERY limited so I wanted to ask if this is at all possible (Change directory before running command) to do this on the same command line of the crontab or if not how can it be achieved please?

当前的cronjob命令是

The current cronjob command is

0 0,6,12,18 * * * /usr/bin/php /var/www/vhosts/clientname/stagingsite/rss/dorss.php

TIA 约翰

推荐答案

我不知道是否有更好的解决方案,但我建议在执行PHP文件之前编写一个可以更改为正确目录的bash脚本,并且使用cron执行此脚本.例如:

I don't know if there's a better solution, but I would suggest to write a bash script that changes to the correct directory before executing the PHP file, and use cron to execute this script. E.g:

#!/bin/bash
cd /Library/WebServer/Documents/clientname/siteroot/rss/
php dorss.php

甚至只是:

#!/bin/bash
php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php

将其保存在某个地方,然后使用cron来运行它.

Save it somewhere and use cron to run that.

这篇关于从特定目录运行Cronjob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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