LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2011, 02:36 AM   #1
snadjari
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Rep: Reputation: 0
Unhappy Running scheduled script with cron


I'm trying to setup a public IP change notifier script. I'm aware of dyndns, but at the moment I think a notifier would be enough. Found some scripts on the Internet and tried to mix them as I don't need some of the functions on the original scripts.

The script ran well on terminal, i.e. when there is a change on my public IP address it would email me the new IP address.

But somehow it failed me when being put in as a cron job. It emailed me blank, i.e. no IP address at all.

So, the cron scheduling is seems to be working and also the emailing part, only the data is somehow failed to be processed and/or parsed along the way.

I'm just moved to Linux since July last year, so please just shoot me anything to make this works on cron job (scheduled).

myscript.sh

#!/bin/sh

FILE="/<dir_name>/<temp_file_name>"
DEV="<interface_code>"

NEWIP=`ifconfig $DEV | awk '/inet addr/ {print $2}' | cut -d: -f2`
[ -e $FILE ] && OLDIP=`cat $FILE`

if [ "$NEWIP" != "$OLDIP" ]; then
sendemail -f <sender@gmail.com> -t <recipient@gmail.com> -u "New IP Address" -m $NEWIP -s smtp.gmail.com:587 -o tls=yes -xu <gmail_username> -xp <gmail_pwd>
echo $NEWIP > $FILE
fi

Notes:
1. The script file is already made executable, i.e. chmod 755. The script and the directory it resides in (/<dir_name>) are own by root.
2. The "/<dir_name>/<temp_file_name> is own by root.
3. The <interface_code> is eth0, or else.
4. The program sendemail is installed and working fine. Tested by manually sending test email, i.e. run from command line directly not using any script file.
5. The email being talked about is a gmail account, not local linux mail.
6. The crontab entries is:
*/30 * * * * /<dir_name>/myscript.sh

Anything else to check?
 
Old 03-03-2011, 09:53 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Firstly, if you log in as root and run "mail" then it may have some cron reports telling you what's gone wrong. Secondly, unless it's a syntax I'm unaware of, "*/30 * * * *" is unusual... If you wanted it run half past every hour, I would use "30 * * * *"
 
1 members found this post helpful.
Old 03-03-2011, 09:58 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
The cron utility executes in a basic (read: limited) environment. If your shell program runs as you expect in a terminal (where you have a full user-oriented environment) but not in cron, that's most likely to be the problem.

One thing you can do is, in your shell program, use absolute path names for everything (cron doesn't know about your PATH environment). If that works, great, all done.

If it doesn't work a trick is to include as much of your environment in the shell program as needed; a simple thing to do is
Code:
echo ${PATH} >> shell_program_name
That will look something like this
Code:
/usr/bin:/bin:/usr/games:/usr/lib64/java/bin:/usr/lib64/java/jre/bin:/usr/lib64/java/bin:/usr/lib64/kde4/libexec:/opt/mailman/bin:/usr/lib64/qt/bin:/usr/share/texmf/bin:.
You would want this at the top of your shell program something like
Code:
#!/bin/sh
#
# Set up reasonable PATH
#
export PATH=/usr/bin:/bin:/usr/games:/usr/lib64/java/bin:/usr/lib64/java/jre:
export PATH=${PATH}/bin:/usr/lib64/java/bin:/usr/lib64/kde4/libexec:
export PATH=${PATH}/opt/mailman/bin:/usr/lib64/qt/bin:/usr/share/texmf/bin:.

program code starts here.
Give that a shot and see what happens.

Hope this helps some.
 
1 members found this post helpful.
Old 03-03-2011, 11:01 PM   #4
snadjari
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Hi all,

First, I'd like to thank you all for the replies. Secondly, I must say sorry as I didn't mentioned it earlier that I was playing the script on a machine running Zentyal 2.0-3 (which is based on Ubuntu 10.04) on a test environment.

@Snark1994:
As for mail check, I did typed "mail" (if it's the right way to run "mail"), but Zentyal said:
The program 'mail' can be found in the following packages:
* heirloom-mailx
* mailutils
Try: apt-get install <selected package>

Well, I guess Zentyal don't use mail by default.

As for the crontab line, Ok, will change as per your suggestion. Just the number without the slash "/" will run the script every 30mins, right?

Anyway, as I mentioned it earlier I was just copy-pasting and then mixing the codes, so at that time I have no idea of what is the proper syntax.

However, your reply made me check the syntax, hopefully I'm looking at the right place: http://crontab.org/

I found that the slash "/' notation is what they called "Step Values".
-- quote --
Step values can be used in conjunction with ranges. Following a range with ``/<number>'' specifies skips of the number's value through the range. For example, ``0-23/2'' can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is ``0,2,4,6,8,10,12,14,16,18,20,22'').

Steps are also permitted after an asterisk, so if you want to say ``every two hours'', just use ``*/2''.
-- end quote --

Does this makes */30 * * * * is the same with 30 * * * * ?



@tronayne:
Ah, I see. No wonder it does not work on cron. Ok, will give it a try an come back.
 
Old 03-04-2011, 02:15 AM   #5
snadjari
LQ Newbie
 
Registered: Mar 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

@tronayne:
Yeah, it works now! Thank you, Sir.

@Snark1994 (and @tronayne):
BTW, I followed Snark1994's suggestion to changed the crontab line. To make sure it works, and can be quickly verified, I modified the timing into 2 minutes instead of the originals 30 minutes:

2 * * * *

that is for the cron to run the script every 2 minutes. However, after several minutes waiting, it's just not working. But when I changed back to the slash notation:

*/2 * * * *

Then it works.

How is the correct way to asked cron to run every certain minute? With or without slash notation?
 
Old 03-04-2011, 02:39 PM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Well done

Regarding 'mail', it should send an email to root@yourhostname, so I'd have thought you have some way of reading the mail...

And "2 * * * *" will run at 2 minutes past every hour (ie 1:02, 2:02 etc.) I hadn't seen that '/' notation If you want to run it every thirty minutes, than that seems to be the way to go.

If you've solved it, then please mark the thread as 'SOLVED'
 
1 members found this post helpful.
  


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
[SOLVED] running php script via cron.... bangsters Linux - General 2 09-16-2009 09:25 AM
bash script running by cron fakie_flip Programming 17 12-24-2007 10:26 PM
Problem in running a script in CRON senthilvael Linux - Newbie 11 11-21-2007 01:23 PM
script not running as a cron job sanjith11 Programming 5 11-23-2004 08:42 AM
script not running correctly off cron wedgeworth Linux - Software 3 10-14-2003 08:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:51 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