LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2005, 02:14 AM   #1
chibi
Member
 
Registered: Aug 2004
Location: Canada
Distribution: Archlabs
Posts: 65

Rep: Reputation: 15
php system() and #!/bin/sh .. first timer..lil help please? :)


Hello!

I have coded a php page that uses the system() command to execute a shellscript file (.sh) in the user's personal directory. All the permissions and everything are setup correctly as far as I know, and I have had success linking to the server executable directly rather than to a script.. but then php hangs.. so I want the script to do it.

The following is the start.sh file, which is checks to see if the server is already running, and if not spawns a copy:

#!/bin/sh

curdir="/home/s4r/domains/s4r.domain.com/servers/server1/"

#echo $curdir\<br\><br\>

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

exit


I am not entirely sure if this script is correct. I created it by looking at examples..it feels kinda weird not putting quoation marks and things around it.. I might need some guidance on this.

When I execute it from commandline it says:

: bad interpreter: No such file or directory

the path for the $curdir variable is correct.


I originally had a simpler script, but it gave me a different error:

$ date: invalid date `161 May 4 0'
sh: line 1: 1115183676 - : syntax error: operand expected (error token is " ")


And the script was:

#!/bin/sh

/home/s4r/domains/s4r.domain.com/servers/server1/runme_tostart

exit $?



..What I would really like to accomplish is using the topmost script in this post (but have it so that it works ) and have all the echo's get sent back to the php page in the browser.

I havent really written anything for bash before..I would really appreciate your help on this.

Also..I cant remember what I had, but I did get it to successfuly spawn the server (without error checking).. but thephp would hang and wouldnt refresh..so I need to tell the script to close after it has spawned the sever..im not sure if exit is good enough or wha..

I am grateful for any assistance

Thanks.

Last edited by chibi; 05-04-2005 at 02:33 AM.
 
Old 05-04-2005, 03:02 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
to start a program in the background and return immediately (ie without waiting for it to finish) use shell syntax:

nohup /path/prog >/somedir/some.log 2>&1 &

the ">/somedir/some.log" is optional if prog already opens its own log/err file

Your system() call is not sending tgt to background, so it's waiting for it to finish...
You should use another call to dtermine if it's running eg chk for (new) lock or log file etc.
 
Old 05-04-2005, 03:09 AM   #3
chibi
Member
 
Registered: Aug 2004
Location: Canada
Distribution: Archlabs
Posts: 65

Original Poster
Rep: Reputation: 15
ADDED: PLEASE IGNORE THIS POST...IT WILL JUST CONFUSE YOU BECAUSE I READ A RESPONSE WRONG AND THIS REPLY IS AMBIGUOUS..READ MY POSTS BELOW THIS FOR MY REAL PROBLEM. SORRY!

Thanks for the reply. The prog itself runs in the background and everything.. but I want to perform the script so that it does the checking to see if the process already runs..instead of running the file directly.

It was the shellscript.sh that didnt seem to wanna exit correctly. Is there a reason why the scripts dont run?

Thanks

comment:
What I meant by "The prog itself runs in the background and everything" was that when I run the binary from the commandline it runs in the background, but not when using the php to start it. sorry for the confusion.

Last edited by chibi; 05-05-2005 at 12:23 PM.
 
Old 05-04-2005, 12:39 PM   #4
chibi
Member
 
Registered: Aug 2004
Location: Canada
Distribution: Archlabs
Posts: 65

Original Poster
Rep: Reputation: 15
Sorry! I actually missinterpreted what you said! I am an idiot I must say :P I thought you were talking about running the file directly instead of using a shellscript. Sorry. Running the file directly produces the same result too.

I tried:

passthru("nohup ".$target."/start.sh >&1 &");
system("nohup ".$target."/start.sh >&1 &");
exec("nohup ".$target."/start.sh >&1 &");
shell_exec("nohup ".$target."/start.sh >&1 &");

the script executes fine, but the php hangs. I figured out my script now so that part is okay. It is just the php hanging part that matters.

for the scripts I needed to do a dos2unix command.

Last edited by chibi; 05-05-2005 at 12:19 PM.
 
Old 05-05-2005, 06:04 AM   #5
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
are you really sure that /bin/sh exists and is executable by the user that executes the script.
This is the normal reason for "bad interpreter:" messages.

Moreover, you forgot to close your if statement by 'fi'
 
Old 05-05-2005, 07:03 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
for the scripts I needed to do a dos2unix command.
Maybe there is a problem with this, try delete the first line
and type by hand #!/bin/sh to make sure
 
Old 05-05-2005, 12:09 PM   #7
chibi
Member
 
Registered: Aug 2004
Location: Canada
Distribution: Archlabs
Posts: 65

Original Poster
Rep: Reputation: 15
Ahh no the scripts are fine now I fixed them (i also added that fi, thanks)

I used the dos2unix command and the scripts converted the endlines and things so that linux would read them correctly. The user can execute the scripts just fine when I run them from the commandline. (and when the script finishes (takes 2 seconds), it gives me my commandline back)

But the problem is when I have php issue the system() or exec() command to run the shellscript remotely, the php will hang because I guess it doesnt want to let go of the file.

The script isnt my problem :P This is my problem in PHP code:

#!/usr/local/bin/php
$last_line = system("nohup ".$target."/start.sh > /dev/null 2>&1 &");

I put this in the PHP code to run the start.sh shellscript. The start.sh file WILL run, but PHP will hang and not load its page. (the user is visiting via website they logged in from, hosted on the same machine)

I have tried many different variations of it. As a test, If I link to any file I want to execute rather than the shell.sh, it will hang as well. So its the system() code not the shellscript im 98% sure.

Thanks for your help.. I know sometimes people dont want to answer because theres a lot to read. I am very grateful. I have tried using the exec() command too and other variations of the above code but nothing works. Please help me

Last edited by chibi; 05-05-2005 at 02:02 PM.
 
Old 05-05-2005, 04:52 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
And what does $last_line contain with your previous code ?

Maybe you could not start nohup from php, man nohup says :
Quote:
If standard output is a terminal, it is redirected so that it is
appended to the file `nohup.out'; if that cannot be written to, it is
appended to the file `$HOME/nohup.out'. If that cannot be written to,
the command is not run. If standard output is not a terminal, then the
standard output of COMMAND will be the same as that of `nohup'.
 
