0

I have a bash script that starts some python code, running on a RasPi under latest raspbian. It runs fine if I run it manually with sudo, but when it auto runs at boot I get a mysql error. The script is called from a line in /etc/rc.local

The script is

#!/bin/bash 
/home/control/solar/v10_1/control.py  >> '/home/control/solar/logs/control_X.X_start.log' 2>&1 &
echo "control started"  >> '/home/control/solar/logs/control_X.X_start.log'
echo "UID is $UID , EUID is $EUID" >> '/home/control/solar/logs/control_X.X_start.log'

The output to the log after bootup is

UID is 0 , EUID is 0
Traceback (most recent call last):
File "/home/control/solar/v10_1/control.py", line 42, in <module>
import variables    # global variables
File "/home/control/solar/v10_1/variables.py", line 13, in <module>
gv.db = MySQLdb.connect("localhost", "solar", "solar", db='solar') # database for logging
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

I have made all the paths absolute, the UID is 0 in both cases, what can cause the difference in behaviour?

Mick Sulley
  • 99
  • 2
  • 11
  • does the socket exist at the time of your script running? what is you boot order i.e. does you script run before mysql starts ? – MMT May 05 '16 at 16:22
  • Your script is probably running before MySQL has been started. – Barmar May 05 '16 at 16:44
  • Thanks for your help. Yes that seems to be the problem. I just added a 15 sec delay before it starts the python code and it works fine. I guess there must be a more elegant way to ensure that mysql is started before I continue, any suggestions? – Mick Sulley May 05 '16 at 17:28
  • What OS/distro is this? Using the normal startup tools instead of just stuffing the command in `rc.local` is the "more elegant" way to do this sort of thing. – Etan Reisner May 05 '16 at 17:57
  • Raspbian is a Debian derivative and follows the [Unix System V](https://en.wikipedia.org/wiki/UNIX_System_V) conventions about how to start/stop services, relying on the [runlevel](https://en.wikipedia.org/wiki/Runlevel) concept. You can make use of the command [update-rc.d](http://manpages.ubuntu.com/manpages/trusty/man8/update-rc.d.8.html) to configure your service to start automatically at boot time. Have a look [here](http://raspberrywebserver.com/serveradmin/run-a-script-on-start-up.html) for an example. – davidedb May 05 '16 at 18:36
  • Possible duplicate of http://stackoverflow.com/questions/11443504/missing-mysql-sock-yielding-operationalerror-2002-cant-connect-to-local-my – Inian May 05 '16 at 19:05

0 Answers0