From owner-freebsd-questions@FreeBSD.ORG Wed Aug 29 16:18:29 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E5E1616A417 for ; Wed, 29 Aug 2007 16:18:29 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 5D84F13C46B for ; Wed, 29 Aug 2007 16:18:27 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (vader.bytemobile.ondsl.gr [83.235.244.135]) (authenticated bits=128) by igloo.linux.gr (8.14.1/8.14.1/Debian-8) with ESMTP id l7TGHuxI002942 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 29 Aug 2007 19:18:17 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.1/8.14.1) with ESMTP id l7TGHZKI005485; Wed, 29 Aug 2007 19:17:52 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.1/8.14.1/Submit) id l7TGHXd6005484; Wed, 29 Aug 2007 19:17:33 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Wed, 29 Aug 2007 19:17:33 +0300 From: Giorgos Keramidas To: Hinkie Message-ID: <20070829161733.GA5302@kobe.laptop> References: <01a901c7ea3d$0ad62720$1e00a8c0@cheqsoft.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <01a901c7ea3d$0ad62720$1e00a8c0@cheqsoft.local> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.951, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.45, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Aug 2007 16:18:30 -0000 On 2007-08-30 01:03, Hinkie wrote: > Hi > > I want to run a cron job in /etc/crontab that runs (ifconfig em0 down; > ifconfig em0 up) if my cables static ip gateway can't be pinged but I > can't figure it out. I can't get the syntax that runs in the command > window, to then put intot the crontab.... > > Can anyone help me? > > I've tried all sorts such as: > > if (ping -c1 a.b.c.1 != 1) then ifconfig em0 down; ifconfig em0 up endif > if ping -c1 a.b.c.1 != 1 then ping -c1 203.97.234.1 endif > if ping -c1 a.b.c.1 (ifconfig em0 down; ifconfig em0 up) > if ( ping -c1 a.b.c.1 != 1) ( ping -c1 203.97.234.182 ) > if $command("=ping -c1 a.b.c.1 =0") echo sucess > if ping -c 1 -w 1 a.b.c.1 >/dev/null echo sucess > if [[ (ping -q -c 3 a.b.c.1) == @(*100% packet loss*) ]]; echo sucess > if ! ping -c a.b.c.1 > /dev/null ; then echo sucess ; echo fail; fi I see you are using csh(1) shell syntax. The crontab entries are passed as commands to /bin/sh, so this sort of syntax won't really work. In general, I find the idea of long, complex crontab entries _very_ error-prone, and I prefer writing scripts which are _callable_ from cron. For example, I have several local clones of repositories which can be synchronized using Mercurial, but properly synchronizing them would require running: #!/bin/sh repo="mercurial/crew" repodir="${HOME}/hg/mercurial/crew" cd "${repodir}" if test $? -ne 0 ; then echo "${repodir}: directory not accessible." >&2 exit 1 fi hg pull Instead of cluttering my crontab with long-winded, complex stuff like this, I wrote a short shell script -- like the one above -- and saved it in my `~/cron.d' directory. Now my crontab contains: */2 * * * * ${HOME}/cron.d/hg-pull-crew.sh Try something similar too. It's easier to maintain potentially complex actions, if you `abstract away' all the complexity behind a script. Then you can also use whatever you prefer to write the scripts, like csh(1), Perl, or Python. Just make the script file executable and make sure it contains the proper '#!' line. - Giorgos