Reputation: 71
How can I check if a service is installed on the node (debian and cent os/amazon)?
I want to stop and disable the service if it is installed. If the services doesn´t exist I´m getting an exception at service stopping: Chef::Exceptions::Service: /etc/init.d/service does not exist!
Best regards André
Upvotes: 3
Views: 10223
Reputation: 1216
To expand and modernize the existing answers: If you're using systemd you can still check the files in a guard, but they'll be in a slightly different location.
You'd want to check for the existence of a file named "servicename.service" (ex. telnet.service, named.service, etc.) in /usr/lib/systemd/system
Add an only_if statement to the resource and you're golden!
Example:
service "service_name" do
action :stop
only_if { File.exist?("/usr/lib/systemd/system/service_name.service") }
end
Upvotes: 1
Reputation: 479
Don't forget: Systemd changes the world, including your service specification location. It's no longer a predictable filename in only one spot, but should exist somewhere under /usr/lib/systemd. If your system is inflicted with systemd and your File.exist?() check isn't working, make sure you're looking in the right new spot.
Upvotes: 0
Reputation: 33
use this for check
only_if { ::File.exist?("/etc/init.d/my_service") }
Upvotes: 1
Reputation: 15784
One solution would be to add a guard in your service resource:
service "my_service" do
action :stop,:disable
only_if { File.exist?("/etc/init.d/my_service") }
end
another one would be to guard the definition of the resource itself: Disclaimer: this is untested code for this example.
service "my_service" do
action :stop,:disable
end.if File.exist?("/etc/init.d/my_service")
and a last one more intuitive
if File.exist?("/etc/init.d/my_service")
service "my_service" do
action :stop,:disable
end
end
the main difference between the first example and the others is that the first one will add a resource to the collection at compile time anyway where the other will only if the file exist so you avoid a new object for this service if it's not there.
Precision, the two last solution have a little drawback, if another recipe install the service they won't see it at first time because when the recipe is compiled the file is not there, so you may end up with the service running until the next run of chef.
Upvotes: 5