How to execute "source" command?
Hi,
How do I execute "source /usr/local/rvm/scripts/rvm" command using exec type reference?
Regards, a Puppet user.
First time here? Check out the FAQ!
Hi,
How do I execute "source /usr/local/rvm/scripts/rvm" command using exec type reference?
Regards, a Puppet user.
This comes up quite a bit so I'm going to try to explain all the interesting parts in one go.
The Exec resource uses the Posix provider by default
The Exec resource uses /bin/sh when you specify shell
The important parts to remember is that any Bashism or other non Posix functions are missing. Most users have seen exactly this problem when they try to run a script from crontab without specifying a shell or environment.
The Exec shell does not load PATH
You will need to specify paths for all commands in the script or set path. Many people set a default path for Exec resources in site.pp, but you set them per Exec as well.
Exec { path => '/usr/bin:/bin' }
How to use Exec with a particular shell, source files, have access to ENV, etc
Using RVM as an example your Exec resource might look like the following.
exec { 'passenger_install_gem':
user => $rvm::user,
command => "bash -c 'source ~/.rvm/scripts/rvm ; gem install passenger -v ${passenger::version}'",
unless => "bash -c 'source ~/.rvm/scripts/rvm ; gem list | grep passenger | grep ${passenger::version}'",
logoutput => on_failure,
path => '/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin' }
}
Notice that we've specified the user we plan to run as rather than letting it default to root. Then we spawn a Bash shell with -c, source the necessary environment files, and finally execute our command. I recommend setting logoutput => on_failure, when working with Exec resources.
// , Ramindk, did you mean to say, "The important parts to remember is that any Bashism or other non Posix functions will not work." in the third sentence, there?
You can avoid sourcing the rvm file, if you do bash --login -c 'your command'. This will behave like a normal bash (with sourcing .bash_rc [which includes the rvm] + setting up the PATH in a proper way).
This might be a better solution, because you don't have to take care of the PATH, too.
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2013-07-21 22:03:46 -0600
Seen: 10,355 times
Last updated: Nov 22 '15
Puppet exec complains about non-absolute paths [closed]
exec returns command not working
Path Attribute File to Exec Command - Windows Agent bat file
Can somebody explain the command in below code?
Modify $path variable between Exec resources
How to avoid an error when an exec command return 1