1

I'm running JetPack 4.6.1 on a Jetson Nano, and a PHP script that I'm trying to run complains about there not being a usleep:

sh: 1: usleep: not found

The answers to Linux usleep: command not found, Why? suggest that it should be in initscripts (on RH) and Debian is apparently the same. However, initscripts has been depreciated and replaced by util-linux (or sysvinit-utils). I installed util-linux but still can't find usleep.

Indeed, looking at the file lists of all three packages (initscripts, util-linux and sysvinit-utils) does not reveal any usleep file.

This answer to Is there usleep for Ubuntu?, suggested installing BusyBox, which I did, but it didn't help. This answer to the same question potentially has a solution (buried within the nosql package), but before trying it, I'd rather just know where it should be, and why is it so difficult to locate? Has the command itself been deprecated?

1
  • 3
    I suspect the usleep command became irrelevant when the sleep command began accepting a real number argument, so the equivalent to usleep 2000 is sleep 0.002000. Commented Jul 20, 2022 at 20:45

2 Answers 2

2

The PHP could be updated to call sleep (of the GNU flavor) with floating point seconds; usleep "sleeps some number of microseconds" and there are a million of those in a second so given a $SleepStep of 100000

$ echo 100000 1000000 / p | dc
.1000

that's equivalent to a sleep 0.1 call. (The GNU sleep, not the other kind that do not take floating point values.)

Make usleep yourself

usleep(1) should not be too difficult to find the source for and manually compile, or to write; the most tricky part would be conversion of the input the user provides into a usleep(3) call value, a hopefully positive 32-bit integer.

perl -MTime::HiRes=usleep -e 'usleep shift' 100000

But this has about a 30 millisecond overhead on my system so ideally you would want a compiled version of usleep(1).

Non-Standard

usleep(1) is otherwise not standard (non-linux systems, probably, will not have it. They may not have GNU sleep, either, for that matter, or may instead call it gsleep.). A manual page indicates usleep was authored by some folks at RedHat; RedHat might decide to simply stop supporting it, and then who knows what various other linux vendors will do at that point. This is where using tools specified by POSIX can help.

Problems with Close-to-Zero Seconds in a Shell Script

Also sleeping for close-to-zero seconds in a shell script will become problematic especially on slower, very busy, or systems with poor time control (virts, perhaps). As the sleep delay approaches zero the overhead of the fork/exec of the usleep (or a fractional sleep call) will come to dominate the runtime. Brief sleeps would be far better handled within a process with a usleep(3) or similar system call. This will have much less overhead than forking out.

Internalize the Call

So, in the PHP, one might instead call usleep thusly:

exec ("v4l2-ctl -d {$Device} -c tilt_speed=1");
# TODO check for a v4l2-ctl non-zero exit here
usleep($SleepStep);
exec ("v4l2-ctl -d {$Device} -c tilt_speed=0");

this has two system(3) calls (what PHP calls exec) instead of one, but the portability of the sleep is improved.

3
  • I meant to mention in my question that I have actually ended up just calling sleep yes, and modifying the value to 0.1... I was just curious as to what had happened to usleep. Commented Jul 20, 2022 at 21:05
  • 2
    two system calls instead of one isn't any worse here anyway, since that one system call calls a shell that calls then three programs. Honestly, doing this through system instead of the appropriate exec tools actually necessitates all this string replacement – and system in PHP is still one of the nr 1 sources of unexpected (uninvited!) remote code execution, so, I'm honestly not a fan of that script, even if it in itself doesn't seem to have a security problem I can spot right away. Commented Jul 20, 2022 at 22:38
  • FWIW, I have posted your answer as a potential solution to a bug: usleep not available in ubuntu #1 Commented Jul 21, 2022 at 18:57
1

Part of your question seems to ask how to find a package with a utility command after some years have passed and the command may have been moved to another package (or there may have been several implementations and some are still available). For Debian and Ubuntu, this kind of search is made easier by their package search websites. I.e., https://packages.debian.org/ and https://packages.ubuntu.com/ respectively.

The customary upper search box named Search package directories, often won't help in this kind of search, but the lower search box named Search the contents of packages will. Clicking the checkbox/radio-button for files whose names end in the keyword is the key.

On both the Debian and Ubuntu sites this search turned up the packages gnulib, epic4-help, scheme9, and lmbench as having files that might be useful to you. Most likely lmbench - the others look like help text rather than executable code.

I don't know if distros with yum/rpm package repositories have similar search capabilities, but the Debian/Ubuntu ones have been very useful for me on some occasions.

3
  • 1
    yum whatprovides '*bin/usleep' on pre-dnf redhat systems
    – thrig
    Commented Jul 20, 2022 at 23:08
  • Thank you @thrig
    – Sotto Voce
    Commented Jul 21, 2022 at 2:26
  • There is a package meant to directly solve this issue apt-file search usleep. Of course you need to install apt-file and update it. Commented Jul 21, 2022 at 6:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .