4

I have a script that runs at startup from rc.local in CentOS. This script contains a command to compile my erlang project using a third party build command called rebar. The rebar line fails to compile my project at startup, but if I then run it manually, the project compiles fine. I assume this is because the rebar command calls other commands without absolute paths.

How can I run a command at startup, but in a context that does not require absolute paths?

I don't want to have to patch this third party command (and deal with the inevitable bureaucracy that attends that mess) and/or keep my own parallel fork of the project just to maintain this one extra feature.

I'm also curious why I can't Google this. It seems like it must come up so often that people wish to use a command in rc.local which itself does not use all absolute path command calls. Moreover, I feel like the answer should be trivial, since I can login immediately after startup and run any command I like, as root, which is the user that rc.local runs as too! Isn't there a hook where you can run a script in the context of "yes, we really are booted up all the way now, let's run this script as a fully peer user on the system without any arbitrary limitations"?

This is not the first time I've run into this problem and I'd love to understand what's going on.

EDIT:

(I'm not going to wait 8 hours to answer my own question, so...)

Well, it looks like setting PATH works. I think I had got the impression that commands in rc.local and its children just aren't searched for in PATH, even after calling a bash script, but that's wrong. (@TiZon gave me a good clue.) The reason I got this impression is because every Google result told the questioner to use absolute paths in command calls, even though this is cumbersome for anything beyond trivial scripts or scripts that depend on third party commands that don't follow this rule. Instead, the answer is to call out to bash script with a proper shebang line, find out what your normal environment's PATH is by echoing it during a regular session, and then set the PATH in your startup bash script to the regular one.

Hopefully this answer will float up in Google and help some other poor lost souls.

mwt
  • 43
  • 1
  • 4

2 Answers2

1

Try putting /bin/bash in front of your command. You can't use relative paths because there is no environment to keep track of them. If you run everything in a bash-shell, bash will keep track of these things for you. Same goes for crontab

Bart De Vos
  • 17,761
  • 6
  • 62
  • 81
0

Bear in mind that rc.local is run in the path /, not from the path in which it is located.
Therefore if you call a script from rc.local such as
/home/myscript
and if that script itself contains relative paths (i.e. it has been written with the assumption that it is running from /home), then it will fail when run from /.
Any script you want to run from rc.local should be tested by running it from /:
cd /
./home/myscript
If it runs OK like that, then you can be sure that there is not a problem with absolute/relative paths