sending an attachment output from sas using crontab - linux

I am trying to send several attachments from Crontab after my SAS job completes.
My sas job output several .csv files and I'd like to attach them to the email crontab sends.
How do I this?
This code
echo "files attached" | mutt -a "file.csv" -s "week 1 day 1 files"
gives me this error:
/prod/file/sas-data2/monthly_goaling_process/macros/week1_day1_process: line 24: mutt: command not found CONFIDENTIALITY NOTICE:

The error message is clear :
mutt: command not found
means that you have to install this app with your package manager, depends of your Linux flavor.
Another approach is to use sendmailcommand with uuencode (this later solution is more 'light')

Related

Linux sendmail command not sending mail while in a cron

I'm trying to run a sendmail command in a Red-hat Linux environment on a bash shell script through a cronjob. I can run this script successfully when it is ran manually and every other job within the shell runs correctly other than the mailing part.I have never used sendmail and am not sure if I need to restructure how it is being presented.
I have tried mail and mailx. I am able to send the emails but the log file contain many weird characters that it put the text format into a att00001.bin attachment on the email which I do not want. The sendmail command seems to be the only one that doesn't send an attachment when ran manually. Other cron jobs works correctly and are able to send emails they just do not have the special characters in the log file.
echo '##################################################'
date
echo '##################################################'
#Run Script and write to log file
/comp/gfb281m.sh > /usr/local/bin/oracle/getload/getload.log 2>&1
#Send log file to developer group
(echo "Subject:GetLoad Shell"; echo; cat
/usr/local/bin/oracle/getload/getload.log) | sendmail -v
exampleEmail#outlook.com exampleEmail2#mail.mil
When ran this cron job should send the contents of the getload.log file to a group a of users.
Fixed the issue thanks to another source. I was not using the sendmail's full path. I was just stating "| sendmail -v email" and not sendmails full path which was "/usr/sbin/sendmail" for me. Not sure if links are allowed here but below is where I found the answer.
https://www.unix.com/red-hat/271632-bash-sendmail-command-not-found.html
crontab sets PATH to /usr/bin:/bin. To avoid typing absolute command names like /etc/sbin/sendmail you can set up the PATH in you crontab:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/30 * * * * sendmail user#example.com%subject: Sample email%%Email body%

Command that outputs script results into a text file then emails results to personal email

I am writing a bash script that outputs server status results and want to have a way for it to put the results into a text file then email it to an email entered depending on user input. How would I go about doing this?
Thanks for the help!
to output the results to a file, use the >> or > to at the end of your command followed by a file name.
>> will append to the file. Which means the previous content of your file will remain intact.
> will remove everything and create a file with the new ouput.
for eg:
echo $serverOutput>output.txt
now to mail it :
read email // will read the email address of the user.
to mail:
uuencode output.txt output.txt|mailx -s "server output" $email
your file will be sent as an attachment.
Is this what you require?
Creating a cron job would be another task. But for your initial question about having the serverStatus inside a text file and then mail that to the user's email. Below is an example:
say we use "ps -ef|grep processname" command. replace "top" with the command you have.
ssh -q username#serverName "ps -ef|grep -i process">>output.txt
echo "enter email:"
read email
uuencode output.txt output.txt|mailx -s "serverStatusReport" $email
if you have any script/template which needs to be given this functionality, please share the code or elaborate a bit.
Regarding the cron job you would need to include the command/script in the crontab file along with the time you want it to schedule.
for eg:
echo "* * * * * /path/to/your/script.sh ">>crontab
Hope this gives you a path forward on your task

Cron output is emailed as a file instead of showing in the body?

I've setup crons through cPanel for a long time and the output is always emailed to me. However, I need a cron to run with root permissions and so I added something like this to crontab:
5 0,12,8,20 * * * php /path/to/file.php 2>&1 | mail -s "my cron job" my#email.com
The command executes and I receive an email, but the output is attached as a file ironically named noname.
How can I get the output to show up in the body of the email instead of in an attached file?
First can you confirm which mail program you are using and which cron program you are using ? E.g. by default most cron software restricts the PATH, unless you have it set at the top of the crontab.
/bin/mail at least from GNU mailutils doesn't support sending attachments without help from another program (uuencode). /bin/mailx from Heirloom which your system may be using supports attachments, and will happily convert mail sent in non interactive mode that contains illegal byte sequences (non ASCII), from text to an octet stream file attachment as per the Changelog
You can get the output to show up in body instead of attached file by
Changing which mail software you use
Removing any illegal byte sequences from your output before piping it to mail. See the existing StackOverflow Question Best way to convert text files between character sets? for how to do that.
I would highly recommend using mutt instead
5 0,12,8,20 * * * php /path/to/file.php 2>&1 |mutt -s "your cron job" -- my#email.com
or if you're really set on using whatever your default mail is, possibly use an intermediary file
5 0,12,8,20 * * * php /path/to/file.php 2>&1 /tmp/outfile; mail -s "your cron job" my#email.com < /tmp/outfile; rm -f /tmp/outfile

How to program Mutt to take an action upon new email arriving?

There is an option beep_new in the Mutt's configuration variables, which causes Mutt to issue a beep when new email arrives. Also there is <shell_esc> command, which executes a command in internal shell. Is there a way to cause Mutt to execute some user defined action (shell command) when new email arrives? Maybe <pipe-message> can help? (One can use <pipe-message> in a configuration file to call a shell command. Is there a way to do this in running Mutt?)
EDIT: as per Glenn advice, a script from Mutt can be called by configuring the custom status format in the .muttrc file:
set status_format="/some/script.sh '%r %f (%L) |"
But I can't figure out how to use this for new email detection, since the "New mail in..." notification appears on the command line, and not in the status line. And if the email arrived to a mailbox that is not the current, then the status line doesn't change at all.
EDIT 2: OK, the %b variable in the status_format did the thing.
Mutt can't do this (at least not without some workaround).
NeoMutt can: see it's new_mail_command.
You can achieve that through inotifywait as the mutt manual suggests.
Install inotify
(example for Debian based distros: apt install inotify-tools)
Watch mutt folder and run your script
(bash script example:
while : ; do inotifywait -e modify -r ~/.mutt ; /my_facy/script ; done

shell script to download remote file and launch and other script write in PHP

I made a shell script that get a remote file with ftp protocol, if the file is well downloaded, it launch another script in php with curl.It's kind of working right now but i have a few questions to improve:
Do the script is waiting the end of the download to execute the rest of the script ?Or during the time of download the script do the following instructions ?
I receive the first mail of beginning instruction but never the last ones (the one that get the result of the curl, and the one at the end of the script) how come ?
I would like to find a good way to disallow the script to be run more than once (if the archive has been downloaded) even if it's launch every hours with crontab ?
what is the difference between quit/bye/by at the end of the ftp connection ?
This is the shell script:
echo start of the script | mail -s "beginning of the script" krifur#krifur.com
cd /my_rep
HOST='domaine.com'
PORT='21'
USER='admin'
PASSWD='pass'
jour=$(date "+%Y%m%d")
FILE="file_"$jour".txt";
ftp -i -n $HOST $PORT <<EOF
quote USER $USER
quote PASS $PASSWD
cd firstlevel
cd end
get $FILE
quit
EOF
if test -f $FILE
then
CurlRes="$(curl "http://doma.com/myfile.php")"
echo debug CURL : $CurlRes | mail -s "debug" krifur#krifur.com
else
echo no file : $FILE | mail -s "no file" krifur#krifur.com
fi
echo this is the end of the script download | mail -s "end of script download" krifur#krifur.com
Do the script is waiting the end of
the download to execute the rest of
the script ?Or during the time of
download the script do the following
instructions ?
If you mean "Will the FTP command block until finished?" , the answer is yes.
I receive the first mail of beginning
instruction but never the last ones
(the one that get the result of the
curl, and the one at the end of the
script) how come ?
Take a look at your code:
then
CurlRes="$(curl "http://doma.com/myfile.php")"
echo debug CURL : $CurlRes | mail -s "debug" krifur#krifur.com
else
echo no file : $FILE | mail -s "no file" krifur#krifur.com
fi
What are the contents of $CurlRes and $FILE respectively? Try ${CurlRes} and ${FILE}. I'd also suggest quoting strings when using echo.
There is also a good chance that spam filters don't like the message, have you checked on that?
I would like to find a good way to
disallow the script to be run more
than once (if the archive has been
downloaded) even if it's launch every
hours with crontab ?
That could be done in a number of ways. Perhaps, upon success echo the file name to the bottom of something like successfully_downloaded.txt , then use grep to see if the file name is in the list. Depending on use, that file might get rather large .. so I'd also implement some means of rotating it.
From man (3) ftp:
bye Terminate the FTP session with the remote server and exit ftp.
An end of file will also terminate the session and exit.
quit A synonym for bye.
by is also a synonym for bye, as far as I know.
This should be avoided at all costs:
USER='admin'
PASSWD='pass'
Use ssh's scp with keys (no password prompt required):
Linux Journal article howto

Resources