Bash Script: To log MQTT feed to txt file

Clash 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,
shell-script logs mqtt
add a comment |Â
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,
shell-script logs mqtt
add a comment |Â
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,
shell-script logs mqtt
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
shell-script logs mqtt
edited Aug 16 at 19:13
Jesse_b
10.5k22659
10.5k22659
asked Aug 16 at 17:37
Usman Asghar
1
1
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Aug 16 at 19:22
user1133275
2,277412
2,277412
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password