LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-13-2020, 05:47 PM   #1
bomberb17
Member
 
Registered: Jul 2005
Posts: 49

Rep: Reputation: 0
Cron script for checking web server availability


I have written the following script for checking that my server is online:

Code:
 #!/bin/bash

nc -w 2 -z -v my.server.web.address 8080 2>&1 | grep 'succeeded' > /dev/null
if [ $? == 0 ]; then
    echo OK
else
    echo FAIL
    /usr/sbin/ssmtp myemail@gmail.com < /home/user/emailalert.txt
fi
When I am logged in and test the above script, everything works as it should.
However if I execute this script in a cronjob, it always returns "FAIL" (and emails me an alert), even if the server is up.
Any ideas of what could be wrong?
 
Old 06-13-2020, 06:41 PM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,366

Rep: Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453Reputation: 1453
It could be a number of things. I notice

#1 the #! line is indented, meaning that the shell executing may not be bash or may be bash in POSIX mode. There should be no characters leading the #!

#2 you do not set the environment for the script, including the path. That may cause no problem for some scripts, but if you want to ensure the execution you expect either set the path or make all calls to external programs using the full path.

#3 your script does no logging, so any internal error may be logged where it is relatively hidden from you, or suppressed entirely. Some kind of logging is always a reasonable precaution.

Consider those, and if that does not lead you to a solution it should AT LEAST lead you to additional information that might help us advise you.
 
2 members found this post helpful.
Old 06-13-2020, 10:36 PM   #3
bomberb17
Member
 
Registered: Jul 2005
Posts: 49

Original Poster
Rep: Reputation: 0
Thanks for the tips, yes I was sloppy and didn't notice the indentation, and I also enabled logging to file. For now it seems to be working, maybe the indentation was the reason after all! Will come back if anything changes
 
Old 06-14-2020, 04:06 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,976
Blog Entries: 9

Rep: Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240
BTW, you can also do this:
Code:
#!/bin/bash

if nc -w 2 -z -v my.server.web.address 8080 2>&1 | grep 'succeeded' > /dev/null
then
    echo OK
else
    echo FAIL
    /usr/sbin/ssmtp myemail@gmail.com < /home/user/emailalert.txt
fi
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Cron directories: /var/spool/cron/cron.GfqqGO Woodsman Slackware 4 05-11-2011 02:37 PM
cron.hourly cron.weekly cron.monthly and 0anacron. Are they necessary? glore2002 Debian 2 09-30-2009 08:57 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 01:57 AM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 01:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:04 AM.

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