Old 05-05-2005, 10:28 PM   #9
chibi
Member
 
Registered: Aug 2004
Location: Canada
Distribution: Archlabs
Posts: 65

Original Poster
Rep: Reputation: 15
Alright. So I remove nohup. I still get the same thing happening.

nohup was there because I've been trying anything and everything..

So maybe what I should be asking is, instead of telling me whats wrong with what I've got, tell me what I should be doing?

system($target/start.sh); There just like that. Now how do I get that to run in the background so php doesnt hang?

(As a reminder..this code resides inside a .php file that is being accessed with using a browser by a user with an account on the machine. The path leads to the right spot where start.sh is)

Quote:
Originally posted by keefaz
And what does $last_line contain with your previous code ?
nothing from previous code. $last_line is supposed to be assigned any output as far as I know.

Thank you

Last edited by chibi; 05-05-2005 at 10:36 PM.
 
  


Reply



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
sudo /usr/bin/chroot /home/chroot /bin/su - xxx| /bin/su: user xxx does not exist saavik Linux - General 3 07-04-2007 10:30 AM
/usr/bin/php: bad interpreter :::what install to avoid this message. Thank you sarajevo Linux - Software 1 09-23-2005 09:38 AM
Linux install on XP system(2nd drive) Does the Recycle Bin get blown away? traindo1 Linux - Newbie 4 05-04-2005 10:38 AM
how can I get this *.bin file in the FreeBSD system?-- dual installation czy11421 *BSD 2 05-30-2004 01:45 PM
how to stop users on the system from access /etc /var /bin /boot ... etc... artistik Linux - Security 11 12-01-2003 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:18 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration