Click to See Complete Forum and Search --> : Cron annoyingness...


Bradmont
04-06-2001, 12:28 AM
Well, I just wrote myself a little combination of a bash script and a couple little c++ apps to store my IP address in a file, then when the script is run, it checks my current IP against the logged one to see if it's changed, email the result to a web-based email account (for those annoying times when I'm away from the house & my IP changes, so I'll still be able to connect to my machine). I prolly could've done it entirely in bash, but then again I don't know bash...

But anyway, here's the problem (and no, this does not belong in the programming/scripting forum... at least I don't think it does ;))

I want to run the script in a cron job to execute once an hour. Now the script runs perfectly when activated from the command line, but doesn't seem to be executed by cron... I inserted the following line into my cron file:

16 * * * * /root/ipmail/ipmail

to get it to run at 16 minutes past every hour...

Here's the code for the script, if that helps:


#!/bin/bash
ifconfig > temp

/root/ipmail/getip temp moo
#takes the redirected output from ifconfig and extracts
#the IP addresses, storing them in the file 'moo'

/root/ipmail/compare moo /root/ipaddress
#This compares the file with the stored one, which is /root/ipaddress.
#It returns 2 if the files are different.

if [ $? = 2 ]
then
mail lordbradmont@gotmail.com < temp
rm /root/ipaddress
cp moo /root/ipaddress
fi
rm temp
rm moo


Thanx.


<edit> Heh, forgot to mention that it doesn't work from cron... </edit>

[ 06 April 2001: Message edited by: Bradmont ]

Tyr-7BE
04-06-2001, 12:49 AM
Dude, what's the problem? It runs at 16 minutes past each hour? What's wrong? Sorry if I don't get it :(

Bradmont
04-06-2001, 01:16 AM
Whoops... heh heh... forgot to mention:

It doesn't work from the cron job! I guess that's a minor detail ;)

PLBlaze
04-06-2001, 02:30 AM
Create or touch /etc/cron.allow file and put your user name on a single line.I'm not sure if crond needs to be restarted for the changes to take effect but you will know if the job gets executed...Hope this helps.

Bradmont
04-06-2001, 03:18 AM
Nope, didn't work :( (I do have other cron jobs that run... now I'm being all confused...) Thanks anyway...

PLBlaze
04-06-2001, 10:05 AM
Hmm... the ipmail script resides under /root but you executing is as regular user which hasn no read/write access to that directory,unless i'm wrong or is the job run from root's crontab?

Is the directory in your $PATH?Just trying to give few hints,sometimes it's those small things... :)

Bradmont
04-06-2001, 04:41 PM
No, it was in root's crontab :(. I've since moved it to /usr/local/bin anyway, storing the temp files in /tmp and the ipaddress file in /etc.

Anyone have any other ideas?

Craig McPherson
04-06-2001, 09:24 PM
First... I never bother with user cron files. Just use the global /etc/crontab. In /etc/crontab, you have to specify the user you want to run it as, so your line should look like this:

16 * * * * root /root/ipmail/ipmail

Make sure you restart cron after you edit the file.

Bradmont
04-06-2001, 09:56 PM
w00t! It worked. Thanks, Craig! :cool:

<edit> I can't believe I put an apostrophe in 'thanks'</edit>

[ 07 April 2001: Message edited by: Bradmont ]

bdg1983
04-07-2001, 06:20 AM
For future reference, you can always resort to the Scheduling NHF (http://www.linuxnewbie.org/nhf/intel/misc/scheduling.html).