Environment variable inheritance in service


#1

We create a file from a template

template "/etc/init.d/service_name" do
  source 'service.erb'
  owner "root"
  group "root"
  mode "0755"
end

And therefore we have “service.erb” (short version)

<% cmd = "\"/etc/app/bin/app-name \"" %>
cmd=<%= cmd %>
case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        cd "$dir"
        sudo $cmd
...

and then in a recipe we invoke the service

service "service_name" do
  action :start
end

This is working fine, the app starts and the service response to start stop or restart.

The problem is that we now need wkhtmltopdf and it is not found because the folder where it is installed (usr/local/bin) is not in the environment variable PATH does not include this folder

How can I alter the environment variable PATH?

I read many proposals, maybe I did something wrong when applying them, maybe I don’t understand them well enough to see that they are not addressing what I want, but I am stuck.

I tried to change the PATH:

  • in the service.erb right before the sudo with an export
  • in the service.erb with Environment=“PATH=#thepath#”
  • right before “template “/etc/init.d/service_name” do” ( but I guess it does not make sense)
  • right before service “evermind-#{app}” do

I tried other stuff (inside " template “/etc/init.d/service_name” do") without success.

How do you achieve this?

Thanks

Gerald


#2

Does any one have an opinion on the subject?

Thanks;


#3

This should be the proper method. something like this should work (for start,stop, status, etc):

export PATH=/usr/local/bin:$PATH
<% cmd = "\"/etc/app/bin/app-name \"" %>
cmd=<%= cmd %>

As you’re not saying how it fails exactly, it’s hard to be give more details (is it a program missing or a library ? maybe the PATH is not the variable to update here)


#4

Thanks Tenibai for your suggestion. I tried the export instruction. It did not work; The way it fails is that the application is starting fine. But when I try to generate a pdf I have the following:

 No wkhtmltopdf executable found at /sbin:/bin:/usr/sbin:/usr/bin]

After a programmatic “which wkhtmltopdf” the code in Pdf.scala throws an exception :

      throw new NoExecutableException(System.getenv("PATH"))

So it is the reason I thought the PATH should be updated.
Seeing the printed PATH value ( /sbin:/bin:/usr/sbin:/usr/bin) it is as if the export had no impact.

What I am running is a Scala application.


#5

There’s a good chance your OS vendor compiles sudo with an option that makes it reset the PATH regardless of whether you pass -E or do anything else. Removing sudo from the script would probably make everything easier.


#6

ohh, I am going to try thx


#7

Thanks @kallistec ! You solved it.
Thanks @Tensibai for your help too.

Gerald