LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-23-2006, 12:16 PM   #1
keyF
Member
 
Registered: Jan 2006
Location: Moscow
Distribution: Debian, RHEL, FreeBSD, CentOS
Posts: 38

Rep: Reputation: 15
Question Cannot create folders with bash script called from php


Hello.

I have bash script with following:

#!/bin/bash
[bla-bla]
echo "`which mkdir` -p /var/www/test1"
`which mkdir` -p /var/www/test1

and I call this script from my php page:

<?php
$output = shell_exec('/usr/bin/my-script.sh');
echo "<pre>$output</pre>";
?>

and i got following output
"/bin/mkdir -p /var/www/test1", i.e. everything should be fine, BUT folder is not created.

when i'm trying to run shell script from shell - all OK, folder created.

apache (which runs my php script) runs unded www-data user who has permission to run my shell script and create folders (i've tested it like this: su www-data && ./my-script.sh).

so, i cannot guess what is wrong

Last edited by keyF; 06-23-2006 at 12:18 PM.
 
Old 06-23-2006, 12:52 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,002
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
"which" relies on your PATH variable. This is set when a user logs in based on things like .bashrc or .profile (and global analogs of same). A script run from command line inherits environmental variablees from your login. A script run by a background process (like cron or init) typically runs as the user but does NOT source in these files (.bashrc/.profile etc...) so would not inherit it. You can solve this by specifically source such files within your script (use the . to source a file e.g. ". /root/.bashrc).

An easier way to solve it is to just hard code the variable or fully qualified path name you need in the script.

export PATH="path_to_mkdir"
mkdir -p ...
-OR JUST-
path_to_mkdir/mkdir -p ...

Since I don't know much about php I can't say whether it may have other issues to address but the above occurs in a lot of different scenarios so may be your solution.
 
Old 06-23-2006, 02:20 PM   #3
reshojaei
LQ Newbie
 
Registered: Oct 2005
Location: toronto
Posts: 8

Rep: Reputation: 0
if we want to express it easier. when you excute a command from command prompt you are user of this computer and you have specific access, but when you excute some thing using webserver you are zero user so you dont have many previleges. You can fix it as jlightner said. but you have to be careful beacause you may make a security hole in your system.
 
Old 06-25-2006, 10:53 AM   #4
keyF
Member
 
Registered: Jan 2006
Location: Moscow
Distribution: Debian, RHEL, FreeBSD, CentOS
Posts: 38

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlightner
"which" relies on your PATH variable. ...
when i'm executing my bash script from php, echo command (echo "`which mkdir` -p /var/www/test1") gives me right output "/bin/mkdir -p /var/www/test1". that means that "which" was able to locate mkdir executable..
I think that PATH variable is not the root for this problem.
But anyway, i'll try to hard-code executable location, who knows, may be it will help..
 
Old 06-25-2006, 10:58 AM   #5
keyF
Member
 
Registered: Jan 2006
Location: Moscow
Distribution: Debian, RHEL, FreeBSD, CentOS
Posts: 38

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by reshojaei
...when you excute a command from command prompt you have specific access, but when you excute some thing using webserver you are zero user so you dont have many previleges. ..
My webserver (apache) runs under www-data user. I've tried to logon with this user, and manually create this folder (which my script attempts to create) and i was able to do it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 12:50 PM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 05:53 AM
How do I create application launcher using bash script msgclb Programming 2 01-30-2005 06:28 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
How come folders/viles created via PHP script act different from those dragged in? rvn Linux - Newbie 0 12-09-2003 05:24 PM


All times are GMT -5. The time now is 04:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration