4

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

  • 1
    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. – Rmano Jul 31 '14 at 09:57
  • 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 – user1880779 Jul 31 '14 at 10:32
  • 1
    You forgot ls -ld /root/server/srv... – Rmano Jul 31 '14 at 11:00
  • ls -ld /root/server/srv: drwxrwxrwx 2 500 500 4096 – user1880779 Jul 31 '14 at 11:33
  • 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. – Rmano Jul 31 '14 at 11:36
  • 1
    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? – Andrea Lazzarotto Jul 31 '14 at 11:42
  • 1
    @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... – Rmano Jul 31 '14 at 11:48
  • It says: "Can't CD to /root/server/srv/" – user1880779 Jul 31 '14 at 11:50
  • any ideas? anyone? – user1880779 Jul 31 '14 at 13:27

1 Answers1

4

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
Lety
  • 6,039
  • 2
  • 29
  • 37
  • 1
    Hello I am getting: '/bin/sh: 0: Illegal option -' when I try to 'sudo /etc/rc.local start' – user1880779 Aug 11 '14 at 13:17
  • In order to exec /etc/rc.local you should use 'sudo /etc/init.d/rc.local start' not 'sudo /etc/rc.local start'. – Lety Aug 11 '14 at 14:09
  • Okay. I tried both but I still got the same message. '/bin/sh: 0: Illegal option -' – user1880779 Aug 11 '14 at 15:00
  • Sorry, check first line of rc.local. is it exactly '#!/bin/sh -e'? An other question: is your script 'start_srv' an 'sh' script? – Lety Aug 11 '14 at 15:09
  • 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. – user1880779 Aug 11 '14 at 18:14
  • 1
    it's really weird, it seems that sh is not sh :) Could you post 'ls -l /bin/sh' output? – Lety Aug 11 '14 at 18:40
  • ls -l /bin/sh: 'lrwxrwxrwx 1 root root 4 Jul 30 22:54 /bin/sh -> dash' – user1880779 Aug 11 '14 at 18:46
  • 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? – Lety Aug 11 '14 at 20:48
  • 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. – user1880779 Aug 12 '14 at 09:42
  • 1
    I updated my answer, windows use DOS format that doesn't work on ubuntu. – Lety Aug 12 '14 at 10:10
  • 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 -'. – user1880779 Aug 12 '14 at 11:27
  • Sorry I was wrong, I thought that 'gedit' would change file format. I updated my answer. – Lety Aug 12 '14 at 13:24
  • Its working now, thanks for all the help! I know my problems are a little bit "specific" because of my lack of knowledge :) – user1880779 Aug 12 '14 at 15:32