Sleep timer based on hostname


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sleep timer based on hostname
# 1  
Old 08-28-2012
Sleep timer based on hostname

Dear all

I have a bunch of hosts which I need to run a common script. I need a sleep timee which will delay the execution based on the hostname. Usually the hostname of the servers are host01, host02 .. host16 with which I cam up with this -

Code:
#!/bin/bash
sleeptimer=$(( $(hostname -s|tr -cd 0-9|sed -e 's/^0//') * 60)) 2> /dev/null
#echo $sleeptimer

if [ -z "$sleeptimer" ]
then
	sleeptimer=$RANDOM
	while [ $sleeptimer -gt 3600 ]; do
		sleeptimer=$RANDOM
	done

fi
#sleep $sleeptimer
echo $sleeptimer

The second line works perfectly if the hostname is as previously mentioned with names host01, host02 .. host16.

What if the hostname is simply a generic name without any numbers are the end, I thought of simply generating a number.

Problem is, when the script is executed, I get this error-message
Code:
line 2: * 60: syntax error: operand expected (error token is "* 60")

I've even added "2> /dev/null" at the end of line 2 and it stlll generate the above errors. This only happens on hosts with hostnames *without* tail-numbers

Works fine otherwise ;-)

Appreciate any inputs!

Last edited by deunan; 08-28-2012 at 06:11 AM..
# 2  
Old 08-28-2012
Code:
end_hostname=$(hostname -s|tr -cd 0-9|sed -e 's/^0//')
if [ -z "$end_hostname" ] 
then     
  sleeptimer=$RANDOM     
  while [ $sleeptimer -gt 3600 ]; do         
    sleeptimer=$RANDOM     
  done  
else 
  sleeptimer=$(($end_hostname * 60))
fi

This User Gave Thanks to delugeag For This Post:
# 3  
Old 08-28-2012
I would use something like this to generate random numbers (not in relation to host names):
https://www.unix.com/302688175-post6.html
Just leave the alphabetic characters out and adjust the bits in the head -c.
# 4  
Old 08-28-2012
Code:
sleeptimer=$(( $(hostname -s| awk '{if ( $0 ~ /[0-9]/) { print $0 } else { print "1" } }' | tr -cd 0-9|sed -e 's/^0//') * 60)) 2> /dev/null

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

2. Shell Programming and Scripting

Program based on hostname

Hi Can you help me get the script for below requirement. when i run below script ./script QM hostname script should be working in below here inputs are QM and hostname by taking inputs it should work like below QM=$1 if QM name ends with 1 $4 should be 51431 ends with ... (4 Replies)
Discussion started by: darling
4 Replies

3. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

4. Shell Programming and Scripting

Kill a Script based on the pid and sleep

I would want to run a code for 1 min and if it doesnt succeed in 1 min..I would want to exit it..I am using the following code...But the script is not going into my code part.It is waiting for 60 secs and then getting killed. The code which is in the while loop actually takes less than 60 secs...... (6 Replies)
Discussion started by: infernalhell
6 Replies

5. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

6. UNIX for Dummies Questions & Answers

Timer

is there a timer function in unix without using C? for example i want to display a message after 5 seconds how do i do that? (2 Replies)
Discussion started by: khestoi
2 Replies

7. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies
Login or Register to Ask a Question