I wrote the following script for a computer that loses its network about once a week do to power outages. When I run the program as a shell script, and if the address begins with 128 it doesn’t do anything. If it doesn’t begin with 128, it restarts the network just in case the network is back up.
#!/bin/sh
#This makes sure it is connected to 128.*** address
connect=ifconfig -a | sed -n '2p' | awk '{print $2}' | sed 's/addr://' | sed 's/.].*//'
if “$connect” != “128” ]; then
/etc/init.d/network restart
fi
However, if I run this script as a cronjob with the following information for the cron file
0,15,30,45 * * * * /root/CronJobs/networkcheck
it restarts the network every time and gives the following message in /var/log/messages
Sep 11 13:15:01 wngr497-pc09 /usr/sbin/cron[24304]: (root) CMD (/root/CronJobs/networkcheck)
Sep 11 13:15:01 wngr497-pc09 ifdown: eth0 device: Broadcom Corporation NetXtreme BCM5751 Gigabit Ethernet PCI Express (rev 01)
Sep 11 13:15:01 wngr497-pc09 dhcpcd[24047]: terminating on signal 15 on eth0
Sep 11 13:15:01 wngr497-pc09 avahi-daemon[3353]: Withdrawing address record for 128.193.97.39 on eth0.
Sep 11 13:15:01 wngr497-pc09 avahi-daemon[3353]: Leaving mDNS multicast group on interface eth0.IPv4 with address 128.193.97.39.
Sep 11 13:15:01 wngr497-pc09 avahi-daemon[3353]: Interface eth0.IPv4 no longer relevant for mDNS.
Sep 11 13:15:01 wngr497-pc09 avahi-daemon[3353]: Withdrawing address record for fe80::218:8bff:fe23:4e61 on eth0.
Sep 11 13:15:02 wngr497-pc09 modify_resolvconf: restored /etc/resolv.conf.saved.by.dhcpcd.eth0 to /etc/resolv.conf
Sep 11 13:15:02 wngr497-pc09 SuSEfirewall2: Warning: ip6tables does not support state matching. Extended IPv6 support disabled.
Sep 11 13:15:02 wngr497-pc09 SuSEfirewall2: SuSEfirewall2 not active
Sep 11 13:15:02 wngr497-pc09 ifup: lo
Sep 11 13:15:02 wngr497-pc09 ifup: lo
Sep 11 13:15:02 wngr497-pc09 ifup: IP address: 127.0.0.1/8
Sep 11 13:15:02 wngr497-pc09 ifup:
Sep 11 13:15:02 wngr497-pc09 ntpdate[24812]: can’t find host time.oregonstate.edu
Sep 11 13:15:02 wngr497-pc09 ntpdate[24812]: no servers can be used, exiting
Sep 11 13:15:02 wngr497-pc09 ifup: eth0 device: Broadcom Corporation NetXtreme BCM5751 Gigabit Ethernet PCI Express (rev 01)
Sep 11 13:15:02 wngr497-pc09 kernel: ADDRCONF(NETDEV_UP): eth0: link is not ready
Sep 11 13:15:03 wngr497-pc09 ifup-dhcp: eth0 (DHCP)
Sep 11 13:15:03 wngr497-pc09 ifup-dhcp: .
Sep 11 13:15:03 wngr497-pc09 ifup-dhcp: .
Sep 11 13:15:04 wngr497-pc09 kernel: tg3: eth0: Link is up at 100 Mbps, full duplex.
Sep 11 13:15:04 wngr497-pc09 kernel: tg3: eth0: Flow control is off for TX and off for RX.
Sep 11 13:15:04 wngr497-pc09 kernel: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
Sep 11 13:15:05 wngr497-pc09 ifup-dhcp: .
Sep 11 13:15:05 wngr497-pc09 ifup-dhcp: .
Sep 11 13:15:06 wngr497-pc09 avahi-daemon[3353]: Registering new address record for fe80::218:8bff:fe23:4e61 on eth0.*.
Sep 11 13:15:07 wngr497-pc09 ifup-dhcp: .
Sep 11 13:15:08 wngr497-pc09 ifup-dhcp: no IP address yet… backgrounding.
Sep 11 13:15:08 wngr497-pc09 SuSEfirewall2: Warning: ip6tables does not support state matching. Extended IPv6 support disabled.
Sep 11 13:15:08 wngr497-pc09 SuSEfirewall2: SuSEfirewall2 not active
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Joining mDNS multicast group on interface eth0.IPv4 with address 128.193.97.39.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: New relevant interface eth0.IPv4 for mDNS.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Registering new address record for 128.193.97.39 on eth0.IPv4.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Withdrawing address record for 128.193.97.39 on eth0.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Leaving mDNS multicast group on interface eth0.IPv4 with address 128.193.97.39.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Interface eth0.IPv4 no longer relevant for mDNS.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Joining mDNS multicast group on interface eth0.IPv4 with address 128.193.97.39.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: New relevant interface eth0.IPv4 for mDNS.
Sep 11 13:15:13 wngr497-pc09 avahi-daemon[3353]: Registering new address record for 128.193.97.39 on eth0.IPv4.
Sep 11 13:15:13 wngr497-pc09 modify_resolvconf: Service dhcpcd modified /etc/resolv.conf. See info block in this file
Sep 11 13:15:13 wngr497-pc09 ifup: eth0 device: Broadcom Corporation NetXtreme BCM5751 Gigabit Ethernet PCI Express (rev 01)
Sep 11 13:15:13 wngr497-pc09 ntpdate[25054]: adjust time server 128.193.10.15 offset 0.066001 sec
Sep 11 13:15:13 wngr497-pc09 SuSEfirewall2: Warning: ip6tables does not support state matching. Extended IPv6 support disabled.
Sep 11 13:15:13 wngr497-pc09 syslog-ng[2169]: SIGHUP received, restarting syslog-ng
Sep 11 13:15:13 wngr497-pc09 SuSEfirewall2: SuSEfirewall2 not active
Sep 11 13:15:14 wngr497-pc09 syslog-ng[2169]: new configuration initialized
Sep 11 13:15:15 wngr497-pc09 kernel: eth0: no IPv6 routers present
Sep 11 13:15:15 wngr497-pc09 kernel: klogd 1.4.1, ---------- state change ----------
and I get the following message in /var/spool/mail/root
From root@wngr497-pc09.physics.oregonstate.edu Thu Sep 11 13:15:38 2008
Return-Path: <root@wngr497-pc09.physics.oregonstate.edu>
X-Original-To: root
Delivered-To: root@wngr497-pc09.physics.oregonstate.edu
Received: by wngr497-pc09.physics.oregonstate.edu (Postfix, from userid 0)
id 347E096CC; Thu, 11 Sep 2008 13:15:38 -0700 (PDT)
From: root@wngr497-pc09.physics.oregonstate.edu
To: root@wngr497-pc09.physics.oregonstate.edu
Subject: Cron <root@wngr497-pc09> /root/CronJobs/networkcheck
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20080911201538.347E096CC@wngr497-pc09.physics.oregonstate.edu>
Date: Thu, 11 Sep 2008 13:15:01 -0700 (PDT)
/root/CronJobs/networkcheck: line 16: ifconfig: command not found
Shutting down network interfaces:
eth0 device: Broadcom Corporation NetXtreme BCM5751 Gigabit Ethernet PCI Express (rev 01)
1A…doneShutting down service network . . . . . . . . . . . . . . …done
Hint: you may set mandatory devices in /etc/sysconfig/network/config
Setting up network interfaces:
lo
lo IP address: 127.0.0.1/8
Checking for network time protocol daemon (NTPD): …unused
1A…done eth0 device: Broadcom Corporation NetXtreme BCM5751 Gigabit Ethernet PCI Express (rev 01)
eth0 (DHCP) . . . . . no IP address yet… backgrounding.
1Awaiting
Setting up service network . . . . . . . . . . . . . . . …done
Could some one help me out?
Thanks,
Jason