LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script running by cron (https://www.linuxquestions.org/questions/programming-9/bash-script-running-by-cron-603067/)

fakie_flip 11-28-2007 02:28 PM

bash script running by cron
 
Is there anything wrong with this bash script that would cause it not to work correctly?

Code:

$ crontab -l
# m h  dom mon dow  command
25 * * * * wget -P/tmp http://www.blackcats-games.net/signup.php; if (grep -q 'current user account limit' /tmp/signup.php ) ; then DISPLAY=:0 xmessage "account registration opened at blackcat" ; fi ; rm -f /tmp/signup.php > /dev/null 2>&1

How could I modify it to only send stderr and stdout to /dev/null if 'current user account limit' does not exist according to grep, so that I would also receive an email?

jlinkels 11-28-2007 03:18 PM

I would write a nice bash script, test that one from the command line and once it works, call that batch script from cron. Execute with
Code:

sh -x yourscript
for debugging. It is almost impossible to do it from cron without losing a lot of time.

jlinkels

colucix 11-28-2007 04:24 PM

I agree with jlinkels, so here's my :twocents:
Code:

#!/bin/bash
wget -P/tmp http://www.blackcats-games.net/signup.php > /tmp/logfile 2>&1

if (grep -q 'current user account limit' /tmp/signup.php)
then
  DISPLAY=:0 xmessage "account registration opened at blackcat"
  cat /tmp/logfile
fi

rm -f /tmp/signup.php /tmp/logfile > /dev/null 2>&1

You can redirect stdout and stderr from any command which is supposed to generate them, to a temporary file. Cat this file only if condition is true and you will receive it by e-mail, otherwise it's lost.

fakie_flip 11-28-2007 04:57 PM

Thank both of you for the information. I followed the advice, and here is what I have now.

Code:

$ crontab -l
# m h  dom mon dow  command
25 * * * * sh -x "wget -P/tmp http://www.blackcats-games.net/signup.php > /tmp/logfile 2>&1; if (grep -q 'current user account limit' /tmp/signup.php); then DISPLAY=:0 xmessage 'account registration opened at blackcat'; cat /tmp/logfile; fi; rm -f /tmp/signup.php /tmp/logfile > /dev/null 2>&1"

If I use sh -x, won't that send emails every 1 hour? These emails are already getting sent to the wrong person, and he's very pissed. There's something wrong with my mail server configuration that I haven't figured out. I'm going to resolve that problem by using Postfix for smtp instead.

colucix 11-28-2007 05:14 PM

What jlinkels meant is to write a script, test it by sh -x for debugging, then put the script in crontab (instead of the long list of commands to execute). Anyway, sh -x give a trace of all the commands executed from the shell and there is no reason to put it in a cron job. Regarding mail recipient, by default the cron daemon sends mails to the owner of the crontab, but you can always specify the recipient by assigning the MAILTO variable at the beginning of your crontab, e.g.
Code:

MAILTO=colucix
* 3 * * * $HOME/bin/my_script


fakie_flip 11-28-2007 05:33 PM

Emails are being sent to my username. They are being sent to chris@gmail.com. chris is my username, but that is not my gmail account. Here is my sSMTP configuration.

Code:

$ cat /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=christopher.lemire@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
rewriteDomain=gmail.com

# The full hostname
hostname=ubuntu

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

UseSTARTTLS=YES                    # Send SSL/TLS messages to Gmail
AuthUser=mygmailusername            # Your Gmail Username
AuthPass=mygmailpass            # Your Gmail Password
rewriteDomain=gmail.com            # So the message appears to come from Gmail
FromLineOverride=YES              # So the message appears to come from Gmail

Here are the results of this configuration.

http://img530.imageshack.us/my.php?i...ronjobaij7.png

Here is the guide that gives this invalid configuration.

http://people.aapt.net.au/~adjlstrong/mutt.html

andrew.46 11-28-2007 07:29 PM

Hi,

My deepest apologies if a guide written by me has lead you astray. Can I ask if this you exact ssmtp.conf file? In which case you may need to modify the following values:

Quote:

Originally Posted by fakie_flip (Post 2973856)
[...]
Code:

$ cat /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail

[...]
AuthUser=mygmailusername        # Your Gmail Username
AuthPass=mygmailpass            # Your Gmail Password


Yes that is my page that you have mentioned, but you will find that it has been extensively modified recently including a move to msmtp. I am usually quite amenable to email discussions of my pages although this forum is fine anyway :-) That particular configuration for ssmtp I have used on my own machine for close on a year without any trouble. The exact instructions were as follows:
Code:

root=Email Address            # Your email address
mailhub=smtp.gmail.com:587    # Gmail details
UseSTARTTLS=YES              # Send SSL/TLS messages to Gmail
AuthUser=Gmail Username      # Your Gmail Username
AuthPass=Gmail Password      # Your Gmail Password
rewriteDomain=gmail.com      # So the message appears to come from Gmail
FromLineOverride=YES          # So the message appears to come from Gmail
hostname=Hostname            # Hostname: use hostname -f in a Terminal

and can still be seen in this form on the Ubuntu forums:

http://ubuntuforums.org/showthread.php?t=565326

There are multiple sites giving roughly the same configuration for ssmtp and gmail:

http://gentoo-wiki.com/HOWTO_Gmail_and_sSMTP
http://wiki.zhekov.net/ssmtpgmail
http://writingitdown.wordpress.com/2...utt-for-gmail/

I have not included directions for cron jobs on this or any of my pages as the potential for errors is just too great.

Please let me know if this resolves the issue,

Andrew

fakie_flip 11-28-2007 10:10 PM

My username and password are correct. I just choose not to give that information out to everyone. I am currently configuring Postfix.

andrew.46 11-28-2007 10:40 PM

Hi,

Quote:

Originally Posted by fakie_flip (Post 2974046)
My username and password are correct. I just choose not to give that information out to everyone. I am currently configuring Postfix.

My apologies, I had considered that since you left your full email address in sight the rest of the document may have been unmodified :-) It is still my hope that you can get ssmtp running, based on the settings that I provided. The idea of my page containing 'invalid configuration' worries me a great deal. I am normally very meticulous about such things and I would be very keen to correct any errors.

Can any other readers of this forum see the errors here?

Andrew

fakie_flip 11-28-2007 11:19 PM

I am supposed to choose mbox or Maildir for Postfix. Does anyone know what fetchmail uses? I think mbox. Should my answer be the same as what Fetchmail uses?

fakie_flip 11-29-2007 12:45 AM

Andrew,

Do you not think Sendmail or Postfix would be a better solution? I believe I'd learn a lot more from them, and they are much more capable.

colucix 11-29-2007 03:27 AM

I cannot be of much help with SMTP configuration, but regarding the issue about crontab you may consider to use MAILTO= with the complete e-mail address.

acid_kewpie 11-29-2007 08:41 AM

Quote:

Originally Posted by fakie_flip (Post 2973856)
Here are the results of this configuration.

http://img530.imageshack.us/my.php?i...ronjobaij7.png

Here is the guide that gives this invalid configuration.

http://people.aapt.net.au/~adjlstrong/mutt.html

Fakie_flip, may he who is without blame cast the first stone... it seems fairly clear that you are the one making the mistake, and are simply not paying enough attention to the document you linked to. it's advice seems perfectly sound but you have clearly dived in far too far without even testing your own config correctly.

fakie_flip 11-29-2007 11:53 AM

Quote:

Originally Posted by acid_kewpie (Post 2974482)
Fakie_flip, may he who is without blame cast the first stone... it seems fairly clear that you are the one making the mistake, and are simply not paying enough attention to the document you linked to. it's advice seems perfectly sound but you have clearly dived in far too far without even testing your own config correctly.

Quit quoting that religious garbage to me. If I made a mistake in my config, then point it out. Otherwise you're not being of any help.

fakie_flip 11-29-2007 11:56 AM

Quote:

Originally Posted by colucix (Post 2974234)
I cannot be of much help with SMTP configuration, but regarding the issue about crontab you may consider to use MAILTO= with the complete e-mail address.

Shouldn't only my gmail username be used with the MAILTO?


All times are GMT -5. The time now is 10:59 AM.