Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my rc.local file (Ubuntu)?

I have a python daemon process that gets started via rc.local. This same script, with the same permissions, is installed on a few other Ubuntu boxes I have. It runs without trouble on those installations. That is, after restarting the box, the daemon process is running.

With this particular installation though, the daemon process is not running by the time I log in and check for the existence of the process. The rc.local files between systems are identical (or at least close enough):

localaccount@sosms:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

python /var/www/myDaemon/Main.py > /var/log/somelog.txt

exit 0

The permissions are:

localaccount@sosms:~$ ls -la /etc/rc.local
-rwxr-xr-x 1 localaccount localaccount 370 Jun  3 11:04 rc.local

I tested if the rc.local process is getting executed by using this test rc.local:

localaccount@sosms:/var/log/sosmsd$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

echo "test" > /home/localaccount/test1
/usr/bin/python /var/www/sosms/sosmsd/Main.py > /var/log/sosmsd/log/log
echo "test" > /home/localaccount/test2

exit 0
localaccount@sosms:/var/log/sosmsd$

And only the first test file (test1) gets created after restarting the box. I'm guessing it means that the python line is causing some kind of issue, but I get no output in /var/log/sosmsd/log/log:

localaccount@sosms:~$ ls
test1

Update:

I then followed larsks' advice and determined that I was getting this error from launching the python script:

mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Does this mean that rc.local is being executed before MySQL has had a chance to be initialized? Where do I go from here?

like image 390
kwikness Avatar asked Dec 08 '22 23:12

kwikness


2 Answers

One thing the other people who answered missed is in your rc, init, etc. scripts that are run on startup the PATH variable may or may not be initialized (there's never a guarantee), meaning simplying doing "python somescript.py" instead of /usr/bin/python may not always work. ALWAYS use absolute paths for everything in init scripts and rc scripts.

And yes, it is likely that rc.local is run before mysqld is started. You need to set this up as an init.d script instead and use insserv-style comments in the header to tell it what dependencies it needs. http://manpages.ubuntu.com/manpages/lucid/man8/insserv.8.html

like image 62
hsanders Avatar answered Dec 11 '22 13:12

hsanders


You should create an init script /etc/init.d/mydaemon for your daemon from available skeleton.

Then you will be able to set its startup order so that MySQL is already available.

Here is a good starting point.

like image 24
Yves Martin Avatar answered Dec 11 '22 14:12

Yves Martin