Hello All I have scheduled a cronjob with shell script for Cloud backup and cre . . .

Kalesha Gagguturu:
Hello All

I have scheduled a cronjob with shell script for Cloud backup and creating a log file in /var/log/backup with time stamp. After completing the backup that log file redirecting as input to the cronjob for sending the log file to an email for backup communication.

0 5 * * 0-5 /bin/bash /root/dailycldbk.sh > /var/log/backup/dailybk.$(date +%Y-%m-%d).log 2>&1; mailx -s “subject” “mailto:[email protected]|emailid” < /var/log/backup/dailybk.$(date +%Y-%m-%d).log

But this log file is huge, so emails are not getting delivered to email provided as mail size restrictions. Is there any way we can just send the last 3 lines of that log file as it contains whether the job completed successfully or failed. So we will be aware of the job instead of contents?

Sagar Utekar:
@Kalesha Gagguturu you can tail logs upto last 3 lines before forwarding it to email.

Sagar Utekar:

watch tail -n 3 log_file.log

Kalesha Gagguturu:
you mean
0 5 * * 0-5 /bin/bash /root/dailycldbk.sh > /var/log/backup/dailybk.$(date +%Y-%m-%d).log 2>&1; mailx -s “subject” “mailto:[email protected]|emailid” < “tail -n 3 /var/log/backup/dailybk.$(date +%Y-%m-%d).log”

Sagar Utekar:
or simply,

tail -n 3 mylogfile.txt

Kalesha Gagguturu:
ya this I am aware, I wanted to confirm is it tail output will redirect as input with below format?

0 5 * * 0-5 /bin/bash /root/dailycldbk.sh &gt; /var/log/backup/dailybk.$(date +\%Y-\%m-\%d).log 2&gt;&amp;1; mailx -s "subject" "emailid" &lt; "tail -n 3 /var/log/backup/dailybk.$(date +\%Y-\%m-\%d).log"

Sagar Utekar:
please test it once.

Kalesha Gagguturu:
No its not working Sagar, getting No such file or directory error

-bash: tail -n 3 /var/log/backup/dailybk.2022-10-24.log: No such file or directory

Kalesha Gagguturu:
But individual command works fine

tail -n 3 /var/log/backup/dailybk.2022-10-25.log
Total: 0 B, Transferred: 558.19 MiB, Speed: 2.26 MiB/s

Info: Completed /root/dailycldbk.sh at Tue Oct 25 05:04:09 BST 2022

Sagar Utekar:
can you try with,

0 5 * * 0-5 /bin/bash /root/dailycldbk.sh &gt; /var/log/backup/dailybk.$(date +\%Y-\%m-\%d).log 2&gt;&amp;1; tail -n 3 /var/log/backup/dailybk.$(date +\%Y-\%m-\%d).log &gt; tailed_logs.log; mailx -s "subject" "emailid" &lt; tailed_logs.log

Kalesha Gagguturu:
Thank you Sagar
I think it may work, even though its long command for schedule
I will try with crontab itself

Kalesha Gagguturu:
@Sagar Utekar
Thanks for your tips it’s working fine. Is there a way we can make simple command for this job?

Sagar Utekar:
run all commands from shell script and use that shell script in cron entry.

Kalesha Gagguturu:
Ya but we are already running script file for backup and redirecting that output only to log

Kalesha Gagguturu:
So after this we need to create another script?

Kalesha Gagguturu:
Can you give me some tips about that?

Sagar Utekar:
Correct. But this is simple command, not sure how you can optimise it more.

Kalesha Gagguturu:
I think we have to keep tail and mail commands in a shell script file and add it corn job. Mostly that’s all we can do right?

0 5 * * 0-5 /bin/bash /root/dailycldbk.sh &gt; /var/log/backup/dailybk.$(date +\%Y-\%m-\%d).log 2&gt;&amp;1; /bin/bash /root/email_alert.sh

cat /root/email_alert.sh
tail -n 3 /var/log/backup/dailybk.$(date +\%Y-\%m-\%d).log &gt; tailed_logs.log
mailx -s "subject" "emailid" &lt; tailed_logs.log

Sagar Utekar:
yes this may work, test it once.