1833777 Members
1968 Online
110063 Solutions
New Discussion

Re: help with Cron job

 
SOLVED
Go to solution
Gus Mestousis
Frequent Advisor

help with Cron job

What would be the best way to go about writing a cron job that pings a node every 5 minutes and outputs the data to a log file?

Sure, let me just drop everything and work on your problem.
9 REPLIES 9
Peter Kloetgen
Esteemed Contributor

Re: help with Cron job

Hi Gus,

first command: crontab -e to edit the crontable

0,5,10,15,20,25,30,35,40,45,50,55 * * * * ping ip_of_host >> /directory/logfile_you_want

everything in one line, you can also do this with SAM( routine tasks ).

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Ian Dennison_1
Honored Contributor

Re: help with Cron job

command is as followsm,...

date >>/var/adm/logs/ping.log
ping [hostname] -n [count] >>/var/adm/logs/ping.log

where hostname is the host to ping, and count is the number of packets to send.

crontab entry,...

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/script

where /tmp/script is the script with the ping command.

Or else you can just enter the ping command straight on the cron line, instead of /tmp/script.

Hope this helps! Are you having problems with network connectivity or response time?
Building a dumber user
someone_4
Honored Contributor
Solution

Re: help with Cron job

I am thinking you are wanting to monitor servers?


This one will write to a file


#!/bin/sh
LANG=C
### The hostnames file is a file with all the hostsname you want
### to monitor in a column
HOSTNAME_FILE=hostnames
for host in $(cat $HOSTNAME_FILE)
do
ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL" >> host_down.log
fi
done


This one will send you an email

#!/bin/sh
LANG=C
HOSTNAME_FILE=hostnames
for host in $(cat $HOSTNAME_FILE)
do
ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL" | mailx -s "hostdown" richard
fi
done
Mark Greene_1
Honored Contributor

Re: help with Cron job

try this script:

#!/bin/ksh
# script: /usr/local/bin/ping_node
remote_node=xxx.xxx.xxx.xxx # ip address here
echo date
/usr/sbin/ping $remote_node -n3
exit $?

and launch it from cron like this:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/local/bin/ping_node >> /var/tmp/ping_node.log 2>&1

please keep in mind I've not tested this, caveat emptor and all that.

HTH
mark
the future will be a lot like now, only later
Gus Mestousis
Frequent Advisor

Re: help with Cron job

All the solutions were great.
Thanks for your fast replies, and great info.

The purpose of the cron job is to monitor a line. There is 2 nodes that I have to ping, andhave been getting complaints about. i just want to have a log with line info, by sending different size packets across, and seeing what the connection is.

Thanks again!
Sure, let me just drop everything and work on your problem.
someone_4
Honored Contributor

Re: help with Cron job

Ohh yeah .. add the path to mine at the top..

#!/usr/bin/sh
PATH=/usr/sbin/ping:/usr/bin/:${PATH}
export PATH

Thoose paths always get you when you are doing cron scripts.

Richard
Sanjay_6
Honored Contributor

Re: help with Cron job

hi Gus,

you can have this entry,

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/sbin/ping host_name -n count >/your_log_file 2>&1

you can set the count as 5 so that the ping is tried 5 times and then it exits.

Hope this helps.

regds
Patrick Wallek
Honored Contributor

Re: help with Cron job

Probably what would be better to do is:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * ping host_name -n 2 >> logfile 2>&1

So that both stderr and stdout go to the logfile
thebeatlesguru
Regular Advisor

Re: help with Cron job

following is my cronfile:
03 11 * * * /usr/bin/ok >> /home/pin/ok.log 2>&1
0,5,10,15,20,25,30,35,40,45,50,55 * * * * ping 202.96.232.8 -n 2 >> /home/pin/ping.log 2>&1

/usr/bin/ok is a shell script
i find second mission is done,and i can find ping.log,but first mission is not done,i cant find ok.log.
why?

hihi