0

I'm in the process of cleaning up many years of hackery and I'm hoping to get some help. I am wanting to convert a shell script I wrote that uses netcat to connect to a Mochad (X10 interface) container and then grabs the output and formats it and passes that along to my MQTT server. This has worked great for years but now I'd like to make it into a simple Docker container.

Here's the simple script I want to convert (mochadtomqtt.sh):

#!/bin/sh
nc  192.168.0.25 1099 | awk ' /HouseUnit:/ && /Func:/ { system("mosquitto_pub -h 192.168.0.25 -q 1 -t /X10/"$6" -m "$8) } '

and this is the Dockerfile I've tried:

FROM ubuntu:14.04

RUN apt-get update && apt-get install -y mosquitto-clients && apt-get install -y mosquitto

CMD ["/x10/mochadtomqtt.sh", ""]

and finally, the portion from my docker-compose:

  x102mqtt:
    container_name: x102mqtt
    image: 'x102mqtt:latest'
    restart: unless-stopped
    depends_on:
      - mqtt        
    volumes:
      - /sharedfolders/appdata/x102mqtt:/x10          
    network_mode: host 

I've built the image and deployed the container but I do not get any sort of output. No errors no nothing. I know the script is being run as I've added an echo "hello world" and that did show up repeatedly. Can someone tell me how I might debug this or if they can spot my obvious errors (outside that I have no experience at doing this).

Many thanks!

EDIT to add more information:

This is what the output from nc looks like:

04/04 15:47:17 Rx RF HouseUnit: B3 Func: On

Answering @The Fool:

I copied most of this from other files. Noted on the command options being blank, I can remove that. Not sure about the restarting as this is intended to be a one shot call. What causes the restarts?

EDIT

I changed the script to ensure that nc, mosquitto_pub and awk were indeed available. They are. I put the full path to these executables in the commandline. I also added an echo to see if the command was running or if it had ended. It ended.

Here's the script modified:

#!/bin/sh
which nc
which mosquitto_pub
which awk
/bin/nc  192.168.0.25 1099 | /usr/bin/awk ' /HouseUnit:/ && /Func:/ { system("/usr/bin/mosquitto_pub -h 192.168.0.25 -q 1 -t /X10/"$6" -m "$8) } '
echo "End"

and resulting output:

/bin/nc
/usr/bin/mosquitto_pub

/usr/bin/awk
End

I'm not sure what that strange square character comes from. I also did a ping previously to the IP I'm trying to connect to and it did ping fine within the container.

Not sure why nc is exiting instead of staying connected. I get no errors at all.

hardillb
  • 54,545
  • 11
  • 67
  • 105
dinki
  • 103
  • 7
  • why do you have an empty string as second item in the CMD array and where do the arguments $6 and $8 in the script come from? Also something with the quoting seems off in that script. Near the end for example. – The Fool Apr 04 '22 at 20:20
  • Restarting it seems also a bit strange if its supossed to be a one shot command. This might explain the repeated hello world. – The Fool Apr 04 '22 at 20:21
  • @TheFool See my comments added to my OP – dinki Apr 04 '22 at 20:49
  • you have this in your compose.yaml `restart: unless-stopped`. I am not 100% sure right now if it would restart when your container exists cleanly or not, but I think it does. I would remove this line from your yaml. – The Fool Apr 04 '22 at 21:02
  • @TheFool Ahh okay. So this script continues to listen to that port forever and passes any message it receives to MQTT broker. I had not realized that it was not sitting there and waiting like I had expected. So something with my script is not working like it should. I have no indication of what that might be though. – dinki Apr 04 '22 at 21:14
  • you can start the container interactivly and run your script and debug from inside. Its proably easier. `docker run --rm -ti x102mqtt bash` – The Fool Apr 04 '22 at 21:22
  • I also notice you have depends on mqtt, make sure you use this as `mqtt` as hostname when trying to communicate with it. I assume its another compose service, right? – The Fool Apr 04 '22 at 21:23
  • you can also put `set -x` at the top of your script to see what its doing x for execute I guess. And I also put `set -e` most of the time so that it exists on error. – The Fool Apr 04 '22 at 21:28
  • @thefool Running the exact same command line debuging from inside via bash works perfectly. So it should work but I have something that is not right. Docker does call the script as I do see output from it but I can't put it all together. I am not smart enough to know how to mount the volume using the bash example you gave so I can't actually test the script like it's being called from the container (if that makes sense) – dinki Apr 04 '22 at 21:31
  • just add another flag `-v /sharedfolders/appdata/x102mqtt:/x10` – The Fool Apr 04 '22 at 21:32
  • oh and regarding mqtt as hostname, that was wrong. I just see that you have network_mode host. Its not recommended to do that. Its better to add the gateway as extra host. See https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal – The Fool Apr 04 '22 at 21:35
  • @TheFool Adding that volume allowed me to run the script from the command line of the container and it works just as I would want. So it seems that the container has all the components to work and the script works fine as well but when called by Docker it just exits. I don't get it. I also changed to #!/bin/bash to see if that would help but it did not. – dinki Apr 04 '22 at 21:37
  • if that worked as expected, just drop the network_mode host. Because in this case you didnt have it and it worked. It might be the reason for some problems, depending on you are are trying to communicate to mqtt and if its actually another container. – The Fool Apr 04 '22 at 21:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243600/discussion-between-dinki-and-the-fool). – dinki Apr 04 '22 at 21:44

0 Answers0