Run PHP script from Linux Shell

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ferreira.jorge
Forum Newbie
Posts: 11
Joined: Tue Nov 14, 2006 6:13 am
Location: Portugal

Run PHP script from Linux Shell

Post 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
Last edited by ferreira.jorge on Fri Nov 17, 2006 9:26 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I see somebody from a Perl background :)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
User avatar
ferreira.jorge
Forum Newbie
Posts: 11
Joined: Tue Nov 14, 2006 6:13 am
Location: Portugal

Nop. Not working

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ferreira.jorge
Forum Newbie
Posts: 11
Joined: Tue Nov 14, 2006 6:13 am
Location: Portugal

Post 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]
Last edited by ferreira.jorge on Wed Nov 22, 2006 11:56 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That's what it looks like. Check that the path is correct (ie, not /usr/local/bin or something similar)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
themurph
Forum Commoner
Posts: 76
Joined: Wed Apr 19, 2006 1:56 pm
Contact:

Post by themurph »

From a command shell, type "which php" to get the proper path you your PHP executable.
User avatar
ferreira.jorge
Forum Newbie
Posts: 11
Joined: Tue Nov 14, 2006 6:13 am
Location: Portugal

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

your cron user may not have the access required to run php.
User avatar
ferreira.jorge
Forum Newbie
Posts: 11
Joined: Tue Nov 14, 2006 6:13 am
Location: Portugal

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

have you "chmod +x" your php file?
User avatar
ferreira.jorge
Forum Newbie
Posts: 11
Joined: Tue Nov 14, 2006 6:13 am
Location: Portugal

Post by ferreira.jorge »

Yes. Everyone can execute this file... (755)
Post Reply