4

I have a python script chatserver.py that includes:-

#!/usr/bin/python
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
<SNIP>
reactor.listenTCP(3800, factory)
print "Server Started"
reactor.run()

This opens a socket on port 3800 which works if I start from SSH but I want to do a check for the open port and if it is closed reopen using PHP but I cannot seem to get the python script to execute.

Here is how I am calling it via PHP at the moment

function serverCheck() {
   $host = "MYHOST";
   $port = 3800;
   $connection = @fsockopen($host, $port);

   if ( !is_resource($connection) ) { // port not open
       exec('/usr/bin/python PATH_TO_FILE/chatserver.py 2>&1');

    if($connection)
       fclose($connection);
}

I have tried a lot of things I have found on my searches but I just cannot seem to get the file to execute.

14
  • Comment: Since your .py file has the shebang, you don't need to explicitly tell bash to use /usr/bin/python. Just chmod +x chatserver.py Commented Apr 5, 2013 at 14:15
  • Did you make your python file executable with chmod -x? Commented Apr 5, 2013 at 14:16
  • Your { and } are mismatched!! Commented Apr 5, 2013 at 14:17
  • You're missing a close } on your if. Commented Apr 5, 2013 at 14:18
  • 1
    ls -l chatserver.py shows root:root? And I assume you're running the php script as yourname:yourname? That's your problem right there :) Commented Apr 5, 2013 at 14:29

2 Answers 2

1

Looks like a permission issue. chatserver.py as it stands needs root user to run it.

Don't just chmod 777 chatserver.py. That is dangerous.

The way I like is to create a group for these administrative tasks:

groupadd chatserver

Then add yourself to that group:

usermod -a -G chatserver willroberts

Then change chatserver.py to be owned by that group:

chown root:chatserver chatserver.py

When php is run by your user, it will have the correct group membership to run the py file. This method allows more flexibility for more "chatserver" executables. Here's more reading if you're interested: https://wiki.archlinux.org/index.php/Users_and_Groups and http://www.tldp.org/LDP/intro-linux/html/sect_03_04.html

Sign up to request clarification or add additional context in comments.

29 Comments

ok, so it now says the group is chatserver but still doesnt execute! The owner comes up as ftpn3wlog-n which is correct!
When you run groups willroberts, does it show you as a member of chatserver? When you run ls -l phpscript.php, is it owned by either willroberts or chatserver? And when you run ls -l chatserver.py, is it owned by group chatserver?
Or, is your php script run by a webserver? You can add the webserver user to the group chatserver.
Groups shows me in chatserver yes. The php file is still owned by root. Should I add that to chatserver too? This is the output I get for ls -l chatserver.py : -rwxr-xr-x 1 ftpn3wlog-n chatserver 1326 Apr 5 06:48 chatserver.py
Waiiiiit. How are you running the php script?
|
0

Problem is fixed :)

It was the shebang in the end :| I know.... I feel stupid!

It should have been #!/usr/bin/python but for some reason I had changed it thorugh various testing of things!

Thank you for your help in this. much appreciated!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.