RE: [PHP] exec from process (deamon) without shell
> -Original Message- > From: Richard Lynch [mailto:[EMAIL PROTECTED] > Sent: Friday, March 02, 2007 12:37 AM > To: [EMAIL PROTECTED] > Cc: Roman Neuhauser; 'PHP Mailing' > Subject: Re: [PHP] exec from process (deamon) without shell > > Add a nice clean line to the end of your PHP scripts: > > exit 0; //exit cleanly > > If your PHP scripts' last function call returns, say, -1, and then PHP > just passes that back on out, it's probably going to look like an > error code to the outermost shell, if you see what I mean... > > I'm not promising this is how it actually works; this is just a theory. > [Peter Lauri - DWS Asia] I will take a look at that tomorrow; right now it is 1am and time for bed :) I will keep you updated on that one to inform you if your theory was working :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] exec from process (deamon) without shell
> -Original Message- > From: Richard Lynch [mailto:[EMAIL PROTECTED] > Sent: Friday, March 02, 2007 12:35 AM > To: [EMAIL PROTECTED] > Cc: Roman Neuhauser; 'PHP Mailing' > Subject: Re: [PHP] exec from process (deamon) without shell > > On Thu, March 1, 2007 12:56 pm, [EMAIL PROTECTED] wrote: > > I tried with /bin/ls but still same thing. I just found out that the > > exitcode is -1 but the $stdout actually gives correct answer. But that > > doesn't make sense. How can I then control what action to take > > depending on the exitcode? > > I *think* this could be the result of trying to ls a bunch of files, > some of which you don't have permission to 'ls', so you are getting a > partial list of stuff, but also an error code for trying to ls stuff > you shouldn't have been doing that too, if you see what I mean... > > I am hoping that your real script is a bit more complicated, as you > currently have a shell script that runs PHP only to run a shell script > that just does 'ls', which, really, you could probably just splat into > your shell script without PHP being involved... > [Peter Lauri - DWS Asia] It is a much more complicated logic, so therefore I am not using a shell script. I will examine how perl acts on executing the same commands and if that is catching 0 as error code I might move to run perl instead. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] exec from process (deamon) without shell
Add a nice clean line to the end of your PHP scripts: exit 0; //exit cleanly If your PHP scripts' last function call returns, say, -1, and then PHP just passes that back on out, it's probably going to look like an error code to the outermost shell, if you see what I mean... I'm not promising this is how it actually works; this is just a theory. Thu, March 1, 2007 5:12 pm, [EMAIL PROTECTED] wrote: > Quoting Roman Neuhauser <[EMAIL PROTECTED]>: > >> # [EMAIL PROTECTED] / 2007-03-01 18:28:35 +0200: >>> #/thepathtophp >>> >> >>> exec("ls -lrt", $stdout, $exitcode); >>> >>> if($exitcode!=0) { >>> echo "Command ls -lrt could not be executed correctly. >>> Exit code: $exitcode\n"; >>> } else { >>> echo "Result:\n"; >>> foreach($stdout AS $line) { >>> echo $line."\n"; >>> } >>> } >>> >>> ?> >>> >>> >>> This code give the exit code -1 that is equal to that "ls -lrt" >>> could not be >>> executed. >> >> Does the daemon run chrooted somewhere? >> What does $PATH contain? >> What happens if you use /bin/ls? >> >> >> -- >> How many Vietnam vets does it take to screw in a light bulb? >> You don't know, man. You don't KNOW. >> Cause you weren't THERE. http://bash.org/?255991 >> > > Becuase this only is having problems when executing from the deamon I > added: > > exec("php -i", $stdout, $exitcode); > > And then put it into a logfile. Is there anything here that makes > $exitcode for all commands executed -1, but they still seams to be > functional as $stdout is as expected: > > > 2007-03-02 00:00:54 Environment > 2007-03-02 00:00:54 > 2007-03-02 00:00:54 Variable => Value > 2007-03-02 00:00:54 TERM => xterm > 2007-03-02 00:00:54 PATH => > /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin > 2007-03-02 00:00:54 _ => /usr/bin/php > 2007-03-02 00:00:54 PWD => /var/www/util > 2007-03-02 00:00:54 LANG => C > 2007-03-02 00:00:54 SHLVL => 6 > 2007-03-02 00:00:54 > 2007-03-02 00:00:54 PHP Variables > 2007-03-02 00:00:54 > 2007-03-02 00:00:54 Variable => Value > 2007-03-02 00:00:54 _SERVER["TERM"] => xterm > 2007-03-02 00:00:54 _SERVER["PATH"] => > /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin > 2007-03-02 00:00:54 _SERVER["_"] => /usr/bin/php > 2007-03-02 00:00:54 _SERVER["PWD"] => /var/www/util > 2007-03-02 00:00:54 _SERVER["LANG"] => C > 2007-03-02 00:00:54 _SERVER["SHLVL"] => 6 > 2007-03-02 00:00:54 _SERVER["PHP_SELF"] => > 2007-03-02 00:00:54 _SERVER["SCRIPT_NAME"] => > 2007-03-02 00:00:54 _SERVER["SCRIPT_FILENAME"] => > 2007-03-02 00:00:54 _SERVER["PATH_TRANSLATED"] => > 2007-03-02 00:00:54 _SERVER["DOCUMENT_ROOT"] => > 2007-03-02 00:00:54 _SERVER["REQUEST_TIME"] => 1172786454 > 2007-03-02 00:00:54 _SERVER["argv"] => Array > 2007-03-02 00:00:54 ( > 2007-03-02 00:00:54 ) > 2007-03-02 00:00:54 > 2007-03-02 00:00:54 _SERVER["argc"] => 0 > 2007-03-02 00:00:54 _ENV["TERM"] => xterm > 2007-03-02 00:00:54 _ENV["PATH"] => > /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin > 2007-03-02 00:00:54 _ENV["_"] => /usr/bin/php > 2007-03-02 00:00:54 _ENV["PWD"] => /var/www/util > 2007-03-02 00:00:54 _ENV["LANG"] => C > 2007-03-02 00:00:54 _ENV["SHLVL"] => 6 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] exec from process (deamon) without shell
On Thu, March 1, 2007 12:56 pm, [EMAIL PROTECTED] wrote: > I tried with /bin/ls but still same thing. I just found out that the > exitcode is -1 but the $stdout actually gives correct answer. But that > doesn't make sense. How can I then control what action to take > depending on the exitcode? I *think* this could be the result of trying to ls a bunch of files, some of which you don't have permission to 'ls', so you are getting a partial list of stuff, but also an error code for trying to ls stuff you shouldn't have been doing that too, if you see what I mean... I am hoping that your real script is a bit more complicated, as you currently have a shell script that runs PHP only to run a shell script that just does 'ls', which, really, you could probably just splat into your shell script without PHP being involved... -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] exec from process (deamon) without shell
The thing is that the commands are executed as they should. I have tested to give full path "/bin/ls" and that gives the same result. BUT the exit code is -1 still. Not 0 as when running it from command line. Best regards, Peter Lauri www.dwsasia.com - company web site www.lauri.se - personal web site www.carbonfree.org.uk - become Carbon Free -Original Message- From: Richard Lynch [mailto:[EMAIL PROTECTED] Sent: Friday, March 02, 2007 12:29 AM To: Peter Lauri Cc: 'PHP Mailing' Subject: Re: [PHP] exec from process (deamon) without shell On Thu, March 1, 2007 10:28 am, Peter Lauri wrote: > I have a very complex script that is being called by a running deamon > that > doesn't have any shell. To simplify it I make this script, that is > also > failing: PHP also uses a mini non-shell thingie to run exec. As such, it has no home directory, no $PATH (or is it $path) and not much of anything. Provide the FULL PATH to 'ls' and your test script should work fine. By extension, you need the FULL PATH to everything file-system-related in your real application. You also need to be aware that YOU run as, well, you, and PHP runs as some user defined in httpd.conf (for Apache module) or IUSER_ for IIS (unless you re-configure) or as, errr, User in httpd.conf for CGI, and then, ummm, for FCGI, you're going to have to research that one. > #/thepathtophp > > > exec("ls -lrt", $stdout, $exitcode); > > > > if($exitcode!=0) { > > echo "Command ls -lrt could not be executed correctly. > Exit > code: $exitcode\n"; You may also want to dump out implode("\n", $stdout) just in case it has useful messages. You could also consider sending stderr (2) into stdout (1) so that any error messages are captured. See thread from last week on this one. > } else { > > echo "Result:\n"; > > foreach($stdout AS $line) { > > echo $line."\n"; If by some chance performance is an issue (probably not) then you may want to benchmark this end-to-end with 'ab' compared to echo implode("\n", $stdout) I'm not making any prediction either way on this, just noting a simple alternative worth exploring if performance IS an issue. > } > > } > > ?> > > > > This code give the exit code -1 that is equal to that "ls -lrt" could > not be > executed. > > > > Running this from shell give correct result. This invariably indicates a path/permissions difference between you logged in as YOU, and PHP running as some other user in a totally different environment/shell. > Executing it from a web page (thru apache) gives correct result. > > > > But the "shell less" deamon is giving the -1 exit code. > > > > Is exec requiring a shell? Any workaround on this? I will play around, > maybe > something like system() or passthru() will do it, but I haven't tested > that > yet, want to get some feedback first. exec doesn't require a shell. It has some kind of minimal shell already, I think. But it's definitely not the same full-blown shell you use when you login as YOU. (well, probably not, unless you're a shell masochist minimalist. :-) -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] exec from process (deamon) without shell
On Thu, March 1, 2007 10:28 am, Peter Lauri wrote: > I have a very complex script that is being called by a running deamon > that > doesn't have any shell. To simplify it I make this script, that is > also > failing: PHP also uses a mini non-shell thingie to run exec. As such, it has no home directory, no $PATH (or is it $path) and not much of anything. Provide the FULL PATH to 'ls' and your test script should work fine. By extension, you need the FULL PATH to everything file-system-related in your real application. You also need to be aware that YOU run as, well, you, and PHP runs as some user defined in httpd.conf (for Apache module) or IUSER_ for IIS (unless you re-configure) or as, errr, User in httpd.conf for CGI, and then, ummm, for FCGI, you're going to have to research that one. > #/thepathtophp > > > exec("ls -lrt", $stdout, $exitcode); > > > > if($exitcode!=0) { > > echo "Command ls -lrt could not be executed correctly. > Exit > code: $exitcode\n"; You may also want to dump out implode("\n", $stdout) just in case it has useful messages. You could also consider sending stderr (2) into stdout (1) so that any error messages are captured. See thread from last week on this one. > } else { > > echo "Result:\n"; > > foreach($stdout AS $line) { > > echo $line."\n"; If by some chance performance is an issue (probably not) then you may want to benchmark this end-to-end with 'ab' compared to echo implode("\n", $stdout) I'm not making any prediction either way on this, just noting a simple alternative worth exploring if performance IS an issue. > } > > } > > ?> > > > > This code give the exit code -1 that is equal to that "ls -lrt" could > not be > executed. > > > > Running this from shell give correct result. This invariably indicates a path/permissions difference between you logged in as YOU, and PHP running as some other user in a totally different environment/shell. > Executing it from a web page (thru apache) gives correct result. > > > > But the "shell less" deamon is giving the -1 exit code. > > > > Is exec requiring a shell? Any workaround on this? I will play around, > maybe > something like system() or passthru() will do it, but I haven't tested > that > yet, want to get some feedback first. exec doesn't require a shell. It has some kind of minimal shell already, I think. But it's definitely not the same full-blown shell you use when you login as YOU. (well, probably not, unless you're a shell masochist minimalist. :-) -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] exec from process (deamon) without shell
Quoting Roman Neuhauser <[EMAIL PROTECTED]>: # [EMAIL PROTECTED] / 2007-03-01 18:28:35 +0200: #/thepathtophp echo "Command ls -lrt could not be executed correctly. Exit code: $exitcode\n"; } else { echo "Result:\n"; foreach($stdout AS $line) { echo $line."\n"; } } ?> This code give the exit code -1 that is equal to that "ls -lrt" could not be executed. Does the daemon run chrooted somewhere? What does $PATH contain? What happens if you use /bin/ls? -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 Becuase this only is having problems when executing from the deamon I added: exec("php -i", $stdout, $exitcode); And then put it into a logfile. Is there anything here that makes $exitcode for all commands executed -1, but they still seams to be functional as $stdout is as expected: 2007-03-02 00:00:54 Environment 2007-03-02 00:00:54 2007-03-02 00:00:54 Variable => Value 2007-03-02 00:00:54 TERM => xterm 2007-03-02 00:00:54 PATH => /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin 2007-03-02 00:00:54 _ => /usr/bin/php 2007-03-02 00:00:54 PWD => /var/www/util 2007-03-02 00:00:54 LANG => C 2007-03-02 00:00:54 SHLVL => 6 2007-03-02 00:00:54 2007-03-02 00:00:54 PHP Variables 2007-03-02 00:00:54 2007-03-02 00:00:54 Variable => Value 2007-03-02 00:00:54 _SERVER["TERM"] => xterm 2007-03-02 00:00:54 _SERVER["PATH"] => /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin 2007-03-02 00:00:54 _SERVER["_"] => /usr/bin/php 2007-03-02 00:00:54 _SERVER["PWD"] => /var/www/util 2007-03-02 00:00:54 _SERVER["LANG"] => C 2007-03-02 00:00:54 _SERVER["SHLVL"] => 6 2007-03-02 00:00:54 _SERVER["PHP_SELF"] => 2007-03-02 00:00:54 _SERVER["SCRIPT_NAME"] => 2007-03-02 00:00:54 _SERVER["SCRIPT_FILENAME"] => 2007-03-02 00:00:54 _SERVER["PATH_TRANSLATED"] => 2007-03-02 00:00:54 _SERVER["DOCUMENT_ROOT"] => 2007-03-02 00:00:54 _SERVER["REQUEST_TIME"] => 1172786454 2007-03-02 00:00:54 _SERVER["argv"] => Array 2007-03-02 00:00:54 ( 2007-03-02 00:00:54 ) 2007-03-02 00:00:54 2007-03-02 00:00:54 _SERVER["argc"] => 0 2007-03-02 00:00:54 _ENV["TERM"] => xterm 2007-03-02 00:00:54 _ENV["PATH"] => /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin 2007-03-02 00:00:54 _ENV["_"] => /usr/bin/php 2007-03-02 00:00:54 _ENV["PWD"] => /var/www/util 2007-03-02 00:00:54 _ENV["LANG"] => C 2007-03-02 00:00:54 _ENV["SHLVL"] => 6 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] exec from process (deamon) without shell
Quoting Roman Neuhauser <[EMAIL PROTECTED]>: # [EMAIL PROTECTED] / 2007-03-01 18:28:35 +0200: #/thepathtophp echo "Command ls -lrt could not be executed correctly. Exit code: $exitcode\n"; } else { echo "Result:\n"; foreach($stdout AS $line) { echo $line."\n"; } } ?> This code give the exit code -1 that is equal to that "ls -lrt" could not be executed. Does the daemon run chrooted somewhere? What does $PATH contain? What happens if you use /bin/ls? I tried with /bin/ls but still same thing. I just found out that the exitcode is -1 but the $stdout actually gives correct answer. But that doesn't make sense. How can I then control what action to take depending on the exitcode? apache 10099 1 0 18:47 ?00:00:00 /tmp/log_monitor -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] exec from process (deamon) without shell
# [EMAIL PROTECTED] / 2007-03-01 18:28:35 +0200: > #/thepathtophp > > exec("ls -lrt", $stdout, $exitcode); > > if($exitcode!=0) { > echo "Command ls -lrt could not be executed correctly. Exit code: > $exitcode\n"; > } else { > echo "Result:\n"; > foreach($stdout AS $line) { > echo $line."\n"; > } > } > > ?> > > > This code give the exit code -1 that is equal to that "ls -lrt" could not be > executed. Does the daemon run chrooted somewhere? What does $PATH contain? What happens if you use /bin/ls? -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php