rc.local won't execute the script

20,263

In order to verify your /etc/rc.local script you should use this command:

  sudo service rc.local start

When system init starts rc.local scripts it runs as root, but if you want to check your script, you need to become root via sudo command.

That is why you got errors like Can't CD to /root/server/srv/.

If your script doesn't work during startup process, consider that the environment is restricted, so you should define missing variables as your script needs.

Here is an example.

The error: /bin/sh: 0: Illegal option - is due to DOS file format.

In order to fix it you should install dos2unix utility and convert rc.local file:

  sudo dos2unix /etc/rc.local
Share:
20,263
user1880779
Author by

user1880779

Updated on September 18, 2022

Comments

  • user1880779
    user1880779 over 1 year

    This is my 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.
    
    cd /root/server/srv/
    ./start_srv &
    
    exit 0
    

    When I execute

        cd /root/server/srv/
        ./start_srv &
    

    via terminal myself, it starts normally and everything works but when I try and do it through rc.local it wont start.

    I know rc.local doesn't need sudo etc. I tried executing rc.local myself through terminal and I get the error: "Can't CD to..."

    What am I doing wrong?

    ls -ld /root: drwx------ 4 root root 4096,

    ls -ld /root/server: drwxr-xr-x 3 root root 4096,

    ls -ld /root/server/srv/start_srv: -rwxr-x--x 1 500 500 468420

    ls -ld /root/server/srv: drwxrwxrwx 2 500 500 4096

    • Rmano
      Rmano almost 10 years
      Please add the output of ls -ld /root, ls -ld /root/server/, ls -ld /root/server/srv and ls -ld /root/server/srv/start_srv. Also the complete error when running /etc/rc.local by hand could be useful.
    • user1880779
      user1880779 almost 10 years
      ls -ld /root: drwx------ 4 root root 4096, ls -ld /root/server: drwxr-xr-x 3 root root 4096, ls -ld /root/server/srv/start_srv: -rwxr-x--x 1 500 500 468420
    • Rmano
      Rmano almost 10 years
      You forgot ls -ld /root/server/srv...
    • Rmano
      Rmano almost 10 years
      Strange permissions, but should be ok. What is the exact error when you try to execute /etc/rc.local/ by hand? With which user you do that? Which user is the one with UID=500 which seems not to be recognized by the system? ---- and please, add the info on the question, it is almost unreadable in the comments.
    • Andrea Lazzarotto
      Andrea Lazzarotto almost 10 years
      cd is not an executable, but a command for the shell. I am not sure, but maybe you can't run it via rc.local. Why don't you just call /root/server/srv/start_srv & instead?
    • Rmano
      Rmano almost 10 years
      @AndreaLazzarotto, cd works perfectly in scripts. And maybe the script need to be started with the current directory set. I would really do a (cd ... && ./start_script ) & to use a subshell and not change the CWD globally, but this is nitpicking...
    • user1880779
      user1880779 almost 10 years
      It says: "Can't CD to /root/server/srv/"
    • user1880779
      user1880779 almost 10 years
      any ideas? anyone?
  • user1880779
    user1880779 almost 10 years
    Hello I am getting: '/bin/sh: 0: Illegal option -' when I try to 'sudo /etc/rc.local start'
  • Lety
    Lety almost 10 years
    In order to exec /etc/rc.local you should use 'sudo /etc/init.d/rc.local start' not 'sudo /etc/rc.local start'.
  • user1880779
    user1880779 almost 10 years
    Okay. I tried both but I still got the same message. '/bin/sh: 0: Illegal option -'
  • Lety
    Lety almost 10 years
    Sorry, check first line of rc.local. is it exactly '#!/bin/sh -e'? An other question: is your script 'start_srv' an 'sh' script?
  • user1880779
    user1880779 almost 10 years
    First line is: '#!/bin/sh -e' exactly copied from rc.local. start_srv is not an 'sh' script. Its just a script to run a voip server made for linux.
  • Lety
    Lety almost 10 years
    it's really weird, it seems that sh is not sh :) Could you post 'ls -l /bin/sh' output?
  • user1880779
    user1880779 almost 10 years
    ls -l /bin/sh: 'lrwxrwxrwx 1 root root 4 Jul 30 22:54 /bin/sh -> dash'
  • Lety
    Lety almost 10 years
    It is ok, so it seems that dash doesn't recognize '-e' option. Did you edit rc.local from windows? Could you post your ubuntu release? Could you remove -e options and post 'sudo /etc/init.d/rc.local start' output?
  • user1880779
    user1880779 almost 10 years
    Yes, I did edit the rc.local from Windows. I thought that wouldn't make any problems. I'm new to Ubuntu. Ubuntu 12.04.4 LTS.
  • Lety
    Lety almost 10 years
    I updated my answer, windows use DOS format that doesn't work on ubuntu.
  • user1880779
    user1880779 almost 10 years
    I changed few lines with 'sudo vi /etc/rc.local' editor, saved and exited. I double checked that the new changes have been saved. But I am still getting :'/bin/sh: 0: Illegal option -'.
  • Lety
    Lety almost 10 years
    Sorry I was wrong, I thought that 'gedit' would change file format. I updated my answer.
  • user1880779
    user1880779 almost 10 years
    Its working now, thanks for all the help! I know my problems are a little bit "specific" because of my lack of knowledge :)