Discuss your project
PHP Shell Zend

Zend: Create a CLI file and activate it in the shell

To add a cron job and make it accessible in the shell, you first need to configure your environment to point to the libraries.Step 1: Add the zf script to the /usr/bin/ directory (zf.sh and zf.php) These files are available in the bin directory of the ZendF...

Zend: Create a CLI file and activate it in the shell

To add a cron job and make it accessible in the shell, you first need to configure your environment to point to the libraries.

Step 1: Add the zf script to the /usr/bin/ directory (zf.sh and zf.php)
These files are available in the bin directory of the ZendFramework 1.x library. You can either copy them or create a symbolic link.

Step 2: Configure your bash to recognize the zf alias.
Edit the ~/.bashrc file and insert the following line

alias zf="/usr/bin/zf.sh"

Step 3: Add the global variable ZEND_TOOL_INCLUDE_PATH to your bash. This will allow the zf.sh script to find the zend library
Edit the ~/.bashrc file and insert the following line

ZEND_TOOL_INCLUDE_PATH=/var/www/monProjet/library/Zend

Step 4: Create the skeleton for your CLI task.
Here is how I implement them. Of course, you are free to place your file wherever you like! Zend is flexible enough to be configured/adapted in different ways.
In this example, we have set up a library dedicated to our project called foo, and placed all our CLI files in a directory called "Cronjobs".
Therefore, our file will need to be placed in the directory: /var/www/myProject/library/Foo/Cronjobs/MytaskProvider.php
Our task will be called "Mytask" and its action will be "do".

class Foo_Cronjobs_MytaskProvider extends Zend_Tool_Framework_Provider_Abstract
{
public function DoitCronjobs()
{
echo "Hello World !";
}
}

All public functions will then be available as separate tasks.

Step 5: Register your task so that it is accessible from the list of Zend commands.
Edit the ~/.zf.ini file and insert the following line

php.include_path = "/var/www/monProjet/library/:.:/usr/share/php:/usr/share/pear"
basicloader.classes.21 = "Foo_Cronjobs_MytaskProvider"

To add a new task, simply increment the class (here 21, so move to 22... and so on).

Version context

This procedure describes the software and the environment available at the time of its publication. The packages, interfaces, and repositories mentioned may have changed or disappeared; first check the system version and its support level.

cat /etc/os-release
uname -r

Work on a backup or a snapshot, note every modified file, and prepare for rollback. For a current workstation or server, prefer signed packages from officially supported repositories.

Reference: official Ubuntu lifecycle.

Share this article