Re: [PHP] PHP CLI Problem
On Feb 6, 2008 6:56 PM, Chris <[EMAIL PROTECTED]> wrote: > > > In your cronjob, replace the php5 entry with the following > > (including the backticks): > > `which php5` > > > > If that still doesn't work, replace it simply with php, not php5: > > `which php` > > You can use 'env php' instead and it'll pick up the first one in $PATH > (same sort of idea as 'which' though). > > So the job would be: > > env php -f /path/to/file.php > > Also while the curl suggestion may work, it's going to (eventually) have > timeout issues and stop working if the backup's become too large. Unless then you employ ignore_user_abort(1); et al. -- Daniel P. Brown Senior Unix Geek -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP CLI Problem
In your cronjob, replace the php5 entry with the following (including the backticks): `which php5` If that still doesn't work, replace it simply with php, not php5: `which php` You can use 'env php' instead and it'll pick up the first one in $PATH (same sort of idea as 'which' though). So the job would be: env php -f /path/to/file.php Also while the curl suggestion may work, it's going to (eventually) have timeout issues and stop working if the backup's become too large. -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP CLI Problem
On Feb 6, 2008 12:25 PM, Nathan Nobbe <[EMAIL PROTECTED]> wrote: > put the script in the webroot; then you can invoke it from cron > using curl; i imagine they have that installed. you can protect > the script with the following as the first line > if($_SERVER['REMOTE_ADDR'] !== '127.0.0.1') { die; } > > then you could hit if from cron as in > `which curl` http://localhost/backup.php > > im now waiting for what will surely be moments for another member > of the list to hack this idea to pieces ;) > but it was fun to think up! No, it's a valid idea, Nate. However, additional suggestions when going this route: 1.) Stop all public output from the script, regardless of whether or not it's only allowed by localhost. 2.) Still, move the databases and backups out of the web root. 3.) Instead of using `which curl` in the cron, use the *nix-only GET command (case sensitive). -- Daniel P. Brown Senior Unix Geek -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP CLI Problem
On Feb 6, 2008 11:48 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: > I contacted 1&1 and was told scripting was not supported. I wrote back > saying I didn't need help writing the script just how to run it. They wrote > back telling me to put it in a cron job using crontab –e and that was all > **rolls eyes**. Perhaps a call will give me the answers I need. > rob, please keep list topics on list for the benefit of others. if you dont get the answer you looking for; allow me to propose a dirty workaround. put the script in the webroot; then you can invoke it from cron using curl; i imagine they have that installed. you can protect the script with the following as the first line if($_SERVER['REMOTE_ADDR'] !== '127.0.0.1') { die; } then you could hit if from cron as in `which curl` http://localhost/backup.php im now waiting for what will surely be moments for another member of the list to hack this idea to pieces ;) but it was fun to think up! -nathan
Re: [PHP] PHP CLI Problem
On Feb 6, 2008 11:42 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: > Not an option. Which php gives me php4 and the script requires php5. > it sounds like you are using 1&1, per dans security warning earlier :) you may want to contact them and ask them how to run php5 scripts via the cli; as i know they allow usage of both 4 and 5 from the webserver, which is configurable via .htaccess directives. and, btw, if you do find out, let us know; i for one am curious how they are doing this (if at all..). -nathan
RE: [PHP] PHP CLI Problem
Not an option. Which php gives me php4 and the script requires php5. From: Nathan Nobbe [mailto:[EMAIL PROTECTED] Sent: Wednesday, February 06, 2008 8:39 AM To: Robbert van Andel Cc: Daniel Brown; php-general@lists.php.net Subject: Re: [PHP] PHP CLI Problem On Feb 6, 2008 11:33 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: I tried adding `which php5` and that didn't work. did you try running which php5 on the cli to ensure it even maps to a path ? you might need which php instead. -nathan
Re: [PHP] PHP CLI Problem
On Feb 6, 2008 11:33 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: > I tried adding `which php5` and that didn't work. did you try running which php5 on the cli to ensure it even maps to a path ? you might need which php instead. -nathan
RE: [PHP] PHP CLI Problem
D'oh. I wondered why I got that email while I was driving. Thanks for the security tip. I had actually planned on protecting those scripts after I was done getting them to work. I removed the shebang, verified the script still works from the command line but it still doesn't work with Cron. I tried adding `which php5` and that didn't work. If I run it as php, then I'm stuck using php4, so that's not really an option. Cron works on the server because I have several perl scripts running through cron with no problems. In fact, I would probably give up on this given the problems I'm having and write in perl, but the perl modules for Amazon's S3 service are poorly documented and I was not able to figure them out. There are no cron logs and the output is not sent to me after a job has completed. -Original Message- From: Daniel Brown [mailto:[EMAIL PROTECTED] Sent: Wednesday, February 06, 2008 7:52 AM To: Robbert van Andel Cc: php-general@lists.php.net Subject: Re: [PHP] PHP CLI Problem On Feb 6, 2008 1:55 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: [snip] > First off, the script runs great from the command line when I type "php5 > backup.php" but when I type ./backup.php I get an error: "bash: > ./backup.php: No such file or directory". I thought maybe this is a problem > with the top declaration in the script "#!/usr/local/bin/php5". The problem > is that it appears the server has several php5s I can reference See my note on this paragraph later in the message. And more importantly, see the addendum "SECURITY NOTICE" at the tail-end of this email. > /usr/local/apache/share/cgi-bin/php5 > /usr/local/bin/php5 > > But it doesn't matter which one I put at the top of the script, I get the > same error. In this case, use /usr/local/bin/php5, which will almost undoubtedly be compiled as a CLI object, while the ../apache/share/cgi-bin/php5 is most likely a CGI binary or (less likely) Apache module. However, keep this part in mind, as with the first paragraph, for later in the message. > Okay, so I can live with having to type "php5 backup.php". However, when I > try to make a cron job from the script, the script never runs. The crontab > entry looks like this > 1 0 * * 2 php5 > /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/ > backup.php > backup.log In your cronjob, replace the php5 entry with the following (including the backticks): `which php5` If that still doesn't work, replace it simply with php, not php5: `which php` This allows BASh/Shell to execute the eval'd command typed within the backticks. > I know the backup never runs because I redirect the output to a file and > have an email sent to me upon conclusion of the script. The log file > doesn't show anything nor do I ever receive an email. My web host will not > provide any support for scripting, so I'm hoping someone here can help. Are you certain that the user under which the script is executed via cron is yourself? On rare occasions (some BSD boxes, Cobalt RaQ RHL variants, et cetera), cron will not always run as the local user. It may run as 'cron', 'crond', 'daemon', 'nobody', 'unpriv', 'anon', or as whomever the sysop has it configured. > Questions: > * How do I determine what to put at the top of the script so that I can just > call "backup.php"? Doing that is not such a great idea, and here's where the first two paragraphs come into play: remove the interpreter designation line (the first line, containing the #! characters). Make sure the script is enclosed in tags. You can find scripts that written to specifically run as crons that instead have the interpreter designator, but it's just one more thing you'll have to manually change when porting from one system to another, or if your host updates their configuration. PLUS: Keep in mind that, even when using an interpreter designator, you *still* have to enclose your code in the tags (or, if short_open_tags is on, which it probably is, just use tags). Why? Because PHP is set to parse *only* code between those tags, and even if you run the CLI with the -r flag (which allows you to run code without the tags), it will only run /one line/ of code per call. (See: `which php` --help -OR- `which php5` --help) So if you absolutely want to run it as ./backup.php from the local directory, or /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/ backup.php, here are the steps to do so: 1.) chmod 755 /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/ backup.php 2.) Line 1 (backup.php): #!/usr/local/bin/php5 (or whatever 'which php5' or &
Re: [PHP] PHP CLI Problem
On Feb 6, 2008 1:55 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: [snip] > First off, the script runs great from the command line when I type "php5 > backup.php" but when I type ./backup.php I get an error: "bash: > ./backup.php: No such file or directory". I thought maybe this is a problem > with the top declaration in the script "#!/usr/local/bin/php5". The problem > is that it appears the server has several php5s I can reference See my note on this paragraph later in the message. And more importantly, see the addendum "SECURITY NOTICE" at the tail-end of this email. > /usr/local/apache/share/cgi-bin/php5 > /usr/local/bin/php5 > > But it doesn't matter which one I put at the top of the script, I get the > same error. In this case, use /usr/local/bin/php5, which will almost undoubtedly be compiled as a CLI object, while the ../apache/share/cgi-bin/php5 is most likely a CGI binary or (less likely) Apache module. However, keep this part in mind, as with the first paragraph, for later in the message. > Okay, so I can live with having to type "php5 backup.php". However, when I > try to make a cron job from the script, the script never runs. The crontab > entry looks like this > 1 0 * * 2 php5 > /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/ > backup.php > backup.log In your cronjob, replace the php5 entry with the following (including the backticks): `which php5` If that still doesn't work, replace it simply with php, not php5: `which php` This allows BASh/Shell to execute the eval'd command typed within the backticks. > I know the backup never runs because I redirect the output to a file and > have an email sent to me upon conclusion of the script. The log file > doesn't show anything nor do I ever receive an email. My web host will not > provide any support for scripting, so I'm hoping someone here can help. Are you certain that the user under which the script is executed via cron is yourself? On rare occasions (some BSD boxes, Cobalt RaQ RHL variants, et cetera), cron will not always run as the local user. It may run as 'cron', 'crond', 'daemon', 'nobody', 'unpriv', 'anon', or as whomever the sysop has it configured. > Questions: > * How do I determine what to put at the top of the script so that I can just > call "backup.php"? Doing that is not such a great idea, and here's where the first two paragraphs come into play: remove the interpreter designation line (the first line, containing the #! characters). Make sure the script is enclosed in tags. You can find scripts that written to specifically run as crons that instead have the interpreter designator, but it's just one more thing you'll have to manually change when porting from one system to another, or if your host updates their configuration. PLUS: Keep in mind that, even when using an interpreter designator, you *still* have to enclose your code in the tags (or, if short_open_tags is on, which it probably is, just use tags). Why? Because PHP is set to parse *only* code between those tags, and even if you run the CLI with the -r flag (which allows you to run code without the tags), it will only run /one line/ of code per call. (See: `which php` --help -OR- `which php5` --help) So if you absolutely want to run it as ./backup.php from the local directory, or /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/backup.php, here are the steps to do so: 1.) chmod 755 /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/backup.php 2.) Line 1 (backup.php): #!/usr/local/bin/php5 (or whatever 'which php5' or 'which php' says - no backticks here) 3.) Enclose your code in tags, not including the interpreter designator (first line). > * What, if anything, do I need to do to make the script work from cron? See above answers. SECURITY NOTICE Here are some things to consider in this specific situation: 1.) NEVER disclose full server path information as you did in your email. From that, I was easily able to discover the domain name, based upon the fact that Oregon is a US state and "swimming" would indicate the interests were most likely of a group or charitable ORGanization. ;-P 2.) It's always a Bad Idea[tm] to place crons of that nature in the web path. I was able to run the cron without the need of logging into your admin panel, because there was no security to stop me. And while all it will do is create a backup of your databases in this particular case, the output from that file also gives me *highly sensitive*, critical information about the location of web path information, and ABSOLUTE WORST - gives me full access to all of your database information in plain-text, and even a convenient zip file. ACTION ITEMS: 1.) *IMMEDIATELY* move the 'backups' directory out of the webroot. Place it somewhere like /kunden/homepages/23/d1179
Re: [PHP] PHP CLI Problem
Robbert van Andel wrote: I am having trouble with a PHP CLI script I wrote to help manage my website. The site is on a shared hosting server where the PHP installation is set up as a CGI. The script creates some backups of my databases and sends them to Amazon's S3 service. First off, the script runs great from the command line when I type "php5 backup.php" but when I type ./backup.php I get an error: "bash: ./backup.php: No such file or directory". I thought maybe this is a problem with the top declaration in the script "#!/usr/local/bin/php5". The problem is that it appears the server has several php5s I can reference /usr/local/apache/share/cgi-bin/php5 /usr/local/bin/php5 But it doesn't matter which one I put at the top of the script, I get the same error. Okay, so I can live with having to type "php5 backup.php". However, when I try to make a cron job from the script, the script never runs. The crontab entry looks like this 1 0 * * 2 php5 /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/ backup.php > backup.log Are you trying to add this to your /etc/crontab file directly, or are you issuing crontab -e from the command line? If you are entering the above line into the crontab directly, then you are missing a parameter. Between the day of the week and where you have php5, you need to add the user that the script is suppose to run as. Since you are able to run the script manually, it must be something to do with how/where you are entering it into cron and/or the environment variables available to the user that your crontag launches the script as. Maybe without a complete login, the cron user doesn't have the /usr/local/bin/ in its PATH ? Just a couple thoughts. I know the backup never runs because I redirect the output to a file and have an email sent to me upon conclusion of the script. The log file doesn't show anything nor do I ever receive an email. My web host will not provide any support for scripting, so I'm hoping someone here can help. Questions: * How do I determine what to put at the top of the script so that I can just call "backup.php"? * What, if anything, do I need to do to make the script work from cron? I've seen some comments on the web regarding PHP CLI when PHP is a CGI, but none of them seem to apply to me. Thank you in advance for your help. Robbert -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP CLI Problem
On Feb 6, 2008 9:36 AM, Robbert van Andel <[EMAIL PROTECTED]> wrote: > I guess I spoke too soon. Even though the script runs from anywhere (that > I > have access too), when I put the perl script in a cronjob, the php script > just refuses to run. There's no output, just the output from the perl > script. And yes, I did set up the perl script to print the output of the > php script. Any ideas? Is there some special consideration I have to > give > to the fact that my PHP installation is setup as a CGI? I've never had > this > much trouble getting a PHP CLI script to run. there are 2 ways to run a php cli script. the first is to prefix the name of the script with php; eg php backup.php if you are specifying this in a cron job, you will have to supply the full path to the php interpreter, which you can find via which php (sorry jocheem, i know you already said that, but my system doesnt have php5, only php [even though its 5] so i thought id mention it again) that is the way i prefer to do it but i wont elaborate as there is no point now. the second way is to make the script executable; this is a 2 step process. the first step is to prefix the contents of the file with #!/path/to/php/binary where the path is the same as earlier, found via the which command. the second step is to mark the script as executable, hopefully you are familiar enough to do that yourself, but just to test it as the user who created it, you can do simply, chmod u+x backup.php i suspect in your case the cron job doesnt know the path to the php binary, ive encountered this before; although its strange its still occurring within the perl script; however i cant help you with that ;) o, btw; here is the relevant page in the manual http://us2.php.net/manual/en/features.commandline.php -nathan
RE: [PHP] PHP CLI Problem
I guess I spoke too soon. Even though the script runs from anywhere (that I have access too), when I put the perl script in a cronjob, the php script just refuses to run. There's no output, just the output from the perl script. And yes, I did set up the perl script to print the output of the php script. Any ideas? Is there some special consideration I have to give to the fact that my PHP installation is setup as a CGI? I've never had this much trouble getting a PHP CLI script to run. Robbert -Original Message- From: Robbert van Andel [mailto:[EMAIL PROTECTED] Sent: Wednesday, February 06, 2008 6:14 AM To: php-general@lists.php.net Subject: RE: [PHP] PHP CLI Problem Thank you, that was very helpful. I created a perl script because the shell script wouldn't run either. It too gave an error stating the file or directory could not be found. The perl script runs a shell command from within it to run backup.php. Manual tests show that that works, I put it in a cron job and am confident that it will run. Robbert -Original Message- From: Chris [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 05, 2008 11:06 PM To: Robbert van Andel Cc: php-general@lists.php.net Subject: Re: [PHP] PHP CLI Problem > First off, the script runs great from the command line when I type "php5 > backup.php" but when I type ./backup.php I get an error: "bash: > ./backup.php: No such file or directory". Maybe backup.php that you think it's running is the wrong one. Add something like: echo "I am file " . __FILE__ . "\n"; in your backup.php file and see if that's printed out when you run "php5 backup.php". If it's not, then you're editing the wrong file. > I thought maybe this is a problem > with the top declaration in the script "#!/usr/local/bin/php5". If you have that as the first line, then you should just need to chmod +x the backup.php file. > The problem > is that it appears the server has several php5s I can reference > > /usr/local/apache/share/cgi-bin/php5 > /usr/local/bin/php5 Ask your host which is the right one (99% sure it'll be /usr/local/bin/php5). > Questions: > * How do I determine what to put at the top of the script so that I can just > call "backup.php"? What difference does it make whether the full path is included in the cron job or not? You need to use the full path somewhere (whether it's in the cron job or in a shell script). You could create a shell script: #!/bin/bash /usr/local/bin/php5 -f /path/to/backup.php chmod +x the new script. Call that instead in cron. > * What, if anything, do I need to do to make the script work from cron? Use the full path to php, use the full path to your backup.php file. -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] PHP CLI Problem
Thank you, that was very helpful. I created a perl script because the shell script wouldn't run either. It too gave an error stating the file or directory could not be found. The perl script runs a shell command from within it to run backup.php. Manual tests show that that works, I put it in a cron job and am confident that it will run. Robbert -Original Message- From: Chris [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 05, 2008 11:06 PM To: Robbert van Andel Cc: php-general@lists.php.net Subject: Re: [PHP] PHP CLI Problem > First off, the script runs great from the command line when I type "php5 > backup.php" but when I type ./backup.php I get an error: "bash: > ./backup.php: No such file or directory". Maybe backup.php that you think it's running is the wrong one. Add something like: echo "I am file " . __FILE__ . "\n"; in your backup.php file and see if that's printed out when you run "php5 backup.php". If it's not, then you're editing the wrong file. > I thought maybe this is a problem > with the top declaration in the script "#!/usr/local/bin/php5". If you have that as the first line, then you should just need to chmod +x the backup.php file. > The problem > is that it appears the server has several php5s I can reference > > /usr/local/apache/share/cgi-bin/php5 > /usr/local/bin/php5 Ask your host which is the right one (99% sure it'll be /usr/local/bin/php5). > Questions: > * How do I determine what to put at the top of the script so that I can just > call "backup.php"? What difference does it make whether the full path is included in the cron job or not? You need to use the full path somewhere (whether it's in the cron job or in a shell script). You could create a shell script: #!/bin/bash /usr/local/bin/php5 -f /path/to/backup.php chmod +x the new script. Call that instead in cron. > * What, if anything, do I need to do to make the script work from cron? Use the full path to php, use the full path to your backup.php file. -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP CLI Problem
Robbert van Andel schreef: I am having trouble with a PHP CLI script I wrote to help manage my website. The site is on a shared hosting server where the PHP installation is set up as a CGI. The script creates some backups of my databases and sends them to Amazon's S3 service. First off, the script runs great from the command line when I type "php5 backup.php" but when I type ./backup.php I get an error: "bash: ./backup.php: No such file or directory". is the script marked as executable? I thought maybe this is a problem with the top declaration in the script "#!/usr/local/bin/php5". The problem is that it appears the server has several php5s I can reference /usr/local/apache/share/cgi-bin/php5 /usr/local/bin/php5 to determine which php5 you are using type: $> which php5 this gives you the full path, which you will need to use in the relevant cron line (and/or the shebang line at the top of the script) - cron requires fullpaths. But it doesn't matter which one I put at the top of the script, I get the same error. Okay, so I can live with having to type "php5 backup.php". However, when I try to make a cron job from the script, the script never runs. The crontab entry looks like this 1 0 * * 2 php5 /kunden/homepages/23/d117947228/htdocs/oregonswimming/administration/backup/ backup.php > backup.log I know the backup never runs because I redirect the output to a file and have an email sent to me upon conclusion of the script. The log file doesn't show anything nor do I ever receive an email. My web host will not provide any support for scripting, so I'm hoping someone here can help. Questions: * How do I determine what to put at the top of the script so that I can just call "backup.php"? * What, if anything, do I need to do to make the script work from cron? I've seen some comments on the web regarding PHP CLI when PHP is a CGI, but none of them seem to apply to me. Thank you in advance for your help. Robbert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP CLI Problem
First off, the script runs great from the command line when I type "php5 backup.php" but when I type ./backup.php I get an error: "bash: ./backup.php: No such file or directory". Maybe backup.php that you think it's running is the wrong one. Add something like: echo "I am file " . __FILE__ . "\n"; in your backup.php file and see if that's printed out when you run "php5 backup.php". If it's not, then you're editing the wrong file. I thought maybe this is a problem with the top declaration in the script "#!/usr/local/bin/php5". If you have that as the first line, then you should just need to chmod +x the backup.php file. The problem is that it appears the server has several php5s I can reference /usr/local/apache/share/cgi-bin/php5 /usr/local/bin/php5 Ask your host which is the right one (99% sure it'll be /usr/local/bin/php5). Questions: * How do I determine what to put at the top of the script so that I can just call "backup.php"? What difference does it make whether the full path is included in the cron job or not? You need to use the full path somewhere (whether it's in the cron job or in a shell script). You could create a shell script: #!/bin/bash /usr/local/bin/php5 -f /path/to/backup.php chmod +x the new script. Call that instead in cron. * What, if anything, do I need to do to make the script work from cron? Use the full path to php, use the full path to your backup.php file. -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php