0

I have bash script that runs daily on customer's Ubuntu 20.04 server. This simple script rsyncs a bunch of files from one location to the another. While copying, it creates a log file with basic info: start time, end time, number of copied files. After backup ends, it uses ssmtp to send that report to my email. The script looks like this:

#!/bin/bash

DAN=monday

rm -R /backup/$DAN/* touch /backup/$DAN/backup.log

DATE=date +%d.%m.%Y. TIME=date +%R

echo Subject: Backup report for date $DATE >> /backup/$DAN/backup.log echo Content-Type: text/html; charset="utf8" >> /backup/$DAN/backup.log echo echo >> /backup/$DAN/backup.log echo "Backup for date $DATE" >> /backup/$DAN/backup.log echo >> /backup/$DAN/backup.log echo "Starting daily at 22.00h" >> /backup/$DAN/backup.log echo >> /backup/$DAN/bekap.log echo "Starting time is $TIME" >> /backup/$DAN/backup.log

rsync -avh --progress -h /mnt/share/ /backup/$DAN

SIZE=du -sh /backup/$DAN/ FILES=tree -f /backup/$DAN/ | wc -l KILLTIME=date +%R

echo >> /backup/$DAN/backup.log echo "Ending time is $KILLTIME" >> /backup/$DAN/backup.log echo >> /backup/$DAN/backup.log echo echo "Total copied size is $SIZE" >> /backup/$DAN/backup.log echo >> /backup/$DAN/backup.log echo echo "Copied $FILES files" >> /backup/$DAN/backup.log echo >> /backup/$DAN/backup.log

ssmtp -v -F "Backup report" me@mydomain.com < /backup/$DAN/backup.log

Now, if I run the script manually, logged in with sudo, it works fine and I receive my report as expected. But if I crontab execute the same script, it replaces sender and receiver and mail is not delivered!

Manually executed script create log entry like this:

Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: Set MailHub="smtp.gmail.com"
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: via SMTP Port Number="465"
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: Creating SSL connection to host
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 220 smtp.gmail.com ESMTP 
cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: EHLO host.domain.com 
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 250 SMTPUTF8
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: AUTH LOGIN
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 334 VXNlcm5hbWU6
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: b2JhdmVzdGVuamFAa29udHJvbHNpc3RlbS5ycw==
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 334 UGFzc3dvcmQ6
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: ZGZ3dW1veHdvbGZ0c3B1eA==
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 235 2.7.0 Accepted
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: MAIL FROM:<recipient@domain.com>
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 250 2.1.0 OK 
cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: RCPT TO:<me@mydomain.com>
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: 250 2.1.5 OK cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp
Oct 18 08:49:47 BKP-ASUS sSMTP[265073]: DATA
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: 354  Go ahead cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Received: by host.domain.com (sSMTP sendmail emulation); Wed, 18 Oct 2023 08:49:47 +0200
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: From: "Backup report" <recipient@domain.com>
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Date: Wed, 18 Oct 2023 08:49:47 +0200
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Subject: Backup report for date 18.10.2023.
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Date 18.10.2023.
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Starting daily at 22.00h
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Starting time is 08:43
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Ending time is je 08:49
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Total copied size is 736M#011/backup/test/
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]: Copied 412 files
Oct 18 08:49:48 BKP-ASUS sSMTP[265073]:
Oct 18 08:49:49 BKP-ASUS sSMTP[265073]: .
Oct 18 08:49:49 BKP-ASUS sSMTP[265073]: 250 2.0.0 OK  1697611789 cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp
Oct 18 08:49:49 BKP-ASUS sSMTP[265073]: QUIT
Oct 18 08:49:49 BKP-ASUS sSMTP[265073]: 221 2.0.0 closing connection 
cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp
Oct 18 08:49:49 BKP-ASUS sSMTP[265073]: Sent mail for recipient@domain.com (221 2.0.0 closing connection cz16-20020a0564021cb000b00535204ffdb4sm2275216edb.72 - gsmtp) uid=0 username=r>

But if I cron execute the script, this is what I found in log: (shortened to important part I hope):

Oct 18 04:15:26 BKP-ASUS sSMTP[264166]: 235 2.7.0 Accepted
Oct 18 04:15:26 BKP-ASUS sSMTP[264166]: MAIL FROM:<root@domain.com>
Oct 18 04:15:26 BKP-ASUS sSMTP[264166]: 250 2.1.0 OK g5-20020a1709067c4500b009ad8084e08asm690857ejp.0 - gsmtp
Oct 18 04:15:26 BKP-ASUS sSMTP[264166]: RCPT TO:<recipient@domain.com>
Oct 18 04:15:26 BKP-ASUS sSMTP[264166]: 250 2.1.5 OK g5-20020a1709067c4500b009ad8084e08asm690857ejp.0 - gsmtp
Oct 18 04:15:26 BKP-ASUS sSMTP[264166]: DATA

Any idea why it acts differently with cron?

FedKad
  • 10,515
Marxer
  • 9

1 Answers1

0

In the end, it wasn't as it looks like ... it was not an sSMTP question, it was bash problem. I replaced "#!/bin/bash" with "#!/usr/bin/env bash" and added another line with PATH and now it works as expected! Thanks to everyone who sent the ideas for help.

Marxer
  • 9