LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running scheduled script with cron (https://www.linuxquestions.org/questions/linux-newbie-8/running-scheduled-script-with-cron-866170/)

snadjari 03-03-2011 02:36 AM

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?

Snark1994 03-03-2011 09:53 AM

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 * * * *"

tronayne 03-03-2011 09:58 AM

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.

snadjari 03-03-2011 11:01 PM

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.

snadjari 03-04-2011 02:15 AM

@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?

Snark1994 03-04-2011 02:39 PM

Well done :D

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'


All times are GMT -5. The time now is 01:16 PM.