4

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?

kwikness
  • 1,425
  • 4
  • 21
  • 37

5 Answers5

7

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

hsanders
  • 1,913
  • 12
  • 22
  • Yes, rc.local does not get run last as one would hope. On my CentOS 7.2 system I had a MariaDB database operation to run on boot, and had a similar problem as above. I fixed it by adding 'systemctl start mariadb.service' to the rc.local prior to the database operation. Though the accepted answer is 'technically' correct, it doesn't address the problem with rc.local and/or how to make rc.local work as desired. – Andrew Oct 03 '16 at 00:39
3

Python exceptions -- and most other error messages -- go to stderr, but you're only redirecting stdout. You should be running your service like this:

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

The 2>&1 tells the shell to send stderr output to the same place as stdout. Do this and post any error messages you see if the problem doesn't end up being obvious.

larsks
  • 277,717
  • 41
  • 399
  • 399
3

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.

Yves Martin
  • 10,217
  • 2
  • 38
  • 77
1

my friends. I spend several days for this error. Fortunately I got a solution.

sudo -u www -i /the/path/of/your/script

Please prefer the sudo manual~ -i [command] The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a loginshell...

U can prefer what I post here: Run script with rc.local: script works, but not at boot

Good luck!

Community
  • 1
  • 1
fantaxy025025
  • 809
  • 8
  • 7
0

From the mysql docs: C.5.2.2. Can't connect to [local] MySQL server

The error (2002) Can't connect to ... normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Does the "/var/run/mysqld/mysqld.sock" file exist?

Is the mysql server running on the same server as the python daemon process?

Have you looked at the mysql log?

It should be located in /var/log/

Named something like:
mysql.log
mysql.err
mysqld.log
mysqld.err

$ sudo ls -l /var/log/
// when you have the name try
$ sudo tail -n 50 /var/log/LogName

One possibility is that you're out of disk space, try:

$ sudo df -h

Look at the 4th column "Avail"

Another possibility is the folder permissions, try:

$ sudo ls -l /var/lib/

You should see something like this:

drwxr-xr-x 33 mysql     mysql   4096 May 22 10:02 mysql

You most likely want the owner and group to be "mysql", if they aren't, you could try:

$ sudo chown -R mysql:mysql /var/lib/mysql

Make a note of the original settings first so you can change it back if it doesn't help. The "-R" means change ownership recursively for the sub folders and files.

The permissions should be rwxr-xr-x (755), if not you can try:

$sudo chmod 755 /var/lib/mysql

I wouldn't do that recursively, most of the files and folders should only be accessible to the mysql user, possibly the mysql group and only read/write not executable.

codewaggle
  • 4,893
  • 2
  • 32
  • 48
  • While this technically is something that can fix a MySQL server, in the context of the question, rc.local executes before init.d. The issue here is that MySQL isn't start at that point in the boot process. – hsanders Nov 09 '12 at 20:00
  • You're right, that's why I up voted your answer when you posted it. But I've left this answer as the information may be useful to someone in the future. – codewaggle Nov 12 '12 at 21:16