开发者

Using Cron with Cake Console on Ubuntu

开发者 https://www.devze.com 2023-01-07 11:45 出处:网络
I need this cron job to execute my shell just as it does when I run it on the command line. I read through the one other question I found about this, but my console-based cron job still is not workin

I need this cron job to execute my shell just as it does when I run it on the command line.

I read through the one other question I found about this, but my console-based cron job still is not working. I want to post some code and what it outputs, maybe someone can tell me what's going on.

First off, this is on Cake 1.3. I am running on Ubuntu 9.10. I have tried the shell-script method described in the Cake Book.

I have NOT established any special user account for running the script. The cake console is on my PATH (for the ubuntu built-in user).

In another question I found, they report that the -app parameter isn't doing anything. This seems to be the case for me as well.

My shell works as it should when I run this from the command line:

./vendors/cakeshell subscription_reminder -cli /usr/bin -app /var/www/www.example.org/htdocs/app -console /var/www/www.example.org/htdocs/cake/console/cake

the output from this looks like:

Welcome to CakePHP v1.3.2 Console
---------------------------------------------------------------
App : app
Path: /var/www/www.directory.sdcweb.org/htdocs/app
---------------------------------------------------------------

I'm logging my cron-job output to a file and the output of that looks different, like this:

EDIT: i've noticed that this following code block is just the cake shell script that comes with CakePHP, if you open up cake/console/cake in a text editor you should find the following script.

################################################################################
#
# Bake is a shell script for running CakePHP bake script
# PHP versions 4 and 5
#
# CakePHP(tm) :  Rapid De开发者_Python百科velopment Framework (http://cakephp.org)
# Copyright 2005-2010, Cake Software Foundation, Inc.
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
#
# @copyright            Copyright 2005-2010, Cake Software Foundation, Inc.
# @link                         http://cakephp.org CakePHP(tm) Project
# @package                      cake
# @subpackage           cake.cake.console
# @since                                CakePHP(tm) v 1.2.0.5012
# @license                      MIT License (http://www.opensource.org/licenses/mit-license.php)
#
################################################################################
LIB=${0/%cake/}
APP=`pwd`

exec php -q ${LIB}cake.php -working "${APP}" "$@"

exit;

My crontab looks like this:

41 20 * * * /var/www/www.example.org/htdocs/app/vendors/cakeshell subscription_reminder -cli /usr/bin -app /var/www/www.example.org/htdocs/app -console /var/www/www.example.org/htdocs/cake/console/cake >> /home/ubuntu/cron-log


First of all, your cronjob runs the script with a different cwd, which may affect behavior. Change it to

41 20 * * * cd /var/www/www.example.org/htdocs/app/; ./vendors/cakeshell ....

But maybe there's something else:

The "cakephp console", cakeshell, probably checks if it's stdin is connected to a tty. If not, it goes in some other, non-interactive mode.

The point is that some programs can talk to you in a terminal (a tty) and others can't.

Some can do both, depending on circumstances.

grep is typically non-interactive

vi, pico and nano are typically only used interactively:

$ vi > test3
Vim: Warning: Output is not to a terminal
$ echo bla | vi
Vim: Warning: Input is not from a terminal
$ vi < test3
Vim: Warning: Input is not from a terminal

bash can do both (non-interactive when running scripts, interactive when serving you in a terminal)

So there's at least the answer as to why the exact same command and environment, can give totally different output. Try to start it from the commandline as you did, but redirecting input from either a pipe or a file, and see what changes.


Well, after a bit more work on this I finally arrived at a crontab which does what I want. It looks like this:

35 01 * * * cd /var/www/www.example.org/htdocs/app; ../cake/console/cake subscription_reminder

Not only does it work, but it is also a lot more readable.


Does your cakeshell begin with

#!/bin/bash

As per the example on http://book.cakephp.org/view/1110/Running-Shells-as-cronjobs?

0

精彩评论

暂无评论...
验证码 换一张
取 消