Page 1 of 2

Run PHP script from Linux Shell

Posted: Tue Nov 14, 2006 6:25 am
by ferreira.jorge
Hi everyone.

I've been trying to create a script that is supposed to run in crontab.
I've allready tested it in Browser and everything works well. Then, i added a line on the top of the script like this: #!/usr/bin/php -q.

When i try to run it from the command shell (php script.php) on suse linux 9 Pro, it works but everytime i try to do it from the crontab, it returns me this error:

Code: Select all

Usage: php [options] [-f] <file> [args...]
       php [options] -r <code> [args...]
       php [options] [-- args...]
  -a               Run interactively
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument 
                   starts with - or script is read from stdin
Looks like i'm missing something here.

The line in crontab looks like this: 30 0 * * 2-4 root php /srv/www/htdocs/script.php

Posted: Tue Nov 14, 2006 7:26 am
by timvw
The easiest (imho) is to remove the she-bang line (#!/usr/bin/php -q) and in your crontab use the complete path to php...

x x x x x /usr/bin/php /home/user/script.php

Posted: Tue Nov 14, 2006 8:51 am
by Jenk
for a start.. there's no '-q' option according to the usage help..

and the definition line should read:

Code: Select all

#!/usr/bin/php

Posted: Tue Nov 14, 2006 9:47 am
by Chris Corbyn
I see somebody from a Perl background :)

Posted: Tue Nov 14, 2006 11:26 am
by BDKR
Jenk wrote:for a start.. there's no '-q' option according to the usage help..

and the definition line should read:

Code: Select all

#!/usr/bin/php
Perhaps not anymore. It was there before to suppress HTTP output. To the best of my knowledge, all CLI installs now do this on their own.

Nop. Not working

Posted: Fri Nov 17, 2006 9:10 am
by ferreira.jorge
One of the things i don't understand is that i've got another script writen just the same way and it works. In shell, in browser and in crontab. This one doesn't.

Is it a good idea to paste the code and the crontab here? With just a minor changes for security reasons.

Thank you for all your replies so far.

Posted: Fri Nov 17, 2006 10:10 am
by pickle
You don't need invoke PHP in the crontab because you're referring to it in the file you're running. Remove the '-q' from your file & change your crontab to:

Code: Select all

30 0 * * 2-4 root /srv/www/htdocs/script.php
That should work fine.

Posted: Fri Nov 17, 2006 10:22 am
by ferreira.jorge
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Whoa!! That make some changes...

Here is the code that i'm executing:

Code: Select all

#!/usr/bin/php
<?
        $hoje = date("Y-m-d");
        require("./phpmailer/class.phpmailer.php");
//      include("config.php");

        mysql_connect("localhost","username","password") or die ("Não foi possíel a conexão ao Servidor MySQL. Por favor contacte o Administrador de Sistemas".mysql_error());

.....

        if(!$mail->Send())
        {
        echo "Menssagem não foi enviada<br>";
        echo "Erro do Mailer: " . $mail->ErrorInfo;        $mail->ClearAddresses();
        $mail->ClearAttachments();        exit;
        }
        echo "Mensagem enviada com sucesso para ".$mail_aval."<br>";
        $mail->ClearAddresses();
        $mail->ClearAttachments();



/* FIM ENVIO E-MAIL */

                        }
                }
        }
?>
And here it is the crontab:

Code: Select all

SHELL=/bin/bash
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin:/usr/bin/php
MAILTO=jorge.ferreira@mail.cmc
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * *   root  test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1
10 1  * * *     root /wherever/avaliar.php
30 0 * * 2-4     root /wherever/avaliacao.php
This was after i made the change you told me.
The system returned this error:

Code: Select all

/bin/bash: /srv/www/htdocs/formacao/avaliacao.php: /usr/bin/php
: bad interpreter: No such file or directory
It doesn't recognizes the interpreter in /usr/bin/php?!

Thank you for your reply.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Nov 20, 2006 9:48 am
by pickle
That's what it looks like. Check that the path is correct (ie, not /usr/local/bin or something similar)

Posted: Mon Nov 20, 2006 10:17 am
by themurph
From a command shell, type "which php" to get the proper path you your PHP executable.

Posted: Tue Nov 21, 2006 4:23 am
by ferreira.jorge
Thanks a lot everyone.
I found that the correct path to my php interpreter (thank you themurph) is in fact /usr/bin/php.
That's what it is in the script and still it doesn't run. It still says that this one is a bad interpreter. But it only does it in crontab. Every where (browser, or shell) else it runs ok.

Another thing very strange is that i've got another php script running on crontab and it runs ok!! Of corse i've compared them to see if there was anything i could do but, the script starts the same way as this, and this time the crontab doesn't say that it has a bad interpreter...
Does anyone have an idea?
Thank you for posting.

Posted: Tue Nov 21, 2006 5:16 am
by Jenk
your cron user may not have the access required to run php.

Posted: Tue Nov 21, 2006 5:22 am
by ferreira.jorge
I've tried it with root, www (user to run php scripts in apache), myself and the result is allways the same.
I'm running out of answers...is it not possible in Suse Linux?
Will this Linux let me down? :x

Posted: Tue Nov 21, 2006 6:28 am
by Jenk
have you "chmod +x" your php file?

Posted: Tue Nov 21, 2006 6:46 am
by ferreira.jorge
Yes. Everyone can execute this file... (755)