Solved

execute bash script from php

Posted on 2008-10-21
10
2,324 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now