DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 4 Weeks Ago
stanl stanl is offline
Real Name: Stan
Port Guard
 
Join Date: Jun 2019
Posts: 10
Default Cron and email

I have the following line in root's crontab:
Code:
00 06 * * *  /root.backup.sh -m
which runs this very basic script:
Code:
#!/bin/sh
mount /dev/sd1a /mnt/
/usr/local/bin/rsync -avh --exclude-from=/home/stan/rsync.exclude /home/stan /mnt/
/usr/local/bin/rsync -avh /etc /mnt/
/usr/local/bin/rsync -avh /var //mnt/
cd /home/stan/
umount /mnt/
sleep 5
shutdown -p now
If I run the script as is, the backup occurs but i get no email confirmation.
If I remove the last 2 lines, the backup occurs and I get email confirmation.

What I'd like to know is why this occurs and how can I run the script as is and receive confirmation?

Any help would be appreciated.
Thanks
Reply With Quote
  #2   (View Single Post)  
Old 4 Weeks Ago
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 876
Default

The mail confirmation sends the output of the command run by crontab (which in this case is the /root.backup.sh script). It would seem then that the mail cannot be sent until the command has completed. Since your script ends with a shutdown, it would seem that the mail sending operation is waiting until shutdown completes, at which point it's too late for the computer to do anything ... ergo, no mail. At least, that's my theory.

What to do about it? What comes to my mind is to end your script by running another script in the background. That second script could contain the sleep and shutdown now residing in your first script. If the mail is sent when the new first script (sans shutdown) completes, but the second one is not complete, then you may get mail of the first script's output. Then the shutdown may take place a little later if the sleep is long enough. Seems worth a try.

Also: the -m flag in your crontab doesn't seem to do anything?
Reply With Quote
  #3   (View Single Post)  
Old 4 Weeks Ago
stanl stanl is offline
Real Name: Stan
Port Guard
 
Join Date: Jun 2019
Posts: 10
Default Cron and email

Your theory appears to be correct. I tried your suggestion and things are working fine now.

Thanks for your assistance.
Reply With Quote
  #4   (View Single Post)  
Old 4 Weeks Ago
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 876
Default

I'm glad to hear it worked! I thought of one tweak to do it a little more cleanly. Instead of using a second script as I suggested, you could try ending the first (and only) script with this:

( sleep 5 ; shutdown -p now ) &

That keeps everything in one file.

Last edited by IdOp; 4 Weeks Ago at 02:00 PM.
Reply With Quote
  #5   (View Single Post)  
Old 4 Weeks Ago
stanl stanl is offline
Real Name: Stan
Port Guard
 
Join Date: Jun 2019
Posts: 10
Default Cron and email

No, that doesn't work. The backup script runs and then shutdown occurs but no mail is sent.

No matter. Your first suggestion works fine and that's good enough for me.

Thanks again for your help.
Reply With Quote
  #6   (View Single Post)  
Old 4 Weeks Ago
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 876
Default

You're welcome, and thanks for reporting the results of that idea.
Reply With Quote
  #7   (View Single Post)  
Old 4 Weeks Ago
fvgit's Avatar
fvgit fvgit is offline
Spikes in tights
 
Join Date: May 2016
Location: perl -MMIME::Base64 -le 'print decode_base64("U2hlcndvb2QgRm9yZXN0")'
Posts: 184
Default

Have you tried rewriting your script to sth. like this?
Code:
#!/bin/sh
backup() {
    mount /dev/sd1a /mnt/
    /usr/local/bin/rsync -avh --exclude-from=/home/stan/rsync.exclude /home/stan /mnt/
    /usr/local/bin/rsync -avh /etc /mnt/
    /usr/local/bin/rsync -avh /var //mnt/
    cd /home/stan/
    umount /mnt/
}
backup | mail -s "backup log" root
sleep 5
shutdown -p now
I haven't tested this. But basically this should put the backup routine into a separate function backup() whose output is then mailed (by the backup script not by cron) to root before shutdown occurs. Optionally you can put a higher value on sleep, just in case, to insure the mail delivery process can complete. Although it's probably not necessary.
Reply With Quote
  #8   (View Single Post)  
Old 3 Weeks Ago
stanl stanl is offline
Real Name: Stan
Port Guard
 
Join Date: Jun 2019
Posts: 10
Default Cron and email

My apologies for taking so long to respond. I've been away for a few days and only got back last night.

I've tried out your script and it seems to work flawlessly. I'll be using it going forward.

Thanks for taking the time and making the effort. It is appreciated.
Reply With Quote
  #9   (View Single Post)  
Old 3 Weeks Ago
fvgit's Avatar
fvgit fvgit is offline
Spikes in tights
 
Join Date: May 2016
Location: perl -MMIME::Base64 -le 'print decode_base64("U2hlcndvb2QgRm9yZXN0")'
Posts: 184
Default

No worries. Glad it works!

Besides, I basically didn't do anything. I just took IdOp's suggestion and streamlined it a little sprinkling a function call here and a few curly brackets there.
Reply With Quote
Old 3 Weeks Ago
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 876
Default

Quote:
Originally Posted by fvgit View Post
Besides, I basically didn't do anything.
Oh I don't know. You had the idea to introduce an explicit mail command instead of relying on the cron output. That takes charge of the part that was going wrong and makes it happen when you want, and also could be done with everything in one file. Both good improvements!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
syspatch cron job azdps OpenBSD General 4 29th June 2018 04:05 PM
cron newsyslog message starbuck FreeBSD General 2 6th August 2008 07:33 PM
Please help me automate getmail with cron cssgalactic FreeBSD General 2 9th July 2008 10:13 PM
Cronjob script does not run in cron godfrank FreeBSD General 9 30th June 2008 12:41 AM
Using sendmail in a cron job erehwon OpenBSD General 6 15th May 2008 09:03 PM


All times are GMT. The time now is 09:36 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick