Unable to get output in mail body after running cron

I have prepared script to get the file details everyday and send mail. while running bash script manually , it sends output in body . but does not send through cron.
cron sends only subject . I believe have to use some wait or sleep syntex. to complete first . please help.

#!/bin/bash
ls -altrh /syslog --time-style=+%D | grep $(date +%D)  |awk '{print $7'} >output.txt
echo -e "SYSLOG Status on Date- `date '+%A %d-%B, %Y'`">output1.txt
echo "------------------------------------------------------------------------------------------------">>output1.txt
echo -e "HostName \t\t\t Status   ">>output1.txt
echo "------------------------------------------------------------------------------------------------">>output1.txt
for hosts in `cat hosts.txt`;
do
output=$(grep -i -w  $hosts output.txt)
if [ $? == 0 ] ;
then
echo -e "$hosts \t\t Log Received">>output1.txt
else
echo -e "$hosts \t\t Log Not-Received">>output1.txt
sleep 5
fi
done
mailx -s "Syslog-Status------Date-`date '+%A %d-%B, %Y'`" abc@abc.com<output1.txt

Have you had a look at cron logs?
What are in them?

getting result by cron .........it does not paste the content.

From: root [mailto:root@abc.com]
Sent: den 12 februari 2020 09:56
To: abc@abc.com
Subject: Syslog-Status------Date-Wednesday 12-February, 2020

SYSLOG Status on Date- Wednesday 12-February, 2020
------------------------------------------------------------------------------------------------
HostName                         Status
------------------------------------------------------------------------------------------------

Also, it is important to use the full paths in cron scripts (especially crontab scripts run as root, but for any user). This is true for both executables and input / output files.

Please do not use relative paths.

As vbe asked, what are the messages on this process in your log files?

cront is running. and getting mail also. but does not output content in mail body.

Again, it important to use the full paths in cron scripts (especially crontab scripts run as root, but for any user). This is true for both executables and input / output files.

Please do not use relative paths.

Rewrite your scripts with full path names and make sure your cron userid has permissions to write / read from those directories.

Neo and vbe thanks for reply.

cron is running from root user. below is the syntax. and the script is giving correct output if runs manual.

*/2 * * * * /bin/sh /usr/local/scripts/syslog-check-linux.sh

Did you read this, which I wrote twice??

What I understand from the script, cron does run below part(bold part). cron has having problem to run for loop. that have to check.

#!/bin/bash
ls -altrh /syslog --time-style=+%D | grep $(date +%D)  |awk '{print $7'} >output.txt
echo -e "SYSLOG Status on Date- `date '+%A %d-%B, %Y'`">output1.txt
echo "------------------------------------------------------------------------------------------------">>output1.txt
echo -e "HostName \t\t\t Status   ">>output1.txt
echo "------------------------------------------------------------------------------------------------">>output1.txt
for hosts in `cat hosts.txt`;
do
output=$(grep -i -w  $hosts output.txt)
if [ $? == 0 ] ;
then
echo -e "$hosts \t\t Log Received">>output1.txt
else
echo -e "$hosts \t\t Log Not-Received">>output1.txt
sleep 5
fi
done
mailx -s "Syslog-Status------Date-`date '+%A %d-%B, %Y'`" abc@abc.com<output1.txt
```[/b]


--- Post updated at 03:50 PM ---

Hi Neo, 
sorry  unable to understand.
you mean to provide full path,  like bold?

```text
for hosts in `cat /usr/local/scripts/hosts.txt`;
do
output=$(grep -i -w  $hosts /usr/local/scripts/output.txt)
if [ $? == 0 ] ;
then
echo -e "$hosts \t\t Log Received">>/usr/local/scripts/output1.txt
else
echo -e "$hosts \t\t Log Not-Received">>/usr/local/scripts/output1.txt
sleep 5
fi

--- Post updated at 03:50 PM ---

Hi Neo,
sorry unable to understand.
you mean to provide full path, like bold?

for hosts in `cat /usr/local/scripts/hosts.txt`;
do
output=$(grep -i -w $hosts /usr/local/scripts/output.txt)
if [ $? == 0 ] ;
then
echo -e "$hosts \t\t Log Received">>/usr/local/scripts/output1.txt
else
echo -e "$hosts \t\t Log Not-Received">>/usr/local/scripts/output1.txt
sleep 5
fi

--- Post updated at 03:53 PM ---

Hi Neo,
sorry unable to understand.
you mean to provide full path, like bold?

for hosts in `cat /usr/local/scripts/hosts.txt`;
do
output=$(grep -i -w $hosts /usr/local/scripts/output.txt)
if [ $? == 0 ] ;
then
echo -e "$hosts \t\t Log Received">>/usr/local/scripts/output1.txt
else
echo -e "$hosts \t\t Log Not-Received">>/usr/local/scripts/output1.txt
sleep 5
fi

--- Post updated at 03:56 PM ---

Wonderful Neo. thanks for suggestion. issue is resolved. now able to get mail.

Thank you so much sir. how to makr this case as resolve?

--- Post updated at 03:59 PM ---

Wonderful Neo. thanks for suggestion. issue is resolved. now able to get mail.

Thank you so much sir. how to make this case as resolve?

Good news,

So, what did you learn?

Maybe.....

Always use full paths in these kinds of script :slight_smile: .

1 Like

Wounderfull Neo. thanks for suggestion. issue is resolved. now able to get mail.

Thank you so much sir. how to makr this case as resolve?