Linux sendmail command not sending mail while in a cron - linux

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%

Related

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

Bash crontab doesn't output products of shell script file

Hi I'm a first year game programing student learning Unix Bash, I have run into a problem trying to understand crontab. I'm trying to do some rather simple things, checking to see if I am online, getting information about a given website, and ping another website to verify it is online. My script file does all of this without fail, however when I try to perform these tasks through crontab I get emails telling me absolutely nothing but jibberish. The output basically just tells me that I am trying to do all these things, but it doesn't output the results. I'm not sure where I am going wrong.
Just to verify I do have permission on the system to use crontab, and I have the script running every minute while I am trying to get it working. I'm hoping someone can point me in the right direction, all of my research online has really just led me astray.
This is my crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
LOGNAME=username
* * * * * /bin/bash /home/students/~/online.sh
30 23 1 * * rm online.log
this is my script
touch online.log
who | grep username >> online.log ; whois yahoo.ca >> online.log ; ping -c 1 www.google.com >> online.log
You need to use absolute paths in your scripts if you want to execute those using cron.
Note that cron executes in a different environment from what you get while executing a script on the command line. For example, changes lines like
touch online.log
to include the absolute path to online.log.
The output is being redirected into online.log, so you need to look there, not in your emails. If you want the output to be in the emails as well, you should look into using tee instead of a redirection.

Cron job from cpanel.. no success

I tried a lot of methods but ended up with nothing in hand..My simple target is to reset a variable to zero at the end of the day.
i checked the location of php as "which php" and "whereis php"
which resulted into /usr/bin/php
here are some of things i tried..
/usr/local/bin/php -f /home/USERNAME/public_html/developer3/crons/filename.php
/usr/bin/php -f /home/USERNAME/public_html/developer3/crons/filename.php
php -q /home/USERNAME/public_html/developer3/crons/filaname.php
/usr/bin/wget -O http://subdomain.sitename.com/crons/filename.php
for quick results, i kept the timming as every minute to execute the code. i could successfully execute the code as
http://subdomain.sitename.com/crons/filename.php
please guide me.
If the path to php on your system is /usr/bin/php then the command you should be running with cron should be
/usr/bin/php /full/path/to/php/script.php
Is the script you're running designed to be invoked from the php-cli in that way? If it isn't and works correctly when you use a browser then you can use curl, if it's installed on your server
curl -A cron-job http://subdomain.sitename.com/crons/filename.php
It would likely be helpful in any event to configure the MAILTO variable (since you're using cPanel, it's just a text box on the appropriate page that you can fill in) so that you get an email with the output from your cron jobs. Having the output emailed to you will help you diagnose what's causing your script to not have the desired effect when it runs.

Resources