theweaselking: (Default)
[personal profile] theweaselking
How do I put a delay smaller than a second into a shell script?

What I really want is "sleep 0.1" or something similar, that I can then adjust up and down to give myself fractions of a second.

Once I can do that, I want to be able to delay a random amount of time - say, between 0 and 1 second, evenly distributed.

And if I have to go to Perl to do it, so be it, let's go to Perl. In that case, how do I do it, and how do I make a call to a command-line script from within my Perl script?

EDIT: Done!
---usleep.c---
int main (int argc, char **argv) {
usleep(atoi(argv[1]));
return 0;
}
---usleep.c---

+ "gcc -o usleep usleep.c"
+ "mv usleep /usr/bin"
= a working usleep implementation for Ubuntu.

Of course, the fact that "sleep" takes floating point arguments in Ubuntu and I just didn't read that part because I'm dumb and made an assumption means this isn't necessary.

Remaining issue:

Shell script, ubuntu linux. I want a way to generate a random number between 0 and 1, so I can loop like this:

Loop {
Do Something
sleep (random number between 0 and 1)
}

Help me, lazyweb!

(no subject)

Date: 2008-04-11 07:56 pm (UTC)
From: [identity profile] scifantasy.livejournal.com
usleep allows you to specify sleep time in microseconds. Not necessarily perfectly accurate, but it should do the trick.

(no subject)

Date: 2008-04-11 08:10 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Excellent! Now, I'm working on a Ubuntu system. Where do I get usleep? The man pages online say that it's obsolete and I should use nanosleep instead, but that doesn't work on the existing things and doesn't appear anywhere in the packages I can find.

(no subject)

Date: 2008-04-11 08:15 pm (UTC)
From: [identity profile] scifantasy.livejournal.com
*shrug* According to Google-fu, Ubuntu doesn't come with it--suggestions include writing your own with the C usleep function.

(no subject)

Date: 2008-04-11 08:18 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Heh. I just did. :)

(no subject)

Date: 2008-04-11 08:18 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Never mind, coded my own in C.

(no subject)

Date: 2008-04-11 08:17 pm (UTC)
jerril: A cartoon head with caucasian skin, brown hair, and glasses. (Default)
From: [personal profile] jerril
In Perl you want to abuse the 4 argument version of select() and undefined arugments.

Leave the first three arguments as undef, and set the fourth to the fraction of a section you want it to sleep. The following sleeps for a quarter second.

select(undef, undef, undef, 0.25);

to execute a command line, you use the system() function (assuming you want the perl script to wait until the system call is done).

so if you wanted to call ls -d (for a random example, I don't even remember if -d is a valid argument on ls) you can do

@args = ("ls", "-d");
system(@args) == 0 or die "system @args failed: $?"

(this catches when your system call fails and prints out an error message, btw).

Each argument of the system call is another item in your @args array.

Your program will be executed in the current working directory.

(no subject)

Date: 2008-04-11 08:18 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
IT's okay, I rolled my own usleep binary.

(no subject)

Date: 2008-04-12 01:37 am (UTC)
From: [identity profile] skington.livejournal.com
Or just use Time::HiRes.

(no subject)

Date: 2008-04-11 08:19 pm (UTC)
From: [identity profile] novas007.livejournal.com
Seems okay here:
mike@whirl:~> for i in 1 5 9; do time /bin/sleep 0.$i; done
sleep 0.$i  0.03s user 0.05s system 42% cpu 0.179 total
sleep 0.$i  0.06s user 0.01s system 13% cpu 0.548 total
sleep 0.$i  0.05s user 0.03s system 8% cpu 0.946 total

mike@whirl:~> /bin/sleep --version
sleep (GNU coreutils) 6.10

(no subject)

Date: 2008-04-11 08:23 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Grr, the manual says you can't do that.

(no subject)

Date: 2008-04-11 08:23 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
No, wait, the manual does. I was reading the wrong manual.

I'm SPESHUL today.

Thanks!

(no subject)

Date: 2008-04-11 08:22 pm (UTC)
From: [identity profile] elffin.livejournal.com
You may wish to try something along the lines of repeatedly echoing some arbitrary string to /dev/null. It'd be the shell script equivalent to NOP.

I don't know what profiling tools Ubuntu has because I'm an ignorant dork. But, you could try doing, say, a printout of system time as the LONGINT and then ten thousand of the echoes, and another printout of the system time, and do that fifty times or so, and get an idea of how long it takes to do ten thousand of yer NOP and with a little math, voila.

(no subject)

Date: 2008-04-11 08:32 pm (UTC)
From: [identity profile] elffin.livejournal.com
Don't forget to use the catapult to fling the marble onto the Enter key when launching the script.

All but the first three replies occurred while I was writing mine. Damn.

(no subject)

Date: 2008-04-11 08:35 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
I actually have one of those desktop missile launchers. I'll set a cron job to fire it at the enter key at the right time!

(no subject)

Date: 2008-04-11 08:42 pm (UTC)
From: [identity profile] jwz.livejournal.com
I once had a machine that wouldn't reboot without hitting the enter key at some dumb prompt I couldn't turn off. I actually investigated whether a dippy-bird could solve this problem. (Answer: no.)

(no subject)

Date: 2008-04-12 12:35 am (UTC)
From: [identity profile] paoconnell.livejournal.com
Buy or borrow Knuth's Seminumerical Algorithms. He has a section on random number generators that are written either in pcode or in pseudo-assembler that are close to random (as opposed to pseudorandom). There may be newer algorithms books around. Don't trust built in rand() functions.

(no subject)

Date: 2008-04-12 01:05 am (UTC)
From: [identity profile] theweaselking.livejournal.com
Dude, I'm not doing cryptography, I'm not parsing credit card transactions, I'm not even playing a game. Built-in rand() is totally okay with me.

What's the syntax for Rand() in bash or sh, again?

(no subject)

Date: 2008-04-12 01:43 am (UTC)
From: [identity profile] dranon.livejournal.com
The bash man page says that you can use $RANDOM to get a random integer from 0 to 32767. You can use either the built-in math $(( ... )) or bc/dc with that depending on your needs.

(no subject)

Date: 2008-04-12 01:44 am (UTC)
From: [identity profile] skington.livejournal.com
Shell is a really fucked-up language.

This should do the trick:

sam@alchemist:~$ for var in 1 2 3 4 5; do echo 0.$(( $RANDOM * 100 / 32767 )); done
0.32
0.90
0.93
0.3
0.54

(no subject)

Date: 2008-04-12 03:45 pm (UTC)
From: [identity profile] quotation.livejournal.com
Better than rolling your own: http://www.koders.com/c/fid1E66F1500EB9E51937AEF238145CF85F8C24ADDD.aspx

Comes stock on most modern deadrat implementations as part of the initscripts RPM.

We have a programmer at work that couldn't figure out the usleep(3) call, and instead just did a while loop that watched the system clock -- with two loops running in two threads.. I'll call the app "Loopy."

Loopy happily ran on a 12x300MHz sun box for 6 years, and the programmer was so lauded for the business usefulness of the application that the programmer was promoted to VP, and then to Executive Director in that same time.

Well, after 6 years, the Sun box became unreliable. Loopy generates several million dollars of revenue every week, so, we had to put it on new hardware. The ED decided that since Loopy used 16.6% of the 12x300=3600MHz machine, it would run splendidly if recompiled for Linux, because a 2xDualCorex3000MHz machine would be 12,000MHz.

So, the 6-yr-old code was pulled out of the archive, and compiled fine on Linux. We rolled it out to production, and found that it used 50% of the CPU in the new 12,000MHz machine! Our rule is to double the hardware whenever we use 50% of the capacity.

But, the ED that wrote the code has now decided that Linux is incredibly inefficient and inappropriate for the task. We're switching back to Solaris.

Brilliant!

And I'm not allowed to say anything because this ED controls our budget and currently likes us.

Profile

theweaselking: (Default)theweaselking
Page generated Feb. 6th, 2026 01:09 am