LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2007, 02:28 PM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Question 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?
 
Old 11-28-2007, 03:18 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 11-28-2007, 04:24 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I agree with jlinkels, so here's my
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.

Last edited by colucix; 11-28-2007 at 04:25 PM.
 
Old 11-28-2007, 04:57 PM   #4
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Exclamation

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.
 
Old 11-28-2007, 05:14 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 11-28-2007, 05:33 PM   #6
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
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

Last edited by fakie_flip; 11-28-2007 at 06:22 PM.
 
Old 11-28-2007, 07:29 PM   #7
andrew.46
Senior Member
 
Registered: Oct 2007
Distribution: Slackware
Posts: 1,365

Rep: Reputation: 493Reputation: 493Reputation: 493Reputation: 493Reputation: 493
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 View Post
[...]
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

Last edited by andrew.46; 11-28-2007 at 11:29 PM. Reason: Added extra links + removed link to my page
 
Old 11-28-2007, 10:10 PM   #8
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
My username and password are correct. I just choose not to give that information out to everyone. I am currently configuring Postfix.

Last edited by fakie_flip; 11-28-2007 at 10:13 PM.
 
Old 11-28-2007, 10:40 PM   #9
andrew.46
Senior Member
 
Registered: Oct 2007
Distribution: Slackware
Posts: 1,365

Rep: Reputation: 493Reputation: 493Reputation: 493Reputation: 493Reputation: 493
Hi,

Quote:
Originally Posted by fakie_flip View Post
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
 
Old 11-28-2007, 11:19 PM   #10
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
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?
 
Old 11-29-2007, 12:45 AM   #11
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
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.
 
Old 11-29-2007, 03:27 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 11-29-2007, 08:41 AM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by fakie_flip View Post
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.
 
Old 11-29-2007, 11:53 AM   #14
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 11-29-2007, 11:56 AM   #15
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Original Poster
Rep: Reputation: 85
Quote:
Originally Posted by colucix View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help: using cron + my bash script --> don't want mail from script beeblequix Linux - General 7 11-23-2007 09:25 PM
Bash script and cron job rust8y Linux - General 2 07-08-2006 07:45 AM
bash script causes trouble with cron Suinatsa Programming 10 06-14-2006 03:20 AM
cron bash script troubles zzyzx Slackware 3 04-12-2005 12:27 PM
bash script from cron kubla Programming 3 01-22-2004 04:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration