# Best way to set all GPIOs to zero before container shutdown?

**URL:** <https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183>\
**Category:** Product support\
**Tags:** docker\
**Created:** [July 17, 2020, 7:06am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183 "2020-07-17T07:06:30Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 17, 2020, 7:06am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/1 "2020-07-17T07:06:30Z")

</div>

I would like to ensure that, before a container is shutdown, during the OTA process, the outputs of the IO modules connected to the RPI are set to zero. I was thinking about something like [https://www.npmjs.com/package/node-cleanup](http://node-cleanup), but I would like to make sure that it will not tamper with the balena cloud logics. Will there be any side effects?

---

<div class="post-metadata">

**Author:** ![kaisoz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/kaisoz/32/3570_2.png) [@kaisoz](https://forums.balena.io/u/kaisoz)\
**Post date:** [July 17, 2020, 9:05am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/5 "2020-07-17T09:05:12Z")

</div>

Hi

No, it shouldn’t cause any problem for the supervisor 🙂

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 18, 2020, 3:51pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/7 "2020-07-18T15:51:16Z")

</div>

@kaisoz I have tested the issue with node-clean up mentioned above, and the code below:

```auto
const IO = require('./io');
var nodeCleanup = require('node-cleanup')

nodeCleanup(function (exitCode, signal) {
  if (signal) {
    console.log(`Running cleanup...`)

    try {
      IO.detach()
      IO.reset()
      nodeCleanup.uninstall();
    } catch(e) {
      console.log(e)
    }
  }
});

module.exports = nodeCleanup

```

But nothing seems to happen. I have tested the code in docker-compose without balena, and the script is called correctly when i run docker-compose down. How does balena shutdown the containers? Meaning, what command does it use?

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 19, 2020, 8:51am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/8 "2020-07-19T08:51:06Z")

</div>

Fyi, I have run a separate project to be able to test issue, where I am running an express server using docker compose in balena-cloud. When stopping the container, the cleanup code does not seem to get called, as the “Running cleanup…” message is not shown on the web console… or, I am thinking, is it possible that the code is actually getting called, but the console detached from the container already?

UPDATE: I have been doing more testing outside of balena, and the cleanup seems to work only on docker-compose down. If I docker kill a container, the cleanup code does not get called. By looking at the balena cloud web console, I see balena says “killing container”, which I am taking to mean that the docker kill command is being used to stop a container. @kaisoz can you confirm?

Assuming those thoughts are correct, and that balena uses SIGKILL to stop a container, then it’s impossible to run cleanup code, as the SIGKILL signal cannot be intercepted. What other options are there to ensure all pins are set to zero on container shutdown then?

---

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [July 20, 2020, 11:29am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/10 "2020-07-20T11:29:24Z")

</div>

Hi, the phrasing in the logs is a bit confusing, unfortunately. But no, sigkill is not used directly to shutdown a container.

When you see the supervisor logs saying about killing a service, the suprevisor actually sends a container stop request to the containers engine.

> <https://github.com/balena-io/balena-supervisor/blob/54cbfface2f8514a46b1aeb277ef90ca0a000d85/src/compose/service-manager.ts#L529>

This is equivalent to running `docker stop` command.  
The engine favors the container stop signal settings that can be defined on your image level[1] or on the container level[2] in the compose file.  
1- [https://docs.docker.com/engine/reference/builder/#stopsignal](https://docs.docker.com/engine/reference/builder/#stopsignal)  
2 - [https://docs.docker.com/compose/compose-file/compose-file-v2/#stop\_signal](https://docs.docker.com/compose/compose-file/compose-file-v2/#stop_signal)

So, the engine first sends the signal specified as the stop signal for the container and then can send a sigkill after a timeout.

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 20, 2020, 12:20pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/12 "2020-07-20T12:20:51Z")

</div>

Is it possible that the web console is just not showing it then?

---

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [July 20, 2020, 12:24pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/13 "2020-07-20T12:24:22Z")

</div>

Sorry, I did’t get the last question.  
What is not shown on the dashboard?

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 20, 2020, 12:27pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/14 "2020-07-20T12:27:16Z")

</div>

In my cleanup code I am writing a string to the log that, on shutdown, should be displayed in the console:

```auto
console.log(`Running cleanup...`)

```

(see example above) to show that the clean up code is actually running. Unfortunately, it never appears in the log console in balena-cloud. Would you expect it to show?

UPDATE: I have just tested cleanup with docker by using the docker stop command and it seems to work. So it does not seem to be a docker issue.

---

<div class="post-metadata">

**Author:** ![roman-mazur](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/roman-mazur/32/2194_2.png) [@roman-mazur](https://forums.balena.io/u/roman-mazur)\
**Post date:** [July 20, 2020, 12:55pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/15 "2020-07-20T12:55:49Z")

</div>

Ah, got it.

Yeah, it might be an issue (probably the supervisor does not capture the logs after sending the stop command).  
Would you test it with `balena-engine ps -a`, and then `balena-engine logs` on the device to check container logs (you could try issuing `balena-engine stop` command manually to test, just keep in mind that the supervisor will try to bring the service up again eventually, to reach the target state dictated by the. backend).

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 20, 2020, 2:38pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/17 "2020-07-20T14:38:26Z")

</div>

Mm I just ran a test using balena-engine, and no message is showing up on container stop… I also tried sending an HTTP request to a server in the callback, but nothing seems to be sent…

---

<div class="post-metadata">

**Author:** ![nazrhom](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@nazrhom](https://forums.balena.io/u/nazrhom)\
**Post date:** [July 20, 2020, 2:57pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/18 "2020-07-20T14:57:01Z")

</div>

Hey, just to clarify did you try to run `balena-engine stop <containerID>` followed by `balena-engine logs <containerID>` and didn’t observe the `Running cleanup` message in the container logs?

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 20, 2020, 2:59pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/19 "2020-07-20T14:59:10Z")

</div>

That’s correct, I have used balena-engine logs -f \<CONTAINER\_ID\>, which at some point stops (as expected), but nothing is showing up…

---

<div class="post-metadata">

**Author:** ![nazrhom](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@nazrhom](https://forums.balena.io/u/nazrhom)\
**Post date:** [July 20, 2020, 3:16pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/20 "2020-07-20T15:16:42Z")

</div>

Thanks, let me check with the balena-engine maintainer and get back to you.

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 20, 2020, 3:21pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/21 "2020-07-20T15:21:04Z")

</div>

thanks a lot, I really appreciate your help.

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 20, 2020, 4:16pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/23 "2020-07-20T16:16:00Z")

</div>

Fyi, I have also tried setting the stop\_signal in docker-compose to SIGINT, but no change…

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 21, 2020, 9:36am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/27 "2020-07-21T09:36:12Z")

</div>

@nazrhom I found something interesting in [this stackoverflow question](https://stackoverflow.com/questions/41451159/how-to-execute-a-script-when-i-terminate-a-docker-container/41451517). Basically, it has to do with the PID. In fact, in order for a process running inside a container to receive POSIX signals, it needs to be running as PID 1. In order to achieve this, docker has to be started with the exec format, while mine was being run in shell format (ENTRYPOINT is being advised in the source mentioned above). In fact, I had to change my Docker file from

```auto
CMD node bin/www

```

to

```auto
ENTRYPOINT ["node", "bin/www"]

```

Now the PID of the process that’s running is 1, and when call balena-engine stop \<CONTAINER\_ID\> the cleanup code gets called. Similarly, when I press the “stop” button from the web ui within balena cloud, the cleanup code seems to run correctly.

---

<div class="post-metadata">

**Author:** ![anujdeshpande](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/anujdeshpande/32/3155_2.png) [@anujdeshpande](https://forums.balena.io/u/anujdeshpande)\
**Post date:** [July 21, 2020, 11:16am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/31 "2020-07-21T11:16:06Z")

</div>

Hi

That’s a good lead! What base images are you using for your docker? Can you share that information?

---

<div class="post-metadata">

**Author:** ![tgirotto](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tgirotto](https://forums.balena.io/u/tgirotto)\
**Post date:** [July 21, 2020, 11:18am UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/33 "2020-07-21T11:18:07Z")

</div>

This is the base image I am using

`FROM balenalib/raspberrypi3-python:3-stretch-run`

running on [balenaOS 2.52.7+rev1](https://github.com/balena-os/meta-balena/blob/master/CHANGELOG.md)

---

<div class="post-metadata">

**Author:** ![dtischler](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/dtischler/32/2483_2.png) [@dtischler](https://forums.balena.io/u/dtischler)\
**Post date:** [July 23, 2020, 10:31pm UTC](https://forums.balena.io/t/best-way-to-set-all-gpios-to-zero-before-container-shutdown/156183/39 "2020-07-23T22:31:44Z")

</div>

Thanks Tommaso, I’ll pass that along. Nice work chasing this down!
