# Monitoring and starting service

**URL:** <https://discourse.nodered.org/t/monitoring-and-starting-service/1330>\
**Category:** General\
**Created:** [4 July 2018 18:26 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330 "2018-07-04T18:26:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 18:26 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/1 "2018-07-04T18:26:07Z")

</div>

Hi!

Is there any way to monitor a linux service and if in case it is stopped to start the same?

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [4 July 2018 18:43 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/2 "2018-07-04T18:43:30Z")

</div>

for instance to see node if node red service is status:

systemctl is-active nodered.service

To start/stop/restart

sudo service nodered start/stop/restart

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 July 2018 19:01 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/3 "2018-07-04T19:01:46Z")

</div>

What hardware/OS are you running? If it is a pi and you have updated using the bash \<(curl script then it will automatically restart if it stops, unless it continuously crashes in which case it will give up after a few goes. The script that controls this is `/lib/systemd/system/nodered.service`

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 19:37 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/4 "2018-07-04T19:37:10Z")

</div>

What I want to monitor is the service of another application (HA - Home Assistant).  
The service is stopping halfway unexpectedly. I wanted to monitor this service if it were to stop the red node would start it.  
The Home Assistant and the Red Node are running on the same Raspberry Pi.  
The raspberry pi does not turn off, only the Home Assistant service it stops. The NR continues to run normally.

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [4 July 2018 19:46 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/5 "2018-07-04T19:46:59Z")

</div>

with the commands I posted you, u can inject a timestamp every x seconds and run an exec node with this command and monitor the output and if is stopped then trigger another exec node to start it, however maybe will be good idea to check why service stops to solve the issue…

regards

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 20:08 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/6 "2018-07-04T20:08:13Z")

</div>

I have already made several changes in the Home Assistant to try to identify why it is entering stop mode but could not identify the reason.  
In parallel I am seeing this possibility.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 July 2018 20:08 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/7 "2018-07-04T20:08:23Z")

</div>

Do you have HA writing to MQTT? If so then subscribe to the Last Will And Testament message which will be sent if HA crashes.

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 20:11 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/8 "2018-07-04T20:11:01Z")

</div>

Yes, I already have the mqtt set up.  
Then it would be more or less a watchdog, I send the command and wait for the Home Assistant to respond?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 July 2018 20:32 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/9 "2018-07-04T20:32:38Z")

</div>

> [@dedynobre](#):
>
> Then it would be more or less a watchdog, I send the command and wait for the Home Assistant to respond?

No, the LWT will send you a message if HA disconnects unexpectedly (but not if it does a clean shutdown).

> **[What is MQTT Last Will and Testament (LWT)? – MQTT Essentials: Part 9](https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/)**
>
> Learn the basics of MQTT Last Will and Testament (LWT) & how it helps maintain system integrity in the event of unexpected client disconnections.

Otherwise then yes, use a watchdog method. Subscribe to something that HA publishes regularly (add that to the HA setup if there isn't already something) and if it stops coming then HA has stopped

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 21:01 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/10 "2018-07-04T21:01:50Z")

</div>

The MQTT Broker, NR and Home Assistant is in same machine.  
Will this work?

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [4 July 2018 21:05 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/11 "2018-07-04T21:05:26Z")

</div>

sure… is how normally ppl have it

---

<div class="post-metadata">

**Author:** ![asandford](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@asandford](https://discourse.nodered.org/u/asandford)\
**Post date:** [4 July 2018 21:15 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/12 "2018-07-04T21:15:17Z")

</div>

I think you should find out why HA keeps on stopping.

The OS also has tools available to monitor and restart daemons.

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 22:24 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/13 "2018-07-04T22:24:32Z")

</div>

I already did everything and could not identify the problem.  
I declined the size of log files, and database went one time without stopping. Now it sometimes reboots.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 July 2018 22:26 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/14 "2018-07-04T22:26:23Z")

</div>

You may want to discuss this on the [Home Assistant forum](https://community.home-assistant.io/) as it isn’t related to Node-RED and they maybe able to help you identify why HA is restarting.

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 22:32 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/15 "2018-07-04T22:32:08Z")

</div>

Right.  
But I will try to look at this second option to monitor and start the service if it is stopped.

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [4 July 2018 22:35 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/16 "2018-07-04T22:35:02Z")

</div>

I’ll try here but I still do not understand what the LWT deployment would look like.

---

<div class="post-metadata">

**Author:** ![asandford](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@asandford](https://discourse.nodered.org/u/asandford)\
**Post date:** [4 July 2018 22:44 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/17 "2018-07-04T22:44:17Z")

</div>

Using NR or MQTT to monitor a failing process is just a band-aid over your real problem, As Nick said, head over to the HA forum for support.

Failing that, google what systemd can do to restart failed daemons.

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [5 July 2018 00:04 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/18 "2018-07-05T00:04:04Z")

</div>

Another thing,

I have the HA component installed in the NR and it is already working here.

I saw an interesting thing that in the HA node is appearing disconnected.

Would not it be possible to get this status and make a comparison and soon after starting the service?

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [5 July 2018 00:25 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/19 "2018-07-05T00:25:03Z")

</div>

dedy I explained you already one way to monitor the status of the service and start it up again upon disconnection,

However as other people told you I think your are focusing on the incorrect path…  
Try t see whatever log that HA gives you or some help to solve the issue with HA, one main test to be done is to make a clean install with latest versions and see what happens,

If I understood well is a rasberry what makes easy to backup the micro sd card and recover your image at any moment, but this is an exercise that maybe you should do if you can not troubleshoot HA further.

If you have a puncture on a wheel on your car, solution is not try to be blowing air all the time on it to keep car running…

Check also how is your system running in terms of HD space, ram, processor,temperature  
etc…

Regards

---

<div class="post-metadata">

**Author:** ![dedynobre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dedynobre/32/950_2.png) [@dedynobre](https://discourse.nodered.org/u/dedynobre)\
**Post date:** [5 July 2018 00:27 UTC](https://discourse.nodered.org/t/monitoring-and-starting-service/1330/20 "2018-07-05T00:27:31Z")

</div>

It’s all right.

Thank you and sorry.

[Next page](https://discourse.nodered.org/t/monitoring-and-starting-service/1330.md?page=2)
