Solved

execute bash script from php

Posted on 2008-10-21
10
2,345 Views
Last Modified: 2013-12-26
Hi Im trying to execute a simple bash script from php that does an svn update, the bash script works from the command line and I have chmod'd the file to 777  i need help getting it to execute from php.  I have adapted this script from the php docs but i cant get it to work,

Any help would be greatly appreciated.

Thanks
<?php
 
echo '<pre>';
 
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('bash /home/xxxxxxxxxxxxx/svn-up', $retval);
 
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
 
?>

Open in new window

0
Comment
Question by:gxp071
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:wildzero
ID: 22765932
Hey there

I think you just need to change this
$last_line = system('bash /home/xxxxxxxxxxxxx/svn-up', $retval);

to
$last_line = system('/home/xxxxxxxxxxxxx/svn-up', $retval);

then as long as you have
!#/bin/bash
or line the same (path to bash) at the top of your bash script you should be sweet.

0
 
LVL 10

Expert Comment

by:wildzero
ID: 22765951
This is the php command i use to export a copy of our reposatory.

<?php
... some stuff..

$result = shell_exec('svn export svn://192.168.1.221/proj1/trunk website/ --force');
$result = trim($result);
echo $result;
?>

0
 
LVL 10

Expert Comment

by:wildzero
ID: 22765962
Also, there is svns's hooks that you can use
Example is the post-comit hook which runs each time you comit a file to the reposatory... it might be worth looking at those depending on what your doing, just go a search in google for svn hooks

:-)
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 9

Expert Comment

by:absx
ID: 22765975
Hi,

Your script would seem correct. Things you can try are defining the entire path to the bash executable, just in case PHP's using a strange path, adding "2>&1" to your command (as in system('bash /home/xxxxxxxxxxxxx/svn-up 2>&1', $retval);) in order to get the potential messages otherwise going to stderr and trying a different approach in PHP entirely. Things to try would be the exec() command and the `command` approach (note the correct character, ` is not '), i.e.

echo "This system is " . `uname -a`;
0
 
LVL 6

Author Comment

by:gxp071
ID: 22765979
Hi,  first of all thanks for helping.

I should of mentioned I had already tried the command without the 'bash ' part, to no avail,  I have attached the bash script as you can see dead simple I have blanked out the paths.  


#!/bin/bash
 
cd /home/xxxxxx/public_html/..../..../....../
svn up
cd /home/xxxxx/public_html/..../....../.../...../....../.../
svn up

Open in new window

0
 
LVL 9

Expert Comment

by:absx
ID: 22766047
It could still be a path issue, in the bash script the command "svn" isn't referenced by full path. Why don't you exec the commands now in the bash script directly from PHP? This way you coud get individual error handling.
0
 
LVL 6

Author Comment

by:gxp071
ID: 22766221
i added 2<&1 showed the error message,

svn: Can't open file '.svn/lock': Permission denied

I'm guessing that my apache user does not have permission to use, modify the svn control files.

i Have also just tired

$result = shell_exec('svn update /home/xxxxxx/public_html/......./....../....../.../..../');
$result = trim($result);
echo $result;

and I get: skipped '.'

the same command from shell works correctly.

background on what i'm trying to do its provide a method for designers to push there changes to a dev server without having to ssh in and svn update every time they commit there working copy to the repository.
0
 
LVL 9

Accepted Solution

by:
absx earned 250 total points
ID: 22766521
Hi,

Unfortunately I've currently no LAMP setting to test on, but the file ".svn/lock" sounds like it's under the user's home (~) directory. If you can't find where exactly this refers to and change the permissions accordingly (echo `whoami`; for user if unknown), I suppose you could use sudo to execute it as some user who has the permission?

0
 
LVL 10

Assisted Solution

by:wildzero
wildzero earned 250 total points
ID: 22769829
Ah yes, that will be your issue.
Permissions :-)

When you are trying it from the shell, switch to root then try
su apache
to switch to apache user and then run the command.

It works for me but that is because the location I tell the php/shell command to use svn has chmod 777 on all the files (on our development copy).
0
 
LVL 6

Author Closing Comment

by:gxp071
ID: 31508208
Hay guys thanks, for all your help, ive split the points 50/50 as you both been great help.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question