# How to check that service is stopped, and if not - stop it?

**URL:** <https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563>\
**Category:** Chef Infra (archive)\
**Created:** [October 15, 2013, 6:22am UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563 "2013-10-15T06:22:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vladimir\_Skubriev](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/vladimir_skubriev/32/263_2.png) [@Vladimir\_Skubriev](https://discourse.chef.io/u/Vladimir_Skubriev)\
**Post date:** [October 15, 2013, 6:22am UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/1 "2013-10-15T06:22:49Z")

</div>

How to check that service is stopped, and if not - stop it ?

For example in this code block:

# See if needed

> <https://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password>

  
bash "bundle\_redmine\_mylyn" do  
cwd  
"#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"  
user node['apache']['user']  
code \<\<-EOH  
# We doing source because of interactive shell don't read  
our .profile  
source #{node['redmine']['home']}/.rvm/scripts/rvm  
bundle install  
EOH  
end

And Can write some not\_if's only\_if's for example

do  
not\_if  
not\_if  
only\_if  
only\_if  
end

?

--  
Best regards,

CVision Lab System Administrator  
Vladmir Skubriev

---

<div class="post-metadata">

**Author:** ![carmstrong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/carmstrong/32/346_2.png) [@carmstrong](https://discourse.chef.io/u/carmstrong)\
**Post date:** [October 15, 2013, 6:25am UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/2 "2013-10-15T06:25:12Z")

</div>

I'm not sure where in your code sample you are trying to stop a service,  
but you can easily do this with the service resource:

> **[service Resource](https://docs.chef.io/resources/service/)**
>
> Use the service resource to manage a service.

Example:  
service "nginx" do  
action :stop  
end

On Mon, Oct 14, 2013 at 11:22 PM, Vladimir Skubriev \<[skubriev@cvisionlab.com](mailto:skubriev@cvisionlab.com)

> wrote:

> How to check that service is stopped, and if not - stop it ?
> 
> For example in this code block:
> 
> # See if needed [http://stackoverflow.com/\*\*questions/14809840/](http://stackoverflow.com/**questions/14809840/)\*\*
> 
> installation-of-redmine-with-\*\*bundler-requires-root-password[http://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password](http://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password)  
> bash "bundle\_redmine\_mylyn" do  
> cwd "#{node['redmine']['datafs']}/ **redmine/plugins/redmine\_mylyn\_**  
> connector"  
> user node['apache']['user']  
> code \<\<-EOH  
> # We doing source because of interactive shell don't read  
> our .profile  
> source #{node['redmine']['home']}/.\*\*rvm/scripts/rvm  
> bundle install  
> EOH  
> end
> 
> And Can write some not\_if's only\_if's for example
> 
> do  
> not\_if  
> not\_if  
> only\_if  
> only\_if  
> end
> 
> ?
> 
> --  
> Best regards,
> 
> CVision Lab System Administrator  
> Vladmir Skubriev

---

<div class="post-metadata">

**Author:** ![Vladimir\_Skubriev](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/vladimir_skubriev/32/263_2.png) [@Vladimir\_Skubriev](https://discourse.chef.io/u/Vladimir_Skubriev)\
**Post date:** [October 15, 2013, 6:54am UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/3 "2013-10-15T06:54:03Z")

</div>

On 10/15/2013 10:25 AM, Christopher Armstrong wrote:

> I'm not sure where in your code sample you are trying to stop a  
> service, but you can easily do this with the service resource:  
> [http://docs.opscode.com/resource\_service.html](http://docs.opscode.com/resource_service.html)
> 
> Example:  
> service "nginx" do  
> action :stop  
> end
> 
> On Mon, Oct 14, 2013 at 11:22 PM, Vladimir Skubriev  
> \<[skubriev@cvisionlab.com](mailto:skubriev@cvisionlab.com) [mailto:skubriev@cvisionlab.com](mailto:skubriev@cvisionlab.com)\> wrote:
> 
> ```
> How to check that service is stopped, and if not - stop it ?
> 
> For example in this code block:
> 
> # See if needed
> http://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password
> bash "bundle_redmine_mylyn" do
> cwd
> "#{node['redmine']['datafs']}/redmine/plugins/redmine_mylyn_connector"
> user node['apache']['user']
> code <<-EOH
> # We doing source because of interactive shell don't
> read our .profile
> source #{node['redmine']['home']}/.rvm/scripts/rvm
> bundle install
> EOH
> end
> 
> And Can write some not_if's only_if's for example
> 
> do
> not_if
> not_if
> only_if
> only_if
> end
> 
> ?
> 
> -- 
> Best regards,
> 
> CVision Lab System Administrator
> Vladmir Skubriev
> 
> ```

Okey.

May be i ask answer some incorrectly  
I need a check if a service stopped  
Before migration of db in this code block

bash "bundle\_redmine\_mylyn" do  
cwd  
"#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"  
user node['apache']['user']  
code \<\<-EOH  
# We doing source because of interactive shell don't read  
our .profile  
source #{node['redmine']['home']}/.rvm/scripts/rvm  
bundle install  
EOH  
not\_if SERVICE\_IS\_STARTED  
end

i.e. SERVICE\_IS\_STARTED in ruby ?

--  
Best regards,

CVision Lab System Administrator  
Vladmir Skubriev

---

<div class="post-metadata">

**Author:** ![Seth\_Falcon\_01](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/seth_falcon_01/32/429_2.png) [@Seth\_Falcon\_01](https://discourse.chef.io/u/Seth_Falcon_01)\
**Post date:** [October 15, 2013, 2:42pm UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/4 "2013-10-15T14:42:05Z")

</div>

[skubriev@cvisionlab.com](mailto:skubriev@cvisionlab.com) writes:

> May be i ask answer some incorrectly  
> I need a check if a service stopped  
> Before migration of db in this code block
> 
> bash "bundle\_redmine\_mylyn" do  
> cwd  
> "#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"  
> user node['apache']['user']  
> code \<\<-EOH  
> # We doing source because of interactive shell don't read  
> our .profile  
> source #{node['redmine']['home']}/.rvm/scripts/rvm  
> bundle install  
> EOH  
> not\_if SERVICE\_IS\_STARTED  
> end

You can guard your bash resource with either a string which will be  
executed as a shell command or a block of Ruby code. So:

```
bash "bundle_redmine_mylyn" do
   cwd 
"#{node['redmine']['datafs']}/redmine/plugins/redmine_mylyn_connector"
   user node['apache']['user']
   code <<-EOH
               # We doing source because of interactive shell don't read 
our .profile
               source #{node['redmine']['home']}/.rvm/scripts/rvm
               bundle install
   EOH
   not_if { File.exist?(some_file) }
end

```

Does that help?

--  
Seth Falcon | Development Lead | Opscode | @sfalcon

---

<div class="post-metadata">

**Author:** ![Vladimir\_Skubriev](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/vladimir_skubriev/32/263_2.png) [@Vladimir\_Skubriev](https://discourse.chef.io/u/Vladimir_Skubriev)\
**Post date:** [October 16, 2013, 5:02am UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/5 "2013-10-16T05:02:35Z")

</div>

On 10/15/2013 06:42 PM, Seth Falcon wrote:

> [skubriev@cvisionlab.com](mailto:skubriev@cvisionlab.com) writes:
> 
> > May be i ask answer some incorrectly  
> > I need a check if a service stopped  
> > Before migration of db in this code block
> > 
> > bash "bundle\_redmine\_mylyn" do  
> > cwd  
> > "#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"  
> > user node['apache']['user']  
> > code \<\<-EOH  
> > # We doing source because of interactive shell don't read  
> > our .profile  
> > source #{node['redmine']['home']}/.rvm/scripts/rvm  
> > bundle install  
> > EOH  
> > not\_if SERVICE\_IS\_STARTED  
> > end  
> > You can guard your bash resource with either a string which will be  
> > executed as a shell command or a block of Ruby code. So:
> 
> ```
> bash "bundle_redmine_mylyn" do
> cwd
> "#{node['redmine']['datafs']}/redmine/plugins/redmine_mylyn_connector"
> user node['apache']['user']
> code <<-EOH
> # We doing source because of interactive shell don't read
> our .profile
> source #{node['redmine']['home']}/.rvm/scripts/rvm
> bundle install
> EOH
> not_if { File.exist?(some_file) }
> end
> 
> ```
> 
> Does that help?

That's help of course.  
I know how I can do check in bash. Purely theoretically I can use next  
condition

not\_if { File.exist?(/run/apache2.pid) }

But I was hope that chef has a built-in syntax to check service status.

Because I don't want to waste a time to create and run script if script  
should not run.

Thank you for hint.

--  
Best regards,

CVision Lab System Administrator  
Vladmir Skubriev

---

<div class="post-metadata">

**Author:** ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)\
**Post date:** [October 16, 2013, 12:18pm UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/6 "2013-10-16T12:18:27Z")

</div>

Le 2013-10-15 08:22, Vladimir Skubriev a écrit :

> How to check  
> that service is stopped, and if not - stop it ?
> 
> For example in this  
> code block:
> 
> # See if needed

> <https://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password>

[1]

> bash "bundle\_redmine\_mylyn" do  
> cwd

"#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"

> 

user node['apache']['user']

> code \<\<-EOH
> 
> # We doing source because of

interactive shell don't read

> our .profile  
> source  
> #{node['redmine']['home']}/.rvm/scripts/rvm  
> bundle install  
> EOH

end

> And Can write some not\_if's only\_if's for example
> 
> do

not\_if

> not\_if  
> only\_if  
> only\_if  
> end
> 
> ?

I would use  
notifications for this use case, something like:

service "apache2"

bash "bundle\_redmine\_mylyn" do  
cwd

"#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"

user node['apache']['user']  
code \<\<-EOH

# We doing source because of

interactive shell don't read  
our .profile  
source  
#{node['redmine']['home']}/.rvm/scripts/rvm  
bundle install  
EOH  
action  
:nothing  
end

ruby\_block "stop apache" do

## Links:

[1]

> <https://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password>

---

<div class="post-metadata">

**Author:** ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)\
**Post date:** [October 16, 2013, 12:25pm UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/7 "2013-10-16T12:25:03Z")

</div>

Bis from mail client bug ...

> Le 2013-10-15 08:22, Vladimir  
> Skubriev a écrit :
> 
> > How to check that service is stopped, and if  
> > not - stop it ?
> > 
> > For example in this code block:
> > 
> > # See if

needed

> >

> <https://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password>

[1]

> > bash "bundle\_redmine\_mylyn" do  
> > cwd

"#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"

> >

user node['apache']['user']

> > code \<\<-EOH
> > 
> > # We doing source because

of interactive shell don't read

> > our .profile  
> > source  
> > #{node['redmine']['home']}/.rvm/scripts/rvm  
> > bundle install  
> > EOH

end

> > And Can write some not\_if's only\_if's for example

do

> > not\_if  
> > not\_if  
> > only\_if  
> > only\_if  
> > end
> > 
> > ?

I would  
use notifications for this use case, something like:

bash  
"bundle\_redmine\_mylyn" do  
cwd

"#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"

user node['apache']['user']  
code \<\<-EOH

# We doing source because of

interactive shell don't read  
our .profile  
source  
#{node['redmine']['home']}/.rvm/scripts/rvm  
bundle install  
EOH  
action  
:nothing  
notifies :start, "service[apache2]  
end

service "apache2" do

action :stop

notifies :run, "bash[bundle\_redmine\_mylyn]"

end

More or less from memory so there may be syntax mistakes with  
notifications but the idea is there, if action :stop is too much it may  
be trigerred by another ressource (a dummy ruby\_block just notyfing or  
anything else)

The major drawback I see from this way is that it is  
NOT idempotent and will do the same again and again at each chef-run if  
there's not way to test the install has to be done or not.

Regards,

Tensibai

## Links:

[1]

> <https://stackoverflow.com/questions/14809840/installation-of-redmine-with-bundler-requires-root-password>

---

<div class="post-metadata">

**Author:** ![Vladimir\_Skubriev](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/vladimir_skubriev/32/263_2.png) [@Vladimir\_Skubriev](https://discourse.chef.io/u/Vladimir_Skubriev)\
**Post date:** [October 18, 2013, 7:51am UTC](https://discourse.chef.io/t/how-to-check-that-service-is-stopped-and-if-not-stop-it/4563/8 "2013-10-18T07:51:49Z")

</div>

On 10/16/2013 04:25 PM, Tensibai wrote:

> Bis from mail client bug ...

no problem )

> > 
> 
> I would use notifications for this use case, something like:
> 
> bash "bundle\_redmine\_mylyn" do  
> cwd  
> "#{node['redmine']['datafs']}/redmine/plugins/redmine\_mylyn\_connector"  
> user node['apache']['user']  
> code \<\<-EOH  
> # We doing source because of interactive shell don't read  
> our .profile  
> source #{node['redmine']['home']}/.rvm/scripts/rvm  
> bundle install  
> EOH  
> action :nothing  
> notifies :start, "service[apache2]  
> end
> 
> service "apache2" do
> 
> action :stop
> 
> notifies :run, "bash[bundle\_redmine\_mylyn]"
> 
> end

I has many bundle and rake block in a whole cookbook, which is setting  
up redmine entirely

I need a check for security of production server data in during operation.

Because application server runned by apache should be stopeed. Bundle is  
not a clear example.

Must clear is rake db:migrate, for example.

Of course now i use in the home of all recipes

service "apache2" do

action :stop

end

And at the end of all code

service "apache2" do

action :start

end

But I think this is not good idea.

> More or less from memory so there may be syntax mistakes with  
> notifications but the idea is there, if action :stop is too much it  
> may be trigerred by another ressource (a dummy ruby\_block just  
> notyfing or anything else)
> 
> The major drawback I see from this way is that it is NOT idempotent  
> and will do the same again and again at each chef-run if there's not  
> way to test the install has to be done or not.
> 
> Regards,
> 
> Tensibai

--  
Best regards,

CVision Lab System Administrator  
Vladmir Skubriev
