Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > PHP webpage HANGS: executing shellscript.sh via system()

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 05-06-2005   #1 (permalink)
Just Joined!
 
Join Date: May 2005
Location: Canada
Posts: 3
PHP webpage HANGS: executing shellscript.sh via system()

Hello,

I've been at this for awhile now. I wrote a shellscript, that when executed from the commandline works fine and exits fine (giving me commandline back) when its done (takes 2 seconds)

I have now written a PHP page, that when visited by the user will execute the shellscript in the user's personal files. (they dont have ssh access, hence the use of php).

This PHP page in question uses the system() command to execute the shellscript (and the shellscript actually does get executed). The problem is, even though running the shellscript was a success, PHP will hang and not load the page.

I am assuming it is because PHP wont let go of the file..I have tried many variations..here are some examples:

$last_line = system("nohup ".$target."/start.sh > /dev/null 2>&1 &");

$last_line = system($target."/start.sh &");


I have even tried substituting system() with exec(). The shell script does echo about 4 lines of output. I would very much so like to return this output to the PHP page as well, please

What am I doing wrong or what should I be doing instead? Also after php hangs, I get this in ps aux:

testuser 24229 0.0 0.0 0 0 ? Z 02:27 0:00 [sh <defunct>]


the 'sh' would be due to the shellscript. I am 100% sure the shellscript is okay. If I try to run an executable directly (instead of start.sh) using system(), then the executable filename will be the defunct instead of sh.

I would be very grateful for your help on this. I have read php.net manuals and tried many things.. I have exhausted my resources :\

it might be interesting to note that this one doesnt hang:

$last_line = exec($target."/mainfile stop &"); // stops the program


./mainfile stop would close the program in question. I guess it works because it closes so PHP lets go of it. If you think it would help to view the start.sh file let me know and I will post it. I am using these:

#!/usr/local/bin/php (correct path)
#!/bin/sh (correct path)

Thank you kindly, means alot..

-Ji
jiketsu is offline  


Reply With Quote
Old 05-06-2005   #2 (permalink)
Linux Engineer
 
Giro's Avatar
 
Join Date: Jul 2003
Location: England
Posts: 1,219
This is a zombie proccess,

Code:
testuser 24229 0.0 0.0 0 0 ? Z 02:27 0:00 [sh <defunct>]
Im not sure whats causeing the problem but have you tried moving the commands inside the shell script directly into the system() call, also have you put a debug message before the call and after?
Giro is offline   Reply With Quote
Old 05-06-2005   #3 (permalink)
Linux Guru
 
kkubasik's Avatar
 
Join Date: Mar 2004
Location: Lat: 39:03:51N Lon: 77:14:37W
Posts: 2,397
Send a message via AIM to kkubasik
other possible might be that the shell script doesn't return what the php system() call expects to indicate that the call has been finished. You'll have to check the docs, but its possible that a TERMINATE_SUCESS of somesort is required before php will finish parsing.

Also try making a file

#!/usr/bin/php

<php?
system(shellscript)
php?>


and running it, its possible that its just your mod_php isnt working, or that apache doesnt have permisson to run the shell script correctly (and if the shell script doesnt have a terminationg built in, it might hang on a permissions issues that it doesnt have when you test it under your normal account)
__________________
Avoid the Gates of Hell. Use Linux
A Penny for your Thoughts

Formerly Known as qub333
kkubasik is offline   Reply With Quote
Old 05-06-2005   #4 (permalink)
Just Joined!
 
Join Date: May 2005
Location: Canada
Posts: 3
Thank you for your responses,

I have not yet attempted to have PHP perform all the functions of the shellscript...however one of the lines inside the Shellscript is an executution of the main 'soldatserver' anyways, and produces the same result (php hanging) as it does trying to run the shellscript would.

(I have tried having system(ls -l); and it outputted the ls command just fine and did not hang)

When I do debugging, I use:

error_reporting(E_ALL); ... But in this case the php webpage does not even load at all so I can't see any of the debugging output.

When the php webpage is told to start the shellscript, it does actually run the shellscript fine. Because the 'soldatguardian' the shellscript runs, does start correctly.

I am kind of thinking it is because PHP keeps a handle on the program it starts and won't let go or something..

Here is the shellscript just in case:
Code:
#!/bin/sh

curdir="/home/s4r/servers/s4r2"

echo "Checking Server Environment..."
process=`ps uxw | grep $curdir/soldatserver | grep -v grep | awk '{print $11}'`
if [ -z "$process" ]; then
echo "Server is not active."
echo "Initializing Server Process..."
$curdir/soldatguardian
sleep 1
echo "Server Started Successfully"
else
echo "ERROR: Server Already Active"
fi

exit 1
It simply checks to see if the soldatserver is running, and if it isnt then it spawns it by running soldatguardian (which is a crash-prevention addon that handles launching the soldatserver instead). All of these run fine from commandline.

start.sh - runs fine from commandline, finishes in 2 seconds gives commandline back
soldatserver - runs fine from commandline, but uses commandline for all its server output
soldatguardian - runs fine from commandline, spawns soldatserver but moves it's output to a log file. gives commandline back

This code works fine to have PHP stop the server and load the page:

$last_line = exec($target."/soldatguardian stop &");

I am stumped..It is strange that stopping it doesnt cause hang but starting it does :\

Thank you for your help I am grateful

-Ji
jiketsu is offline   Reply With Quote
Old 05-07-2005   #5 (permalink)
Just Joined!
 
Join Date: May 2005
Location: Canada
Posts: 3
Quote:
Originally Posted by qub333
Also try making a file

#!/usr/bin/php

<php?
system(shellscript)
php?>

and running it, its possible that its just your mod_php isnt working
Is it just plain old system(shellscript) or system(/path/to/a/shellscript.sh) ?
I guess I should try both. What should this do?

And what may I ask is mod_php ? The system($target."/server stop &"); command worked...would that be an indication that I can do what I need? Except that command is to stop the server.. I need it to work when I am trying to start it

When I do try to start, I know the shellscript runs and executes fine. But php hangs. I should probably try to do that, and while php is hanging, stop the server via commandline, and then see if php will suddenly load :P

-Ji
jiketsu is offline   Reply With Quote
Old 06-02-2006   #6 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 1
I had the same problem as you trying to get my mp3_randomzier to execute a the command to play a new song after n seconds. This code seems to work .

Code:
        system("nohup thecommand > /dev/null & echo $!;");
- dark2k1
dark2k1 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:33 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2