unable to run exec() on apache in a php file

2,516 views
Skip to first unread message

charan puvvala

unread,
Apr 9, 2010, 2:53:56 AM4/9/10
to vglug
Hi all,
I want to run a shell script from a php file so that when the user wants to run the script he just has to visit the page. It worked fine with my previous host, but it aint working now. This is the error when I log the scipt.

<b>Warning</b>:  exec() [<a href='function.exec'>function.exec</a>]: Unable to fork [echo '************************************************* *******' &gt;&gt; loging.txt] in <b>/home2/xyz/public_html/scripts/runshell.php</b> on line <b>7</b><br />

Do I have to do some settings on apache?
Can anyone tell me if safe_mode_exec_dir is somehow related to my problem??

MShehzad Pankhawala

unread,
Apr 9, 2010, 5:18:10 AM4/9/10
to vg...@googlegroups.com
Please check the execute permission of the file being executed (runshell.php), at least execute permission should be assigned to user 'apache'.


--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG-un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG



--
Thanks,
MohammedShehzad Pankhawala.

charan puvvala

unread,
Apr 9, 2010, 5:53:11 AM4/9/10
to vg...@googlegroups.com
I have changed the permissions for execute to all and it seems to be running fine on ssh, but still the same problem with executing in a php file.

Piyush Sharma

unread,
Apr 10, 2010, 2:34:53 AM4/10/10
to vg...@googlegroups.com
Hi,

Check  exec function in php.ini file, have u disable it or not ?.. Most of hosting service provider disabled this function due to security reasons.


vi /usr/local/lib/php.ini

Disable_Function =  exec


And also try through ssh or command line....

[root@~]#  php -q  /home2/xyz/public_html/scripts/runshell.php
Regards,


Piyush M Sharma
www.linuxsurgeon.org
RedHat Certified Engineer  
09413194321
Linux Administrator


charan puvvala

unread,
Apr 10, 2010, 2:45:06 AM4/10/10
to vg...@googlegroups.com
Thanks a lot...will give it a try and follow up

charan puvvala

unread,
Apr 12, 2010, 2:22:58 AM4/12/10
to vg...@googlegroups.com
Hi all,
Followup to Mr.Piyush Sharma's response.
I have checked the php.ini file and disable_functions=   is empty.
Next I tried running the php file in a ssh session and it worked great. So, is the problem that the apache user(user browsing my site) cannot use the exec function??

MShehzad Pankhawala

unread,
Apr 12, 2010, 2:46:10 AM4/12/10
to vg...@googlegroups.com
On Mon, Apr 12, 2010 at 11:52 AM, charan puvvala <charan4...@gmail.com> wrote:
Hi all,

Followup to Mr.Piyush Sharma's response.
I have checked the php.ini file and disable_functions=   is empty.
Next I tried running the php file in a ssh session and it worked great. So, is the problem that the apache user(user browsing my site) cannot use the exec function??

Have you tried shell_exec?

Hope that helps.

--
Thanks,
MohammedShehzad Pankhawala.

Piyush Sharma

unread,
Apr 12, 2010, 4:39:30 AM4/12/10
to vg...@googlegroups.com
Hi,

Paste your following command's informations.

[root@~]# ll public_html/

and

[root@~]# ll runshell.php


and r u using any control panel ?




--
Thanks,
MohammedShehzad Pankhawala.

--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG-un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG

Linuz Ashu

unread,
Apr 10, 2010, 12:55:04 AM4/10/10
to vg...@googlegroups.com
Can i see your PHP code??


On Fri, Apr 9, 2010 at 12:23 PM, charan puvvala <charan4...@gmail.com> wrote:

--
Please read http://www.catb.org/~esr/faqs/smart-questions.html before posting.
You received this message because you are subscribed to the "Vibrant GNU/Linux User Group".
To stop receiving emails from this group, mail to VGLUG-un...@googlegroups.com
To post to this group, send email to VG...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/VGLUG



--
Regards,
Ashu

charan puvvala

unread,
Apr 12, 2010, 4:55:34 AM4/12/10
to vg...@googlegroups.com
Yes I have tried shell_exec and system() as well.
I have been provided with cpanel.

