how to run a script of foreign app in linux

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

Post Reply
soonic
Forum Newbie
Posts: 9
Joined: Fri Jan 13, 2012 1:04 pm

how to run a script of foreign app in linux

Post by soonic »

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

Re: how to run a script of foreign app in linux

Post by pickle »

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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
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

Post by Christopher »

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
(#10850)
soonic
Forum Newbie
Posts: 9
Joined: Fri Jan 13, 2012 1:04 pm

Re: how to run a script of foreign app in linux

Post by soonic »

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:

Code: Select all

/usr/local/app_name/
/usr/local/app_name/bin/app_file
My root and script which uses other file:

Code: 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');
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/
?>
with shell_exec() also nothing and doesn't give any string output.
I'm stuck with this ... hope you can still help...
soonic
Forum Newbie
Posts: 9
Joined: Fri Jan 13, 2012 1:04 pm

Re: how to run a script of foreign app in linux

Post by soonic »

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)
Post Reply