Shell Script Segmentation Fault - php

Hi I have a shell script which should run based on the return of php code:
x=1
while [[ "$x" != 5 ]]
do
echo "Welcome $x"
php test.php
x=$?
done
And the php code
echo "Testdfdf test".PHP_EOL;
exit(4);
So I want whenever I get 5 from php to quit the loop.
But I get sometimes:
./myshell: line 7: 20529 Segmentation fault php test.php
Should it loop without problem?

Probably because of this error which affects both Ubuntu and Debian... https://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870

It should and it does, but no clue about why php is ending with a segfault.

your shell while loop will loop forever, as your php script returns 4 to shell, and your while loop checks for !=5. which means the condition is not going to be met. what actually is it you are wanting to do? unless necessary, i would advise to do everything with php (or shell) , but try not to intermingle both.

Related

Passing argument from PHP to Python not working

I have a python srcipt, called rainbow.py. I can run it optionally with an argument. From command line
python rainbow.py, python rainbow.py 4 works well. When I call this script from php I am unable to pass the argument.
I tried:
$argument=4;
exec("python rainbow.py 4");
exec("python rainbow.py $argument");
exec("python rainboy.py .$argument");
They make the code run like there's no valid argument. (I use duration=int(sys.argv[1] in my python code, the script needs to stop after a while, and when calling from php, always the default duration is active)
I tried
$argument="4"
too, did not work.
Can you tell me what's wrong?How can I pass this argument through? I am confident with python, but a total php newbie.
part of my php code:
$argument="2";
echo "printing line <br>";
exec("python rainbow.py $argument");//duration option does not work
part of my python code:
duration=10
try:
if sys.argv[1]!=None:
print "arg found!"
duration=int(sys.argv[1])
except: pass
print "duration:",duration
I cant see the duration printing out when calling python from php, but from LEDs I can cleary see the duration is always 10 seconds
I added sudo to the python call:
exec("sudo python rainbow.py $argument")
and it works now properly. Can someone tell me why?
The python script ran without it too, but without considering the argument.

Python to PHP strange error

I hope somebody can help me. :)
I am calling a python script from PHP, with the help from phpseclib/ssh2 i ssh into my server and it works fine.
My problem is that if i use "time.sleep(5)" in the loop of my python script i dont get a result back, but if i remove time.sleep(5) and time.sleep(3) it works.
Anybody have an idea why this happen?
If i try the python script i my console everything is picture perfect.!
items = [
'1',
'2',
'3'
]
itemArray = {}
def checker():
for item in items:
time.sleep(5) # If added not working, if removed working, result gets send back
position = 1 # keeps track of the ranking position
for start in range(int(deep)):
time.sleep(3)
results = 'something'
for div in results:
try:
if div.find('i', href=True)['href'].find(something) != -1:
exit_conditon = True
break
else:
position += 1
except:
print "Unexpected error:", sys.exc_info()[0]
raise
if 'exit_conditon' in locals():
if exit_conditon is True:
exit_conditon = False
itemArray.update({value: 1})
break
sys.exit(itemArray)
checker()
Please help.
Update: if i have 3 rows in the items array i need to remove the second time.sleep(5) to get it working, if i have 2 items in my array i only need to remove the first time.sleep(5).
It depends how long your script take time to execute. If your script (php + python) take longer than 30 second with the default config, them php kill it.
Just add set_time_limit(120) at the beginning of your php code
See http://php.net/manual/en/function.set-time-limit.php
And If you are executing the php and python script on the same server you should use exec or shell_exec. It will be faster this way. See php shell_exec() vs exec()
The script is now working as i should do :) Thanks to "neubert" for the answer.
If i set the $ssh->setTimeout() to unlimited, whould i still get a respons if the script halts at some point?

How to run an external program in php

I have a program that I can run on my command line but I was wondering if I could actually get it to run in php. Basically my program would have a user insert a couple values to search for, then those values would be passed on into the program for it to run. Then I would want the result of the program to be displayed
I found a function called exec() but I didn't understand it at all so I was wondering if anyone else knows a way or can help me out!
exec() runs a command on the command line, just as you desire. You can capture the output of the command in an array named as the second argument.
For example:
exec("whoami", $output);
var_dump($output);
This runs linux's "whoami" command and captures the result in the array $output. The second line displays the contents of the array. Is that similar to what you want to do?

php exec() and shell_exec()

I currently have a php page that my webserver serves. In order to display all the information I need to display on the page I need output from an external python script. So I have been using the exec() command of php to execute the python script and capture the output in an array of strings as follows:
$somequery = $_GET['query'];
$result = exec("python /var/www/html/query/myscript.py ".somequery."");
//some for loop to loop through entries in result and echo them.
However there are never any entries to be printed, yet when I run the command directly on the console of the server it will output correctly. I've tried echoing out the command on the webpage that I am executing and it's the correct command. The only thing I think it can be is that exec() doesn't stop the rest of the php program from executing before it finishes, leading to the loop i have printing out entries finding that $result is empty.
How can I ensure that exec() finishes executing before the rest of my php script? Are there maybe settings in php.ini that I would need to change? I'm not entirely sure.
EDIT: I've tried running and storing the output of shell_exec("echo hello"); and printing that output, it now prints. However, when running my command that takes a few seconds longer, the program never finishes executing it before going to the next line.
EDIT 2: I found my solution in the following post https://stackoverflow.com/a/6769624 My issue was with with the numpy python package I was using and I simply needed to comment out the line in /usr/lib64/python2.7/ctypes/init.py like the poster did and my script output correctly.
The correct way to get your shell output is like this:
exec("python /var/www/html/query/myscript.py ".somequery."", $result);
var_dump($result); //output should be in here
Give it a try.

PHP exec on another PHP file - How to get my script to wait?

I'm running a continuous PHP loop that executes another PHP file by using exec("php ...");. The plan is for the executed script to run, then sleep for 2 seconds, then start again. However, it seems like my loop is starting a new instance every 2 seconds instead. So long question short, how do I get my first php script to wait until the execution of script nr 2 is complete?
All this is run using the command line. I would also like the echo functions in script nr 2 to show up on the command line.
Any thoughts would help.
Thanks
Exec does not maintain any state information between instances. You could:
Loop in your subscript
OR
You could set some sort of environment variables or that are read at the beginning of the subscript and written at the end.
OR
You could have the subscript read/write to a file in a similar fashion
OR
You could pass in parameters to the subscript who's output is captured
For outputting to the screen, you might play around with the other exec/system calls:
exec
shell_exec
passthru
system
I believe passthru() will work. Another possibility if it doesn't is to call exec(), using the output parameters to capture the output strings from the subscript. Then just echoing that output on return of the subscript.
I also believe that using the output parameters (or capturing the result of the function in a variable) will cause the exec to wait until the command is complete before continuing on.
The problem is, once you excute the script, it will run. Another exec will start another instance like you found out.
What you can do is
Put the sleep inside the executed script. Once it starts running, it will do its own sleep. You can look at setting an execution time limit and maybe ignoring user abort.
You can create a function and let your script call that function. It will then sleep after execution and call the function again.
// maybe set time limit here
Function loop ()
{
Sleep(120);
//you can make a check whether to loop or not.
Loop();
}
Loop();

Categories