ssmtp send date and time

B

bmandl

Guest
Hello everybody.
I recently set up my raspberry pi with raspbian which is debian in general and I am accessing it throu VNC over LAN. I found a script and modified it a little, which checks if process is running and if not, it starts it and send me an email with ssmtp, so I now, that application restarted. I added the script to cron to run every 5 minutes. It is all working, but now, I would like to also send date and time, when application restarted. I am including my text for email with < command which is writen in txt file. If I add variables in this txt file, it doesn't convert them, they are just raw text. How could I include variable in ssmtp command?
This is my script:
Code:
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
NOW=$(date +%c)

check_process() {

  echo "$ts: checking $1"

  [ "$1" = "" ]  && return 0

  [ `pgrep -n $1` ] && return 1 || return 0

}


  # timestamp

  ts=`date +%T`


  echo "$ts: begin checking..."

  check_process "myapplication"

  [ $? -eq 0 ] && echo "$ts: not running, restarting..." && `env DISPLAY=:0.0 /usr/bin/myapplication > /dev/null`&& `ssmtp mymail < /home/pi/mail.txt`
 


Hi, I took a look around the web and found this thread on setting environment variables in crontab. Is this what you were looking for?
 
Thanks, but this is not, what I am looking for. I would like to send variable value with ssmtp (date and time).
 
I don't fully understand your problem. What you are asking isn't making a lot of sense ATM!

You want to send the values of the date/time variable in your script via ssmtp/sendmail, but pass them how, and to what? What exactly are you trying to pass the date/time to??

You've said that you have managed to write the date/time to the text file that you're mailing yourself. Which will write the value of the variable as text in the text file.... What is the problem with that?? Isn't that exactly what you need?

Rather than using a fixed text file in the home dir, you could build the content of the message inside the script using a temporary file in /tmp/ for storage.
e.g. Once you have determined that your program is not running:
Code:
echo $NOW > /tmp/tempMail.txt
echo "MyProgram not running - restarted" >> /tmp/tempMail.txt
Where $NOW is the result of the date command in your original post. $(date +%c)

Then send /tmp/tempMail.txt via ssmtp. After sending the mail, you can delete the temporary file using rm.

That way you should get a message like this:
Code:
Thu, Feb 19, 2015 11:48:26 AM
MyProgram not running - restarting!

Is that what you were trying to do?? If not, you will need to explain a little more clearly, or in more detail!
 
JasKinasis, thank you for your reply. I will try this with temporary file. This is exactly, what I wanted.
PS.:
JasKinasis said:
You've said that you have managed to write the date/time to the text file that you're mailing yourself.

I never said that. In fact, I said the opposite, if you read my first post again:
bmandl said:
If I add variables in this txt file, it doesn't convert them, they are just raw text. How could I include variable in ssmtp command?

But never mind, you gave me the answer in your second half of your post. Thank you again :)
 
Ah, OK!
I see where I went wrong. I misunderstood this part of your message:
If I add variables in this txt file, it doesn't convert them, they are just raw text.

When you said that, I thought you meant that you'd managed to write the values of the variables to the file and that they were being written as text, which wasn't what you wanted (and I assumed that you'd removed whatever code you'd used to write the variables out from the script because it didn't do what you wanted). If that makes any sense, ha ha! It did confuse me a bit!

So I'm guessing what you actually meant in that bit of your post was that you had put place-holders for the variables in the text file, but were unsure how to populate them before passing the file to ssmtp. In which case, my suggestion of building the text file from inside the script is exactly what you need. It was the only thing I could think of that made any sense to do.

The only other option would be to load the external text file into your script, parse it to find the placeholders and then substitute the placeholder text for the actual values you want to use and then send the modified version of the text via ssmtp, but that would just be waaaaaay too complicated. Far simpler to just build the message text inside the script! :)

Glad I was of some help!
 

Members online


Top