Here is the output for the commands:
|| public_html/
-rw-r--r--  1 arasanch arasanch      130 Mar  9  2006 400.shtml
-rw-r--r--  1 arasanch arasanch      162 Mar  9  2006 401.shtml
-rw-r--r--  1 arasanch arasanch      201 Mar  9  2006 403.shtml
-rw-r--r--  1 arasanch arasanch      175 Mar  9  2006 404.shtml
-rw-r--r--  1 arasanch arasanch      363 Mar  9  2006 500.php
-rw-r--r--  1 arasanch arasanch       71 Mar  9  2006 500.shtml
-rw-r--r--  1 arasanch arasanch  2397815 Apr  4 02:53 Arasan-2010-03-19T09-59-03.mysql
-rw-r--r--  1 arasanch arasanch   477890 Apr  4 02:53 arasan_home.swf
drwxr-xr-x  2 arasanch arasanch     4096 Apr  4 02:53 beta/
-rw-r--r--  1 arasanch arasanch  3258932 Apr  4 02:53 beta1.sql
drwxr-xr-x  4 arasanch arasanch     4096 Apr  4 02:52 cache/
drwxr-xr-x  2 arasanch arasanch     4096 Mar 30 15:41 cgi-bin/
-rw-r--r--  1 arasanch arasanch      320 Apr  4 02:53 cookies.txt
-rw-r--r--  1 arasanch arasanch        0 Feb 10  2009 default.html
-rw-r--r--  1 arasanch arasanch   484482 Apr 12 02:52 error_log
-rw-r--r--  1 arasanch arasanch      539 Feb 10 12:43 favicon.ico
-rw-r--r--  1 arasanch arasanch    44034 Apr  4 02:53 final_intro.swf
-rw-r--r--  1 arasanch arasanch   210190 Apr  4 02:53 final_intro_arasan.swf
-rw-r--r--  1 arasanch arasanch   236784 Apr  4 02:53 final_intro_arasan_audio.swf
-rw-r--r--  1 arasanch arasanch    49341 Apr  4 02:53 final_intro_v1.swf
-rw-r--r--  1 arasanch arasanch    72526 Apr  4 02:53 final_intro_v3.swf
drwxr-xr-x  2 arasanch arasanch     4096 Apr  4 02:52 includes/
-rw-r--r--  1 arasanch arasanch     1021 Apr  4 02:53 index.html
-rw-r--r--  1 arasanch arasanch     1034 Apr  4 02:53 index.html~
-rw-r--r--  1 arasanch arasanch      980 Apr  4 02:53 index.php
drwxr-xr-x  3 arasanch arasanch     4096 Apr  4 02:52 misc/
-rw-r--r--  1 arasanch arasanch    39016 Apr  4 01:53 php.ini
drwxr-xr-x 14 arasanch arasanch     4096 Apr 11 23:56 products/
drwxr-xr-x  3 arasanch arasanch     4096 Apr  4 02:52 profiles/
-rw-r--r--  1 arasanch arasanch     1590 Apr  4 02:53 robots.txt
drwxr-xr-x  2 arasanch arasanch     4096 Apr 12 02:52 scripts/
drwxr-xr-x  7 arasanch arasanch     4096 Apr  4 02:53 themes/
drwxr-xr-x  3 arasanch arasanch     4096 Apr  4 02:53 tmp/
-rw-r--r--  1 arasanch arasanch       44 Apr  4 03:02 url.txt
-rw-r--r--  1 arasanch arasanch      352 Apr  4 02:53 xmlrpc.php
aras...@arasanchipsystems.com [~]#


|| scripts/runcron.php
-rwxr-xr-x 1 arasanch arasanch 484 Apr  9 01:03 scripts/runcron.php*

charan puvvala

unread,
Apr 12, 2010, 4:59:34 AM4/12/10
to vg...@googlegroups.com
Here is the php code
<html>
<head>
<body>
<img src="/sites/all/themes/droid/images/arasan_header.jpg" >
<center><b>Script for CRON Job manually</b></center>
<?php
shell_exec("echo '************************************************* *******' >> loging.txt");
exec("sh mysh.sh");
system("sh mysh.sh >> myshlog.txt");
shell_exec("sh mysh.sh >> myshlogfinal.txt");
?>
<br/>
<br/>
<center>The system has prepared the latest Leads report and has been mailed.</center>

Piyush Sharma

unread,
Apr 12, 2010, 5:49:40 AM4/12/10
to vg...@googlegroups.com
Hi,

If you have WHM access then check once php.ini from whm.

WHM  >>  Main >> Service Configuration >> PHP Configuration Editor  >> Disable_function

And it seems like your permissions are correct..

run ur url from browser and check in /usr/local/apache/logs/error_logs  &  /usr/local/apache/logs/suexec_log

Linuz Ashu

unread,
Apr 12, 2010, 11:58:54 PM4/12/10
to vg...@googlegroups.com
Hi,


On Mon, Apr 12, 2010 at 2:29 PM, charan puvvala <charan4...@gmail.com> wrote:
Here is the php code

My suggestions :-
i) Use absolute path for all.
ii) You have using "sh" command for executing shell code. Do not us "sh" just make you script executable "chmox +x scriptname".
iii) Use absolute path of mysh.sh. Let say, you have put mysh.sh into /tmp then your absolute path wud be "/tmp/mysh.sh"
iv) And, also use absolute path of log file.
v) May be SELinux occurring problem. Also check this.
 
 
<html>
<head>
<body>
<img src="/sites/all/themes/droid/images/arasan_header.jpg" >
<center><b>Script for CRON Job manually</b></center>
<?php
shell_exec("echo '************************************************* *******' >> loging.txt");
exec("sh mysh.sh");
system("sh mysh.sh >> myshlog.txt");
shell_exec("sh mysh.sh >> myshlogfinal.txt");
?>
<br/>
<br/>
<center>The system has prepared the latest Leads report and has been mailed.</center>




Changed code by {A} ->

<html>
<head>
<body>
<img src="/sites/all/themes/droid/images/arasan_header.jpg" >
<center><b>Script for CRON Job manually</b></center>
 <?php
shell_exec("/bin/echo '************************************************* *******' >> /tmp/loging.txt");
system("/bin/bash /tmp/mysh.sh >> /tmp/myshlog.txt");
system("/bin/bash /tmp/mysh.sh >> /tmp/myshlogfinal.txt");

?>
<br/>
<br/>
<center>The system has prepared the latest Leads report and has been mailed.</center>


Above code should be work. Please let me know the results.

 



--
Regards,
Ashu

Jigar Raval

unread,
Apr 14, 2010, 11:18:26 PM4/14/10
to vg...@googlegroups.com
Hello,

check php.ini may be exec is blocked by php.ini. Normally, it is not advisable to allow this. If exec mentioned in php.ini, remove it and try again.

Jigar

--- On Tue, 4/13/10, Linuz Ashu <ashufo...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages