I want to get an email alert if a certain folder is modified but how do I pipe the output from the command so it sends an email not just show the changes to the folder in the terminal?
something like the following but... gives an error on the email part
inotifywait -m /home/tom -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
| /usr/bin/Mail -s "notify" "email#12345mail.com"
done
Could it be you simply missed the semicolon before done?
This line works for me (note I also used mutt instead of Mail):
inotifywait -m /home/tom -e create -e moved_to | while read path action file; do echo "The file '$file' appeared in directory '$path' via '$action'" | /usr/bin/mutt -s "notify" "email#12345.com" ;done
Related
I am trying to move all the *.csv files to another folder on server but every time i get access failed error , I am able to get all the files to local server using mget but mv fails everytime , i can see the file on the server and got full permissions on the files, sh script is not working with wild characters. struck here with the simple command .
Download to local directory
localDir="/home/toor/UCDownloads/"
[ ! -d $localDir ] && mkdir -p $localDir
#sftp in the file directory to be downloaded
remoteDir="/share/CACHEDEV1_DATA/Lanein1/Unicard/"
#The file to be downloaded is fileName
lftp -u ${sftp_user},${password} sftp://${host}:${port}<<EOF
PS4='$LINENO: '
set xfer:log true
set xfer:log-file "$logfileUCARC"
set xfer:clobber true
set xfer:auto-rename true
debug 9
cd ${remoteDir}
lcd ${localDir}
#mget *.CSV
ls -l
mv "/share/CACHEDEV1_DATA/Lanein1/Unicard/"*.csv "/share/CACHEDEV1_DATA/Lanein1/Unicard/Archives/"
#rm /share/CACHEDEV1_DATA/Lanein1/Unicard/!(*.pdf)
bye
EOF
This is not a shell or Bash problem. It is a LFTP problem.
From the manual of LFTP:
mv file1 file2
Rename file1 to file2. No wildcard expansion is performed.
LFTP just does not support what you asking for. It will treat *.csv as a part of the file name.
See here for an alternative.
I want to echo text as well as tail the last part of a log to pipe it to email. I can easily do one or the other, but how do I do both without first writing everything to a file and then emailing that? I basically want to combine the below two commands into one to only send out one email.
echo "There is an error in the log, see the below for detail" | mail -s "error in the log" xxx#yyy.zzz
and
tail -n 10 /var/log/error.log | mail -s "error in the log" xxx#yyy.zzz
Thanks.
You could use the concatenate command along with process substitution:
cat <(echo "There is an error in the log, see the below for detail") <(tail -n 10 /var/log/error.log) | mail -s "error in the log" xxx#yyy.zzz
I have a file which I want to save it some other server say abc.com.
In case this operation fails because of some reason like (permission denied or anything else), currently it fails out silently.
But I want , in such situation it shoudl send an email to Ruhi#.abc.com.
How can I implement this and test the scenario.
Excerpt of the code:
sed -e abc.txt > /net/abc.com/test/abc.txt
#Sending email
if [ -f /net/abc.com/test/abc.txt ]; then
echo "content of abc.txt saved to server" | mail -s "Content of abc" dave#klm.com
else
echo "File does not exist"
fi
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 want to tail log file with grep and sent it via mail
like:
tail -f /var/log/foo.log | grep error | mail -s subject name#example.com
how can i do this?
You want to send an email when emailing errors occur? That might fail ;)
You can however try something like this:
tail -f $log |
grep --line-buffered error |
while read line
do
echo "$line" | mail -s subject "$email"
done
Which for every line in the grep output sends an email.
Run above shell script with
nohup ./monitor.sh &
so it will keep running in the background.
I'll have a go at this. Perhaps I'll learn something if my icky bash code gets scrutinised. There is a chance there are already a gazillion solutions to do this, but I am not going to find out, as I am sure you have trawled the depths and widths of the cyberocean. It sounds like what you want can be separated into two bits: 1) at regular intervals obtain the 'latest tail' of the file, 2) if the latest tail actually exists, send it by e-mail. For the regular intervals in 1), use cron. For obtaining the latest tail in 2), you'll have to keep track of the file size. The bash script below does that - it's a solution to 2) that can be invoked by cron. It uses the cached file size to compute the chunk of the file it needs to mail. Note that for a file myfile another file .offset.myfile is created. Also, the script does not allow path components in the file name. Rewrite, or fix it in the invocation [e.g. (cd /foo/bar && segtail.sh zut), assuming it is called segtail.sh ].
#!/usr/local/bin/bash
file=$1
size=0
offset=0
if [[ $file =~ / ]]; then
echo "$0 does not accept path components in the file name" 2>&1
exit 1
fi
if [[ -e .offset.$file ]]; then
offset=$(<".offset.$file")
fi
if [[ -e $file ]]; then
size=$(stat -c "%s" "$file") # this assumes GNU stat, possibly present as gstat. CHECK!
# (gstat can also be Ganglias Status tool - careful).
fi
if (( $size < $offset )); then # file might have been reduced in size
echo "reset offset to zero" 2>&1
offset=0
fi
echo $size > ".offset.$file"
if [[ -e $file && $size -gt $offset ]]; then
tail -c +$(($offset+1)) "$file" | head -c $(($size - $offset)) | mail -s "tail $file" foo#bar
fi
How about:
mail -s "catalina.out errors" blah#myaddress.com < grep ERROR catalina.out