Bash Script: To log MQTT feed to txt file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have written a small bash script (datalogger.sh) to store the mqtt data on to SD card inside a linux machine. The bash script is as below:-



#!/bin/bash
fileNumber=1
temp=1 // flag to check and create new files
fileName="Data"
while [ $temp -le 1 ]
do
if [ -f "/media/card/$fileName$fileNumber.txt" ]
then
((fileNumber++))
else
touch "/media/card/$fileName$fileNumber.txt"
mosquitto_sub -v -t "gateway/+/rx" | tee /media/card/$fileName$fileNumber.txt
temp=2
fi
done


The bash script works totally fine if i run it with the following command



./datalogger.sh


As of next step, i used the update-rc.d datalogger.sh defaults so that on boot the bash script automatically runs. However, i only get the empty text files. Could anyone guide me, what mistake i am making?
Best Regards,










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have written a small bash script (datalogger.sh) to store the mqtt data on to SD card inside a linux machine. The bash script is as below:-



    #!/bin/bash
    fileNumber=1
    temp=1 // flag to check and create new files
    fileName="Data"
    while [ $temp -le 1 ]
    do
    if [ -f "/media/card/$fileName$fileNumber.txt" ]
    then
    ((fileNumber++))
    else
    touch "/media/card/$fileName$fileNumber.txt"
    mosquitto_sub -v -t "gateway/+/rx" | tee /media/card/$fileName$fileNumber.txt
    temp=2
    fi
    done


    The bash script works totally fine if i run it with the following command



    ./datalogger.sh


    As of next step, i used the update-rc.d datalogger.sh defaults so that on boot the bash script automatically runs. However, i only get the empty text files. Could anyone guide me, what mistake i am making?
    Best Regards,










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have written a small bash script (datalogger.sh) to store the mqtt data on to SD card inside a linux machine. The bash script is as below:-



      #!/bin/bash
      fileNumber=1
      temp=1 // flag to check and create new files
      fileName="Data"
      while [ $temp -le 1 ]
      do
      if [ -f "/media/card/$fileName$fileNumber.txt" ]
      then
      ((fileNumber++))
      else
      touch "/media/card/$fileName$fileNumber.txt"
      mosquitto_sub -v -t "gateway/+/rx" | tee /media/card/$fileName$fileNumber.txt
      temp=2
      fi
      done


      The bash script works totally fine if i run it with the following command



      ./datalogger.sh


      As of next step, i used the update-rc.d datalogger.sh defaults so that on boot the bash script automatically runs. However, i only get the empty text files. Could anyone guide me, what mistake i am making?
      Best Regards,










      share|improve this question















      I have written a small bash script (datalogger.sh) to store the mqtt data on to SD card inside a linux machine. The bash script is as below:-



      #!/bin/bash
      fileNumber=1
      temp=1 // flag to check and create new files
      fileName="Data"
      while [ $temp -le 1 ]
      do
      if [ -f "/media/card/$fileName$fileNumber.txt" ]
      then
      ((fileNumber++))
      else
      touch "/media/card/$fileName$fileNumber.txt"
      mosquitto_sub -v -t "gateway/+/rx" | tee /media/card/$fileName$fileNumber.txt
      temp=2
      fi
      done


      The bash script works totally fine if i run it with the following command



      ./datalogger.sh


      As of next step, i used the update-rc.d datalogger.sh defaults so that on boot the bash script automatically runs. However, i only get the empty text files. Could anyone guide me, what mistake i am making?
      Best Regards,







      shell-script logs mqtt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 16 at 19:13









      Jesse_b

      10.5k22659




      10.5k22659










      asked Aug 16 at 17:37









      Usman Asghar

      1




      1




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Services generaly use wrapper scripts like for example;



          #!/bin/bash

          # Provides: example
          # Required-Start:
          # Should-Start:
          # Required-Stop:
          # Default-Start: 2 3 5
          # Default-Stop: 0 1 2 6
          # Short-Description: example
          # Description: example

          #
          # /etc/init.d/example
          #

          set -e
          trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR

          NPID=$(pgrep -f example2.sh || true);

          function start
          if [ "$NPID" = "" ] ; then
          screen -S example -d -m bash -c '/root/example2.sh'
          else
          echo "example service is already running as $NPID";
          fi


          function stop
          if [ "$NPID" != "" ] ; then
          kill "$NPID";
          else
          echo "example service was not running" >&2;
          fi


          function status
          if [ "$NPID" = "" ]; then
          echo "example is not running";
          else
          echo "example is running with pid $NPID";
          fi


          if [ "$1" = "start" ]; then
          start
          elif [ "$1" = "stop" ]; then
          stop
          elif [ "$1" = "restart" ]; then
          stop;
          start;
          elif [ "$1" = "status" ]; then
          status
          else
          echo " * Usage: /etc/init.d/example start";
          fi





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463028%2fbash-script-to-log-mqtt-feed-to-txt-file%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Services generaly use wrapper scripts like for example;



            #!/bin/bash

            # Provides: example
            # Required-Start:
            # Should-Start:
            # Required-Stop:
            # Default-Start: 2 3 5
            # Default-Stop: 0 1 2 6
            # Short-Description: example
            # Description: example

            #
            # /etc/init.d/example
            #

            set -e
            trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR

            NPID=$(pgrep -f example2.sh || true);

            function start
            if [ "$NPID" = "" ] ; then
            screen -S example -d -m bash -c '/root/example2.sh'
            else
            echo "example service is already running as $NPID";
            fi


            function stop
            if [ "$NPID" != "" ] ; then
            kill "$NPID";
            else
            echo "example service was not running" >&2;
            fi


            function status
            if [ "$NPID" = "" ]; then
            echo "example is not running";
            else
            echo "example is running with pid $NPID";
            fi


            if [ "$1" = "start" ]; then
            start
            elif [ "$1" = "stop" ]; then
            stop
            elif [ "$1" = "restart" ]; then
            stop;
            start;
            elif [ "$1" = "status" ]; then
            status
            else
            echo " * Usage: /etc/init.d/example start";
            fi





            share|improve this answer
























              up vote
              0
              down vote













              Services generaly use wrapper scripts like for example;



              #!/bin/bash

              # Provides: example
              # Required-Start:
              # Should-Start:
              # Required-Stop:
              # Default-Start: 2 3 5
              # Default-Stop: 0 1 2 6
              # Short-Description: example
              # Description: example

              #
              # /etc/init.d/example
              #

              set -e
              trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR

              NPID=$(pgrep -f example2.sh || true);

              function start
              if [ "$NPID" = "" ] ; then
              screen -S example -d -m bash -c '/root/example2.sh'
              else
              echo "example service is already running as $NPID";
              fi


              function stop
              if [ "$NPID" != "" ] ; then
              kill "$NPID";
              else
              echo "example service was not running" >&2;
              fi


              function status
              if [ "$NPID" = "" ]; then
              echo "example is not running";
              else
              echo "example is running with pid $NPID";
              fi


              if [ "$1" = "start" ]; then
              start
              elif [ "$1" = "stop" ]; then
              stop
              elif [ "$1" = "restart" ]; then
              stop;
              start;
              elif [ "$1" = "status" ]; then
              status
              else
              echo " * Usage: /etc/init.d/example start";
              fi





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Services generaly use wrapper scripts like for example;



                #!/bin/bash

                # Provides: example
                # Required-Start:
                # Should-Start:
                # Required-Stop:
                # Default-Start: 2 3 5
                # Default-Stop: 0 1 2 6
                # Short-Description: example
                # Description: example

                #
                # /etc/init.d/example
                #

                set -e
                trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR

                NPID=$(pgrep -f example2.sh || true);

                function start
                if [ "$NPID" = "" ] ; then
                screen -S example -d -m bash -c '/root/example2.sh'
                else
                echo "example service is already running as $NPID";
                fi


                function stop
                if [ "$NPID" != "" ] ; then
                kill "$NPID";
                else
                echo "example service was not running" >&2;
                fi


                function status
                if [ "$NPID" = "" ]; then
                echo "example is not running";
                else
                echo "example is running with pid $NPID";
                fi


                if [ "$1" = "start" ]; then
                start
                elif [ "$1" = "stop" ]; then
                stop
                elif [ "$1" = "restart" ]; then
                stop;
                start;
                elif [ "$1" = "status" ]; then
                status
                else
                echo " * Usage: /etc/init.d/example start";
                fi





                share|improve this answer












                Services generaly use wrapper scripts like for example;



                #!/bin/bash

                # Provides: example
                # Required-Start:
                # Should-Start:
                # Required-Stop:
                # Default-Start: 2 3 5
                # Default-Stop: 0 1 2 6
                # Short-Description: example
                # Description: example

                #
                # /etc/init.d/example
                #

                set -e
                trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR

                NPID=$(pgrep -f example2.sh || true);

                function start
                if [ "$NPID" = "" ] ; then
                screen -S example -d -m bash -c '/root/example2.sh'
                else
                echo "example service is already running as $NPID";
                fi


                function stop
                if [ "$NPID" != "" ] ; then
                kill "$NPID";
                else
                echo "example service was not running" >&2;
                fi


                function status
                if [ "$NPID" = "" ]; then
                echo "example is not running";
                else
                echo "example is running with pid $NPID";
                fi


                if [ "$1" = "start" ]; then
                start
                elif [ "$1" = "stop" ]; then
                stop
                elif [ "$1" = "restart" ]; then
                stop;
                start;
                elif [ "$1" = "status" ]; then
                status
                else
                echo " * Usage: /etc/init.d/example start";
                fi






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 16 at 19:22









                user1133275

                2,277412




                2,277412



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463028%2fbash-script-to-log-mqtt-feed-to-txt-file%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)