I am a python newbie, very interested in raspberry pi and while I was crusading the code I had one idea, tried a lot of ways to make it but with no success.

I'm using this python script to get data from a gps receiver and send it to a database:

#! /usr/bin/python 
import os
from gps import *
from time import *
import time
import threading
import MySQLdb
import re

conn = MySQLdb.connect(host= "localhost",

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

time_to_use = strftime("%Y_%m_%d-%H_%M_%S", gmtime())
time_to_use = "track_"+time_to_use

class GpsPoller(threading.Thread):
  def __init__(self):
    global gpsd #bring it in scope
    gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
    self.current_value = None
    self.running = True #setting the thread running to true
    self.time = time.time();

  def run(self):
    global gpsd
    while gpsp.running:

if __name__ == '__main__':
  gpsp = GpsPoller() 

    while True:

       if gpsd.fix.mode>1:

          x = conn.cursor()

          # print gpsd.status
          # print 'mode        ' , gpsd.fix.mode

             x.execute("""INSERT INTO raw_data (`data`, `date`, `track`) VALUES (%s, CURRENT_TIMESTAMP, %s)""",(gpsd, time_to_use))
             print 'error on mysql query'

       time.sleep(5) #set to whatever

  except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    print "\nKilling Thread..."
    gpsp.running = False
    gpsp.join() # wait for the thread to finish what it's doing

  print "Done.\nExiting."

I want to start and stop the script from an php file, already tried the next script, but it wont work, the python won't execute.

shell_exec('sudo python /var/www/gpsc.py > /dev/null &'); 

Can someone help me achieve the expected functionality?

I just want to call start.php and stop.php to start and stop the recording from the gps.

Best regards

closed as off-topic by Morgan Courbet, RPiAwesomeness, Milliways, lenik, Alex L Oct 10 '14 at 2:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Morgan Courbet, RPiAwesomeness, Milliways, lenik, Alex L
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I'd start by looking at the error messages. As a matter of interest if this is a serial device you could log the NMEA sentences with cat </dev/ttyAMA0 >>logfile. – joan Sep 11 '14 at 8:15
  • Hi joan. Thanks for the reply. If I run that script from the console it works, it writes the data to the database can't only run it from the UI I made using html+php – user2048076 Sep 11 '14 at 13:46
  • Perhaps send the shell_exec output to a file rather than /dev/null? shell_exec('sudo python /var/www/gpsc.py >/tmp/myerr 2>&1 &'); It might not be able to find sudo or python. Have you tried giving the full path to those programs? – joan Sep 11 '14 at 14:08

There is a php.ini setting called safe_mode which must be turned off to allow shell execution. you can see that, and the php.ini location, by opening phpinfo.php in a webbrowser which contains

<?php phpinfo(); ?>
(always backup old php.ini. php.ini changes need a restart of your webserver/apache daemon)

usually php + the webserver runs as user www-data make sure your /etc/sudoers contains this user something like


note this can be a security risk!!! finally rather than calling a php script which calls a python script, I'd call the python script directly, or handle a python-script with another python script. There are tutorials howto Configuring Apache2 to run Python Scripts.

Since memory is rare with a raspi, I'd rather use event-driven webservers such as lighttpd (easy to configure) or nginx(not so easy to configure) instead of the heavy old apache2.

  • Thanks Michael D. I will check that info about running the python script directly from apache. Do you know if is it possible, when creating the thread, to give it a name so that I can just call "killall thread"? Thanks. – user2048076 Sep 15 '14 at 18:20
  • on linux (*nix) os each process gets a programm id (pid) there are cmdline tools like "pidof myprog" to get the pid of the process, which you can terminate with "kill -p myprogpid" for more info run man pidof and man kill in your linux shell – Michael D. Sep 15 '14 at 19:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.