Operating System - HP-UX
1745793 Members
3646 Online
108722 Solutions
New Discussion юеВ

Re: Shell script for days

 
SOLVED
Go to solution
Robert Legatie
Advisor

Shell script for days

Hello,

I need to ping a server daily but only on business days i.e. Monday to Friday. How would I do that.

Thanks in advance.
7 REPLIES 7
Pete Randall
Outstanding Contributor

Re: Shell script for days

With a cron entry like this:

00 05 * * 1-5 "/usr/sbin/ping servername -n 3"


Pete

Pete
Robert Legatie
Advisor

Re: Shell script for days

I need it in a shell script.
James R. Ferguson
Acclaimed Contributor

Re: Shell script for days

Hi Robert:

> I need it in a shell script

Pete gave you a shell script, albeit a commandline one. What's wrong with that?

If you insist, encapsulate his suggestion like this:

# cat ./myping
#!/usr/bin/sh
/usr/sbin/ping servername -n 3

...and make an entry in your 'crontab' like Pete suggested:

00 05 * * 1-5 /absolute_path_to/myping

...which runs a 0500 on Monday-Friday.

And since the output isn't redirected, you (the initiating user) will receive a mail message of the results.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Shell script for days

Then you'll need to do something with the date command to test the day of the week.

$ date +%u
4

Tells me it's Thursday. Skip 6 and 7 and you're all set.


Pete

Pete
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell script for days

Hi (again) Robert:

To add to Pete's suggestion, I would _not_ use:

# date +%u

...but rather:

# date +%w

...as in:

# WEEKDAY=$(date +%w)
# echo ${WEEKDAY}

The "%w" format returns values 0-6 for Sunday-Saturday, whereas "%u" returns 1-7 for Monday-Sunday.

The '0-6' coding is consistent with a crontask and is the more common expectation in coding.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: Shell script for days

Shalom,

Good all purpose date/day utility script.

http://mirrors.develooper.com/hpux/caljd-2.25.sh

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Shell script for days

>JRF: I would _not_ use: date +%u

There is such a thing as a comment. :-)