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

19,820

Solution 1

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.

Solution 2

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

Solution 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.

Solution 4

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!

Share:
19,820
kwikness
Author by

kwikness

Updated on June 26, 2022

Comments

  • kwikness
    kwikness almost 2 years

    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?