LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2009, 07:54 AM   #1
rosv
Member
 
Registered: Jul 2008
Distribution: Slackware, ubuntu
Posts: 53

Rep: Reputation: 15
Bash script can't handle errors when run by cron


Hi,
I have a script that that is supposed to send me an e-mail when a host is not responding to ping:

#!/bin/bash
/bin/ping -c 1 -w 5 myhost.mynetwork.intra &>/dev/null
if [ $? -ne 0 ] ; then
/usr/bin/mailx -s "Server down " john.doe@domain.com < ./status.txt
fi

The script works fine when I execute it directly but when cron executes it, the ping error is never picked up by the script so the if statement is ignored.

What have I done wrong?

Crontab entry:
*/1 * * * * /home/user/scripts/checkstatus.sh >/dev/null 2>&1
 
Old 01-08-2009, 08:28 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I see nothing strange except you have to use the full path of status.txt, unless it is placed in the crontab owner's home directory. Also, for testing purposes, remove the redirection to /dev/null from the crontab. If the cron job spits out some error, it will be sent to the owner's mail and you can check it. Hope this helps.
 
Old 01-08-2009, 09:24 AM   #3
rosv
Member
 
Registered: Jul 2008
Distribution: Slackware, ubuntu
Posts: 53

Original Poster
Rep: Reputation: 15
I had to disable the cron tab out put because it was sending me one e-mail a minute.
I just can't figure out what's wrong with this one.
 
Old 01-08-2009, 09:29 AM   #4
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
it was sending me one e-mail a minute.
Any pointers in the emails?
 
Old 01-08-2009, 07:50 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
There's no need to background (&) the ping cmd either.
 
  


Reply

Tags
bash, cron, error, ping



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
help: using cron + my bash script --> don't want mail from script beeblequix Linux - General 7 11-23-2007 09:25 PM
Setting up cron to proccess a Bash Script to run every 15 minutes jamtech Programming 3 07-30-2007 03:32 PM
Run bash commands script as cron anjanesh Linux - General 4 04-03-2007 06:25 AM
bash script from cron kubla Programming 3 01-22-2004 04:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 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