I have a postfix server and procmail installed and working.
The problem is when I try to output the content of an e-mail to a file.
I have the following script:
/var/log/user1/fooscript.sh
#!/bin/bash
echo "Trying to get e-mail" > success.txt
echo $1 >> success.txt
/var/log/user1/.procmailrc
VERBOSE=off
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/procmail.log
INCLUDERC=$PMDIR/rc.filters
/var/log/user1/.procmail/rc.filters
:0
* ^From:(.*\<)?(test#gmail\.com)\>
| /var/log/user1/fooscript.sh
After sending an e-mail, /var/log/user1/.procmail/rc.filters
contains:
From test#gmail.com Thu Jul 18 05:08:13 2013
Folder: /var/log/user1/fooscript.sh 513
but the success file only shows:
Trying to get e-mail
(empty line)
I've chmod 777 all files and directories, so don't think its a permissions issue.
Any help would be greatly appreciated.
Your script gets the message via standard input (STDIN). Try:
#!/bin/bash
echo "Trying to get e-mail" > success.txt
# append data read from STDIN to success.txt file
cat >> success.txt
BTW for more complicated scripts use custom lock to avoid running two scripts in parallel:
:0 w :fooscript.lock
* ^From:(.*\<)?(test#gmail\.com)\>
| /var/log/user1/fooscript.sh
Related
I am attempting to add the x-header X-APP-VOLT: Yes to the header of my email with a .tar attachment. I only have access to usr/sbin/sendmail and mailx. I do not have root access so I can't download other versions of mailx or mutt.
I can add the x-header to usr/sbin/sendmail using the below code, but I can't figure out how to add the .tar attachment.
/usr/sbin/sendmail -i -- toemail << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works! END
I can attach a .tar file to mailx using the the below code, but I can't figure out how to add a x-header. My mailx also does not have the -a option.
cat file | uuencode filename | mailx -s "Test" toemail
Thank you
One way is to construct your input in a temporary file:
cat > tmpfile$$ << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works!
END
uuencode filename < file >> tmpfile$$
/usr/sbin/sendmail -i -- toemail < tmpfile$$
Also, I usually use sendmail's -t flag in this case, rather than repeating the recipient:
/usr/sbin/sendmail -i -t < tmpfile$$
If you don't want to use a temporary file, if you want to use a pure pipeline, you can use ( ) to create a subshell to do the construction:
(
echo "To: toemail"
echo "Subject: Test"
echo "X-APP-VOLT: Yes"
echo
echo "Hope this works!"
echo
uuencode filename < file
) | /usr/sbin/sendmail -i -t
(Of course, these days most recipients will probably find it easier to deal with MIME attachments rather than uuencode. It's pretty straightforward to create MIME attachments with a shell script, also.)
I was given a few example usages of mailx and asked to come up with another by my manager.
I have a program that will be left running. Once a day, we want to send the log file it generates to a list of recipients.
This was the closest example I received
00 11 * * * unset noclobber;cat /home/emailList.txt /tools/temp/summary.csv | mailx -t -s "Report `/bin/date`" -r person#company.com > /home/file.log
Seems like I want something like:
MyProgram | mailx -t -s "Report `/bin/date`" /home/emailList.txt -r person#company.com
But here, I'm not sending it once a day. This sends at MyProgram closing, right? I'm also sending MyProgram's output, not it's log file.
Thanks in advance.
I'm having a small issue, i'm running and cron task every 5 minutes which is looking text chain and replacing it with nothing..
In order to optmize something i would like to add a new function to my cronstrask : send me an email if it replaces something.. if the crontask does not find the chain no need to send a mail. I have no idea how to do that , maybe you can help me .
Here is my current cron task :
find /home -type f | xargs sed -i 's$chain if would like to era$ $g'
Thanks in advance
Here is an example that I did with help of other poeple.
It will maybe help some other poeple
#!/bin/bash
grep -r -q 'stringtoreplace' /home/ #Find the string in all files on my home
if [ $? == 0 ] #if the last result fit then
then
echo "At the following time : " $(date +%H:%M:%S) | mail -s "[Serveur Leaked] Bad iframe has been found " my#mail #we send a mail with the date
find /home -type f | xargs sed -i 's$stringtoreplace$ $g' #we replace the string with a whitespace
else
exit 1
fi
exit 0
I have a bash code to backup my iOS files and send them to my website FTP in the directory: (http://mywebsite.com/sms) but when I run this code, it isn't .zip'ing the files and leaves the file 'zippyy.db' in the root of my website, not in the /sms folder.
I will be running this script from a few devices so when I execute the code, if there is already a file in the FTP called zippyy.zip, it will change it to zippyy1.zip, zippyy2.zip etc..
I would be really grateful for somebody to re-write the script for me. Thank you in advance! Here's my code:
#!/bin/bash
ROOTFOLDER="/var/root"
ZIPNAME="zipfolder"
ZIPFOLDER=$ROOTFOLDER/$ZIPNAME
LIBFOLDER="/var/mobile/Library"
ZIPFILE="zippyy.zip"
mkdir -p $ZIPFOLDER
cp $LIBFOLDER/SMS/sms.db $ZIPFOLDER/
cp $LIBFOLDER/Notes/notes.sqlite $ZIPFOLDER/
cp $LIBFOLDER/Safari/Bookmarks.db $ZIPFOLDER/
cp $LIBFOLDER/Safari/History.plist $ZIPFOLDER/
cd $ROOTFOLDER
zip -r $ZIPFILE $ZIPNAME
HOST=HOSTNAME
USER=USERNAME
PASS=PASSWORD
ftp -inv $HOST << EOF
user $USER $PASS
cd sms
dir . remote_dir.txt
bye
EOF
FILECOUNT=$(grep zippyy remote_dir.txt | wc -l)
NEXTDB="zippyy${FILECOUNT}.db"
mv $ZIPFILE $NEXTDB
ftp -inv $HOST << EOF
user $USER $PASS
put $NEXTDB
bye
EOF
You mean your archive is corrupt once it's been ftp'd?
Its likely your sending the file in default mode on your machine, which must be ASCII mode.
But first, on you local copy of zip file, issue the test option
zip -t $ZIPFILE
If that succeeds, then change you ftp here-doc to
ftp -inv $HOST << EOF
user $USER $PASS
binary
put $NEXTDB
bye
EOF
Note the addition of the ftp command binary, which means send file without translations for ASCII.
It's highly recommended to issue the following command
man ftp
And read through it at least once. Granted there are sections of even a good ftp man page that I have failed to find useful! ;-) . Also be aware that there are many ftp clients, with only a semblance of adherence to a common set of options, parameters and sub-commands. Don't assume that once you get it working at home, that it will work at the office, or at your friends place!
IHTH
I am writing a series of cron jobs. I want each task to log its output to file, and then I want the contents of the file mailed to me at say me#somewhere.com
I think logging the output to file can be done using simple pipe redirection like this:
30 0 * * * /path/to/script1 > task1.log
30 1 * * * /path/to/script2 > task2.log
However, I am not sure how to mail the files (or simply their contents) to me in seperate emails to me#somewhere.com
Also, is there a way to dynamically create the log file names, based on the date, so that the log names would be something like %Y%m%d.task1.log ?
Where the prefix is the date ?
I am running on Ubuntu 10.0.4 LTS
If your system has a working /usr/bin/sendmail (doesn't have to be sendmail sendmail, most mail servers provide a /usr/bin/sendmail wrapper script) then you can use the mail(1) utility to send mail:
echo "hello world" | mail -s hello me#example.com
mail(1) is pretty primitive; there's no MIME file attachments, you're stuck with plaintext.
If mutt(1) is installed, you can use MIME to attach files:
echo "hello world" | mutt -a task*.log -- me#example.com
As for giving the logfiles dates:
$ echo "hi" > $(date "+%Y%m%dlog.txt")
$ cat 20110328log.txt
hi
$
So, try this:
30 1 * * * /path/to/script2 > $(date "+\%Y\%m\%dlog.txt") && mutt -a $(date "+\%Y\%m\%dlog.txt") -- me#example.com