I have a zendframework project for which i need to run a script periodically to upload the content of a folder and another do download. The script itself is ready but I am struggling to figure out where or how to set up the script to run. I have tried lynx and curl so far. I first had an error about specified controller being wrong and i fixed that but now I just get a blank screen when I run the script but file(s) are not uploaded.
For a zendframework project how do I setup script to be run by cron?
EDIT: My project structure looks like this:
mydomain.com
application
library
logs
public
index.php
scripts
cronjob.php
tests
cronjob.php is the script i need to run. The first few lines of which are:
<?php
define("_CRONJOB_",true);
require('/var/www/remotedomain.info/public/index.php');
I also modified my index.php file like below:
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
/** Cronjobs don't need all the extra's **/
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
However now when i now try to run the script, I get the message:
Message: Invalid controller specified (scripts).
Does it mean that I need to create a controller for the purpose? But the script folder is outside the application folder. How do i fix this?
Add command lines to the file, following the syntax described in "Syntax of crontab File Entries". The crontab file will be placed in /var/spool/cron/crontabs . Verify the crontab file by using the crontab -l command.
It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .
Thanks all for your answers. However the solution that worked for me came from this site Howto: Zend Framework Cron. The original link is dead, but its copy can be found on Internet Archive.
I am posting a cut of the code here. But please this is not my solution. All credits goes to the original author.
The trick with cronjobs is that you do not want to load the whole View part of ZF, we don't need any kind of HTML output! To get this to work, I defined a new constant in the cronjob.php which I will check for in the index.php.
define("_CRONJOB_",true);
require('/var/www/vhosts/domain.com/public/index.php');
// rest of your code goes here, you can use all Zend components now!
date_default_timezone_set('Europe/Amsterdam');
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
/** Cronjobs don't need all the extra's **/
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
First, make sure you have this at the top of your PHP script (followed by the opening PHP tag):
#!/usr/bin/php
<?php
// ...
Second, make sure the permissions are right. For example, if you create the cron job under root, then I believe root will try to run the PHP script if I'm not mistaken. Likewise, if you create the cron job under a different user, they better have the correct permissions to the PHP script.
For example (note, depending on your server environment, the permissions will need to be adjusted accordingly. e.g. this is just a hypothetical example)
$ chmod 755 script.php
$ chown userThatRunsScriptWithCron script.php
If you want to see the current cron jobs for the current user you're logged in as, do this:
$ crontab -l
Or, if your cron job is set up in one of the folders such as cron.hourly, cron.weekly, etc, then you can view which user "owns" those jobs by doing this:
$ cat /etc/crontab
Then at the bottom of the file you'll see them.
Now, to setup the cron job run this command to open the editor:
$ crontab -e
Then enter your values:
1 2 3 4 5 php /path/to/script.php
Now save and close the file. Obviously, you're going to change 1 2 3 4 5
to something real and meaningful. For more information about that see this page (google "cron").
Disclaimer: I am not a cron master by any means. Please correct me on any of this if I'm wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With