Hi
I'd like to run a script using a linux application called from PHP. The output is an image file which I'd like to show in a browser. My problem is that I don't know how to run this script in PHP. I tried to use system() or exec() but nothing happens. I can't even see what is the error. The script is tested in the terminal and works fine.
this how I do it in the terminal:
terminal: app_name arguments script_name
output: image file in the same folder as this script
to make it work I've to be in the same folder as this script because it has references to other files. The script is deeper then the index.php.
How to run correctly a script from PHP?
how to run a script of foreign app in linux
Moderator: General Moderators
Re: how to run a script of foreign app in linux
Make all file references to the script and within the script absolute - then it doesn't matter where the script is run from.
Also note that the shell command when run via exec() is run by a different user than you when you run it yourself - so there might be permission issues.
Finally, try using shell_exec(), as it returns the output from the command.
Also note that the shell command when run via exec() is run by a different user than you when you run it yourself - so there might be permission issues.
Finally, try using shell_exec(), as it returns the output from the command.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: how to run a script of foreign app in linux
Remember that PHP runs as the same user as the web server, so the script needs to be accessible and executable by the web server user. That includes the directory where the images is to be written needs to be writable by the web serve user.
I think shell_exec() can return all the output from the shell. See the docs: http://us2.php.net/manual/en/book.exec.php
I think shell_exec() can return all the output from the shell. See the docs: http://us2.php.net/manual/en/book.exec.php
(#10850)
Re: how to run a script of foreign app in linux
there's one more thing I should mention earlier. This application which runs that script needs to load some configuration file first which I do every time when I open terminal by typing "source app_path/config_file". I've put this line in .bashrc so now when I open my terminal it does automatically. If I don't source then there is info in the terminal that the application is not installed. I think PHP doesn't see that, so I also put the line exec('source app_path/config_file') and then exec('app_name arguments script_name') but this also didn't work.
I've this folder structure:
The path where the app is installed:
My root and script which uses other file:
I made this test:
index.php
test.php
with shell_exec() also nothing and doesn't give any string output.
I'm stuck with this ... hope you can still help...
I've this folder structure:
The path where the app is installed:
Code: Select all
/usr/local/app_name/
/usr/local/app_name/bin/app_fileCode: Select all
ROOT>folder1/folder2/script_file, test.php
>folder3/folder4/other_files_needed_by_script
>index.php
I made this test:
index.php
Code: Select all
include('folder1/folder2/test.php');Code: Select all
<?php
//it gives me ROOT folder
echo getcwd().'<br>';
chdir('folder1/folder2');
//it gives me ROOT/folder1/folder2/
echo getcwd().'<br>';
$script = '/usr/local/app_name/bin/app_name arguments script_name';
//$script = 'app_name arguments script_name';
//exec('source app_path/config_file');
exec($script); //doesn't work
exec("/bin/touch test"); //this works fine it creates the test file in ROOT/folder1/folder2/
?>
I'm stuck with this ... hope you can still help...
Re: how to run a script of foreign app in linux
well, I've written little script.sh
#!/bin/bash
source app_path/config_file'
app_name $* script_name
now it works as wanted to and I can move on with my work, but this is not the way I'd like to do it. Every time when there is a call for foreign app with the script I have to initiate the environment for that application by sourcing. Is there a way to put "source app_path/config_file" somewhere in the system? I tried with .bashrc but php doesn't it (of course it works fine when I open the terminal)
#!/bin/bash
source app_path/config_file'
app_name $* script_name
now it works as wanted to and I can move on with my work, but this is not the way I'd like to do it. Every time when there is a call for foreign app with the script I have to initiate the environment for that application by sourcing. Is there a way to put "source app_path/config_file" somewhere in the system? I tried with .bashrc but php doesn't it (of course it works fine when I open the terminal)