LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 12: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 12:41 AM. Reason: correction
 
Old 11-13-2011, 02: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, 04: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 04:07 AM. Reason: correction
 
Old 11-13-2011, 05:02 AM   #4
aysheaia
LQ Newbie
 
Registered: Jun 2011
Distribution: Ubuntu
Posts: 26

Rep: Reputation: Disabled
You are welcome
 
  


Reply



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

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

All times are GMT -5. The time now is 05:09 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
Open Source Consulting | Domain Registration