Register
It is currently Mon Sep 01, 2014 12:36 pm

please help understand '&&' opt


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun May 10, 2009 4:46 am   

Joined: Sun May 10, 2009 4:25 am
Posts: 1
Hi,

I have a script which will be run the cron weekly, here's the content of the script

Code:
#!/bin/bash

emerge --sync &&

# prepare the mail subject
echo -e 'subject: emerge updates' > /etc/cron.weekly/updates.world &&

# prepare the mail body
emerge -uvDp world >> /etc/cron.weekly/updates.world &&

# sendmail
sendmail root < /etc/cron.weekly/updates.world


Yes, I am using Gentoo Linux.

What I want to do is the sync the portage tree weekly, and send me an email about what needs to be updated. But I never receive a email...When I check the system log, I found something like this:

May 9 04:10:03 david-gentoo sSMTP[18917]: Set MailHub="my.email.com"
May 9 04:10:03 david-gentoo sSMTP[18917]: via SMTP Port Number="25"
May 9 04:10:37 david-gentoo sSMTP[18917]: 220 Esmtp XX Mail Server
May 9 04:10:37 david-gentoo sSMTP[18917]: EHLO gentoo
May 9 04:10:38 david-gentoo sSMTP[18917]: 250 8BITMIME
May 9 04:10:38 david-gentoo sSMTP[18917]: AUTH LOGIN
May 9 04:10:40 david-gentoo sSMTP[18917]: 334 3TGTGEW
May 9 04:10:40 david-gentoo sSMTP[18917]: GW3TGEWRT43
May 9 04:10:41 david-gentoo sSMTP[18917]: 334 DSG345TGDG
May 9 04:10:41 david-gentoo sSMTP[18917]: DGEWT5RGF==
May 9 04:10:42 david-gentoo sSMTP[18917]: 235 Authentication successful
May 9 04:10:42 david-gentoo sSMTP[18917]: MAIL FROM:<a@b.com>
May 9 04:10:43 david-gentoo sSMTP[18917]: 250 Ok
May 9 04:10:43 david-gentoo sSMTP[18917]: RCPT TO:<a@b.com>
May 9 04:10:43 david-gentoo sSMTP[18917]: 250 Ok
May 9 04:10:43 david-gentoo sSMTP[18917]: DATA
May 9 04:10:44 david-gentoo sSMTP[18917]: 354 End data with <CR><LF>.<CR><LF>
May 9 04:10:44 david-gentoo sSMTP[18917]: Received: by gentoo (sSMTP sendmail emulation); Sat, 09 May 2009 04:10:02 +0800
May 9 04:10:44 david-gentoo sSMTP[18917]: From: "root" <a@b.com>
May 9 04:10:44 david-gentoo sSMTP[18917]: Date: Sat, 09 May 2009 04:10:02 +0800
May 9 04:10:44 david-gentoo sSMTP[18917]: To: root
May 9 04:10:44 david-gentoo sSMTP[18917]: Subject: Cron <a@b.com> test -x /usr/sbin/run-crons && /usr/sbin/run-crons
May 9 04:10:44 david-gentoo sSMTP[18917]: X-Cron-Env: <SHELL=/bin/bash>
May 9 04:10:44 david-gentoo sSMTP[18917]: X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
May 9 04:10:44 david-gentoo sSMTP[18917]: X-Cron-Env: <MAILTO=root>
May 9 04:10:44 david-gentoo sSMTP[18917]: X-Cron-Env: <HOME=/>
May 9 04:10:44 david-gentoo sSMTP[18917]: X-Cron-Env: <LOGNAME=root>
May 9 04:10:44 david-gentoo sSMTP[18917]: X-Cron-Env: <USER=root>
A long list of the out put of command 'emerge --sync'
May 9 04:15:44 david-gentoo sSMTP[18917]: killed: timeout on stdin while reading body -- message saved to dead.letter.
May 9 04:15:44 david-gentoo sSMTP[18917]: Timeout on stdin while reading body


The last message is why I feel strange. By using the '&&' operator, I suppose every command can only be executed after the previous one have been successfully run. But it look like the command to retrieve the update information and the command to send the email are running in parallel, and since the input of the email is the out put of the updating command, and it takes a long time the retrieve the update information, the email send command failed to execute in time. By checking my system, the file which I used to save the update information is NOT updated at all, but the system DID retrieved the update information.

If my script is not correct, would some one point it out? And help me fix it.

Thanks,
David


Top
 Profile  
 PostPosted: Mon May 11, 2009 2:58 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi!

Yeah, you're right. It looks like you're running each of these tasks in the background, which makes them run in parallel. Just take out the "&&" in each of those lines and you should be fine. The script should naturally wait for each step to finish before moving on to the next:

Code:
#!/bin/bash

emerge --sync

# prepare the mail subject
echo -e 'subject: emerge updates' > /etc/cron.weekly/updates.world

# prepare the mail body
emerge -uvDp world >> /etc/cron.weekly/updates.world

# sendmail
sendmail root < /etc/cron.weekly/updates.world


That should do it! Let us know how it goes.

-J


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP