1

I am still a bottle feeding linux newbie, so go easy. I've googled myself this far but can't find the answer.

I'm running a program that outputs csv data, and piping it to log.csv

I'm looking for a way to have the piped output contain the date/time it was created.

Right now the command looks like this (hand key'd every morning, no crontab yet until I get this working)

rtlamr -format=csv -msgtype=scm | tee /home/jonboy545/energy/logs/3-16-18_7_30.csv

I'm using | tee so I can still see the output on the terminal as well as log the file.

So, how do I pipe the output to a dynamic filename? I have a simple bash script that will output text but how do I tell rtlamr to | a bash script to generate a filename?

Here's the script called "log_energy.sh" to create the text file name:

#!/bin/bash
today=`date '+%m_%d__%H_%M_%S'`;
filename="/home/jonboy545/energy/logs/$today.csv"
echo $filename;

If you run ./log_energy.shthen it just outputs the text like you'd expect:

/home/jonboy545/energy/logs/03_16__17_30_39.csv

So my stupid thinking is something like this (which obviously doesn't work)

rtlamr -format=csv -msgtype=scm | tee /home/jonboy545/./log_energy.sh

Now that I think about it, it may be easier to just create a script that does all that. So something like this?

#!/bin/bash
today=`date '+%m_%d__%H_%M_%S'`;
filename="/home/jonboy545/energy/logs/$today.txt"
/usr/bin/rtlamr -format=csv -msgtype=scm | tee $filename;

Thinking I may have just answered my own post but I'm sure that script above has got something wrong with it.

  • You could make your question so much simpler if you used a simpler command than rtlamr. Like use a command people are familiar with that is installed natively, for your question. – barlop Mar 16 '18 at 22:10
  • try $(`./script`) Or, the kind of thing you could want to do is run a command and get the output of that command in a variable, ten use that variable so a=$(`./abc.sh`) Then use $a – barlop Mar 16 '18 at 22:14
  • So it isn't clear where things stand and what your question currently is. If you're not sure if your solution works, test it. If it doesn't work, focus the question on that problem. If it does work, move your solution to an answer. – fixer1234 Mar 16 '18 at 22:16
  • @fixer1234 his question is clear to me. If it's not clear to you then you should ask him what it is you don't understand otherwise it's absurd. – barlop Mar 16 '18 at 22:17
  • | tee /home/jonboy545/$a And then make sure $a has the filename you want. By doing $a=$(`abc.sh`) – barlop Mar 16 '18 at 22:18
0

You're using bash, so you can use process & command substitution.

From the title I thought you might want to add the date to the start of the logfile's output, which would use something like this:

$ program |while read pong; do echo $pong; echo $(date):\ $pong >> logfile; done

Or adding the date to the filename would redirect to >> "$(date)-logfile"

But it looks like you just want to use the filename with tee:

rtlamr -format=csv -msgtype=scm | tee "$(date '+%m_%d__%H_%M_%S')"

If you're running as your user, then the file should be just like any other file created by your user.

  • Ah, I like the second option, but ultimately having a bash script worked better, as the bash script allows me to throw in a couple extra features commands in there. Thank you though as I didn't know you could put "dynamic" data into a | tee output! I'll select this as best answer because it does directly answer the question. – poor_red_neck Mar 19 '18 at 18:34
  • Welcome & thanks :) Bash's large man page goes over a lot of it's features briefly, but searching for a specific problem to solve is good too. Quoting and the order of things can get a little hairy, especially with funny characters like spaces, newlines, nulls... – Xen2050 Mar 20 '18 at 13:50
0

So I guess I did answer my own question, or rather I got it to work... had the program location wrong but that's just semantics.

log_energy.sh:

#!/bin/bash
today=`date '+%m_%d__%H_%M_%S'`;
filename="/home/jonboy545/energy/logs/$today.txt"
/home/jonboy545/GoCode/bin/rtlamr -format=plain -msgtype=scm | tee $filename;

That produces the expected results. If I run ./log_energy.sh I get the terminal output on the screen, and the filename is created with the date.

Off to learn about crontabs now! Specifically how to have a script expire at a certain time, and then start again. More googling!!!!

Thanks for the help anyways, MAYBE somebody will come across this since I swear I googled the sh*t out of this and couldn't find an answer but now I see I was thinking about it all wrong.

  • I now have an issue with this "solution." When I go to view the csv files they are all "read/write protected" and says they are owned by root. Are scripts run under crontab executed as root? – poor_red_neck Mar 16 '18 at 23:39
  • well you can always change these things, eg making csvs chmod 666(so everybody can read/write), but does this help? askubuntu.com/questions/419548/… – barlop Mar 16 '18 at 23:56
  • Yes, but mainly because it made me see how ignorant I really am. Note to self: if you run crontab as sudo... it's going to edit root's crontab. Duh. Just edit crontab as the user... problem solved. Did crontab -u jonboy545 -e and of course that was the proper crontab to edit in the first place... – poor_red_neck Mar 17 '18 at 0:21
  • Doesn't systemd replicate a lot of what cron can do? – Xen2050 Mar 17 '18 at 4:38
  • Maybe, but had no idea what that was until I saw your comment. I just have the crontab run at midnight every day, and it executes a script. The script does a pkill of the current running program incase it was executed later in the day from a crash or something, and then executes the rtlamr program for a duration of 23h59m55s. By the time the crontab runs again there shouldn't be any instance of pkill to terminate but its there just incase. Then it does it all over again. So far so good! – poor_red_neck Mar 21 '18 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.