LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-13-2011, 01:40 AM   #1
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Rep: Reputation: Disabled
Cronjob is not working


Hi..everyone,
Here is the crontab entry
Quote:
#crontab -l
# m h dom mon dow command
*/2 * * * * /home/linux/net_restart
Here is the net_restart script content
Quote:
cat net_restart
#!/bin/bash
now=`ifconfig eth0 | grep -i "inet addr" | awk '{ print $2}' | cut -d. -f 4`
ip=198
rm -rf /tmp/test
if [ $ip -ne $now ]; then
/etc/init.d/networking restart > /dev/null
fi
This is because, in one of the server the IP address is often changing itself to dhcp even the static IP address is given in /etc/network/interface file. Then I'll login to the server and running "/etc/init.d/networking restart" command to bring the IP address back to 198. But we cannot say that when it changes to dhcp, sometimes working fine for a week and sometimes for 2 or 3 days and sometimes it is changing to dhcp twice in a day. So I thought of make a shell script and add it to crontab for watch in every 2 minutes.
First I thought of to check this from my PC and then add it to the server if everything goes fine. The script file works fine if I manually run and it is changing back to static IP address if the last octet of IP address is not "198", but it is not through crontab. Then I created /tmp/test file and added an entry in script to remove the file. But it is neither removing /tmp/test file nor restarting networking service through crontab but both can be done only if I run it manually. I also removed all crontab entries and re-added only this script but still cronjob is not working.
So please help me why crontab is not working in my PC. I am using ubuntu 11.04

Last edited by loveulinux; 11-13-2011 at 01:41 AM. Reason: correction
 
Old 11-13-2011, 03:10 AM   #2
aysheaia
LQ Newbie
 
Registered: Jun 2011
Distribution: Ubuntu
Posts: 26

Rep: Reputation: Disabled
When launching jobs with crontab, the value of PATH is minimal. For example, /sbin is not is PATH
So you should replace your commands with their absolute paths or redefine the PATH variable inside your crontab :
Code:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
For testing purpose, you could also add some echo commands and redirect all outputs to a log file :
Code:
*/2 * * * * /home/linux/net_restart &>/tmp/net.log
 
1 members found this post helpful.
Old 11-13-2011, 05:06 AM   #3
loveulinux
Member
 
Registered: Aug 2011
Posts: 68

Original Poster
Rep: Reputation: Disabled
Cronjob is not working

Hi..Thanks for the reply.
Super, I added an entry in crontab as you said and found the problem is solved
Quote:
#crontab -l
# m h dom mon dow command
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/2 * * * * /home/linux/net_restart &>/tmp/net.log
I ran "dhclient eth0" and it got changed to some other IP. Then I watched for a while. Super now it started to change to static IP if the last octet of the IP address is not "198".
Once again thank you very much master.

Quote:
Originally Posted by aysheaia View Post
When launching jobs with crontab, the value of PATH is minimal. For example, /sbin is not is PATH
So you should replace your commands with their absolute paths or redefine the PATH variable inside your crontab :
Code:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
For testing purpose, you could also add some echo commands and redirect all outputs to a log file :
Code:
*/2 * * * * /home/linux/net_restart &>/tmp/net.log

Last edited by loveulinux; 11-13-2011 at 05:07 AM. Reason: correction
 
Old 11-13-2011, 06:02 AM   #4
aysheaia
LQ Newbie
 
Registered: Jun 2011
Distribution: Ubuntu
Posts: 26

Rep: Reputation: Disabled
You are welcome
 
  


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
cronjob not working intermittently sinfuldips Solaris / OpenSolaris 4 02-04-2010 02:31 AM
cronjob not working.. vivendi Linux - Newbie 6 08-12-2009 05:40 PM
cronjob not working vinaytp Linux - Newbie 3 06-23-2009 10:06 AM
Cronjob stops working after a while ToonM Slackware 4 01-23-2007 01:53 AM
Call of Duty 2 Cronjob Not working zend Linux - Software 3 11-15-2005 11:52 AM


All times are GMT -5. The time now is 04:32 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration