Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Ruby script to delay execution of a command (intended for @reboot crontab) (See related posts)

# Start after delay

# Delay specifications must be one of Xs (secs), Xm (mins), Xh (hours)
case ARGV[0]
when /(\d+)s/
@delay = $1.to_i
when /(\d+)m/
@delay = 60 * $1.to_i
when /(\d+)h/
@delay = 3600 * $1.to_i
raise "Incorrect delay specification"

# A random factor of up to 10% will be added to any given delay
@rf = ( @delay * 0.1 ).to_i
@delay += rand( @rf )
sleep( @delay )

# Join up remaining arguments to form the command to be executed
ARGV.slice!( 0 )
@command = ARGV.join( ' ' )
exec( @command )

Comments on this post

nickstenning posts on Aug 04, 2005 at 01:06
Could you just put the code between < code >< /code > tags? (without the spaces of course)
zenspider posts on Nov 28, 2005 at 21:49
do not use @instancevariables when you don't need them. All of your @'s are superfluous and only go to confound things. Why ARGV.slice!(0) when a simple shift will suffice? Especially when you could do that up at the top instead of ARGV[0]?

Finally, this whole thing smells of overkill, when a simple "sleep 2700; rest_of_command" in your crontab would have sufficed.

You need to create an account or log in to post comments to this site.

Related Posts