Sending content of cron with cron via email - email

is there a simple way to send the content of the cron as an email by cron? Like: I would to receive a "backup" of my crontabs every month via email.
Thnx for your kind help,
Dirk.

You can use this for that problem
0 0 1 * * crontab -l | mail -s "Backup Cron" youemail#mail.com
and try on https://crontab.guru/every-month if you make cron schedule

Ah thnx. Works great.
In case anyone else is looking, I also found another option:
mail -s "Cron-Backup" name#domain.de

Related

Telegram CLI unread messages

I want to display unread messages with telegram cli running on my raspberry pi, and save them to some file which will be processed later with php, and displayed on a small TFT screen.
I think it may be possible with get_dialog_list () but I can't find a way to make it work.
Any help/advice is welcome! :-)
If you choose php as a laguage to write your customized client or just pull messages from Telegram, you need to connect to the tg client and then send or receive messages.
You can easily wirte a Bash script and use it inside your php code.
#!/bin/bash
now=$(date)
from=$1
subject=$2
body=$3
tgpath=/home/telpath/tg
LOGFILE="/home/logpath/tglog.log"
cd ${tgpath}
${tgpath}/telegram -k ${tgpath}/tg-server.pub -W <<EOF
msg $to $subject
safe_quit
EOF
echo "$now Recipient=$from " >> ${LOGFILE}
echo "Finished" >> ${LOGFILE}
So we will have:
<?php
while (TRUE) {
$output = shell_exec('tg.sh', '#user');
echo "<pre>$output</pre>";
}
?>
You can easily iterate over your bash script (tg.sh) to pull messages from any contact.
Note that these codes are trivial like a sudo code and needs more development.

Combining grep and sendmail

I am looking for a way using the command line in UNIX to email the results from a grep command.
I am grepping the error logs looking for a "searchword". I temporarily want to email the results to my work account. This is a temporary solution until the SA has the time to write a script that will write it to a file where the file will be read by an automated analyzing program.
sendmail joetester#workemail.com < grep searchword error*
Does anyone have an idea on how to do it that they can share. Thank you.
You want something like grep searchword error* | sendmail joetester#workemail.com, see this question.

fetchmail/procmailrc filter by date

I have fetchmail grab my email from a pop account and send it to procmail. I have 'keep' set in my fetchmailrc file because i also use the email for outlook and i cant have fetchmail taking all my emails.
My problem is i want to download just todays emails or at lease since last time fetchmail ran.
right now i am trying to filter by date in the procmail file but it is not working.
I am using fedora14
contents of .procmailrc
SHELL=/bin/bash
DATEZ="date +'%a, %d %m %Y'"
:0
*^From.*\<(blah#blah\.com|blah2#blah2\.ca)\>
*^Content-Type:*
*^ name.*\.(xls|doc)
*$ ^Date:.*$DATEZ
{
:0fw
| uudeview -p $HOME/Inbound/Received -
:0
| $HOME/Inbound/Start.bash 2> /dev/null
}
Well it pays to read. In the fetchmail manual it explains in detail to use the -U flag. This then only downloads the emails from when you last polled the server.
so the command is:
fetchmail -k -U

How to test crontab entry?

I have an entry in my crontab that looks like this:
0 3 * * * pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz
That script works perfectly when I execute it from the shell, but it doesn't seem to be running every night. I'm assuming there's something wrong with the permissions, maybe crontab is running under a different user or something. How can I debug this? I'm a shared hosting environment (WebFaction).
You need to escape "%" characters in crontab entries with backslashes- see the crontab(5) manpage. I've had exactly the same problem.
For example:
0 7 * * * mysqldump usblog | bzip2 -c > usblog.$(date --utc +\%Y-\%m-\%dT\%H-\%M-\%SZ).sql.bz2
Do you not get emails of cron errors? Not even if you put "MAILTO=you#example.com" in the crontab?
You may also need to set PATH in your crontab if pg_dump or gzip isn't on the system default path (so use "type pg_dump" to check where they are, crontab usually only runs commands in /bin or /usr/bin by default)
Always use full paths in crontab entrties. For example, /usr/bin/gzip. You will also need to do that for pg_dump and date.
When you say it doesn't work, what do you mean? Does it not generate the file at all or is it empty?
If your system is set up correctly, crontab should send you an email if your command generated any output.
Try something like this to verify crontab is running. It will touch the file every minute.
* * * * * touch /tmp/foo
And check your paths like James mentioned.
If this is in something like /etc/crontab, make sure the user is included:
0 3 * * * <user_goes_here> pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

Cron send email with STDERR but NOT STDOUT?

I have some python scripts that run on a daily basis in cron. How can I have cron send me an email ONLY WHEN THERE IS STDERR OUTPUT from my script? I want to be able to mail multiple recipients, and set the subject line individually for each cron entry.
I tried this:
./prog > /dev/null | mail . . .
but it didn't work -- I still receive blank emails when there is no STDERR. Do I need to do this in the script itself?
Sorry if this seems basic, I have googled a lot but can't seem to find this answered succintly.
For cron you don't need to pipe through mail. The cron daemon will automatically mail any output of your command to you. Your crontab entry should look like:
# every minute
* * * * * ./prog >/dev/null
If there is no STDERR output, you won't get any mail.
You are asking incorrect question. When you are using mail(1) to send the email, it is no longer relevant that its in cron. What you actually need is to pipe stderr to stdin of mail. Normal pipe is from stdout to stdin, so simplest way to solve this is redirect:
{ /prog > /dev/null ; } 2>&1 | mail ...
Or in the less-clear way because of confusing order of redirectings:
/prog 2>&1 > /dev/null | mail ...
mail v1.6 has an option to not send messages with an empty body:
-E Do not send messages with an empty body.
This is useful for piping errors from cron(8) scripts.
This might be what you are looking for.
The -s file test will tell you if a file exists and has size greater than zero.
./prog >/dev/null 2>some/file ; if [ -s some/file ] ; then mail < some/file ; fi
There is a nice tool called cronic that does this. It is part of the moreutils package.
If your SCRIPT has commands that may produce STDERR that you want to be notified on, then you need to use a mail or mailx call within the script itself (if then else or ). The cron job STDOUT and STDERR redirects are ONLY for cron job EXECUTION STDOUT and STDERR. hkmaly had it right on the n