Episode: 4155 Title: HPR4155: GNU sleep tips Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr4155/hpr4155.mp3 Transcribed: 2025-10-25 20:28:02 --- This is Hacker Public Radio Episode 4155 for Friday 5 July 2024. Today's show is entitled, New Sleep Tips. It is hosted by Delta Ray and is about 10 minutes long. It carries a clean flag. The summary is, Delta Ray gives an overview of the sleep command and some uses for it. You are listening to a show from the Reserve Q. We are airing it now because we had free slots that were not filled. This is a community project that needs listeners to contribute shows in order to survive. Please consider recording a show for Hacker Public Radio. Ah, sleep. Isn't sleep great? Wouldn't you like to sleep all the time? Especially when you can pass decimal numbers to it and use it to annoy your friends. Wait, what? No, not that kind of sleep. I mean GNU sleep, which is a command that can be used in the shell to delay execution before or after a command. Hi, I'm Delta Ray, creator of CLI Magic on Mastodon and Twitter. I use the sleep program all the time. You can try it too, simply run sleep space 5 and you will see it return your command prompt after 5 second delay. But what uses that? Well, using the semicolon to divide a command into sections, you can sleep for a set amount of time before running another command. Maybe you want to get a notification after a certain amount of time has passed. Say an hour. You could pass 3,600 to it as the number of seconds an hour. But modern versions of GNU sleep are more robust and can take unit measurements in their arguments. So for an hour, you can just pass it 1H as the argument to sleep with sleep space 1H. You can also use compound times such as two hours and 45 minutes by just adding the additional divisions as additional arguments. So sleep space 2H space 45M for minutes. You could use sleep as a Pomodoro timer, which is an Italian 25 minute timer that looks like a tomato in order to keep you focused. This could simply be a matter of sleeping for 25 minutes and then running some other kind of alert program, such as the socks package to run playspacebell.wave or something similar to that. So you would put in a command of sleep space 25M, semicolon, spaceplayspacebell.wave. Of course a true Pomodoro timer will make a ticktock sound to help you focus. While it wouldn't use the sleep command, you could use the socks play command to repeatedly play a ticktock sound for 25 minutes. The command for that is in the show notes, a little bit complex. Speaking of notifications, there is also a nice program you can use to notify you through the desktop under Linux called Notify-Send. You will submit a message into the notification demon so that it shows up similar to other notifications show up, such as when you need to restart your computer after doing system updates or if you connect your Bluetooth headphones to your computer and other notifications. To use Notify-Send, simply type in Notify-Send-Space and then in double quotes put the message you want to show. When you hit Enter, it should send a message and display it somewhere on your screen, probably up in the upper right hand corner. So if you order to pizza, but you still want to work for a bit, but you don't want to forget that you order to pizza, you might set up a sleep timer to notify you in 15 minutes that you need to go pick up the pizza. So you'd put sleep space 15M, then semicolon, notify-send, space, and double quotes pick up the pizza. The sleep command is especially useful in shell loops such as a four loop that runs a command that you don't want to run as quickly as possible or a while loop that you don't want the command to run as quickly as possible, but you want to have a little bit of a delay. For instance, if you have a temperature probe hooked up to your computer and you want to just watch the temperature every 10 minutes, not like every single second. So you use a while loop and a sleep command to create an internal display. So you have while, space, temperature probe command, semicolon, do, space, sleep, space 10M, semicolon, done, and then it will run temperature probe every 10 minutes. You might also want to download a bunch of files from a website, but don't want to overload the website with your requests. While some of the programs like Curl and W get do have delay commands as options, you can also use sleep in the loop. This is especially good if you use the dollar random variable, like the random variable enbashed at a bit of random time to your sleep commands so that it looks a bit more like human behavior. The random variable gives you a random number between 0 and 32,767, which is 2 to the 15th, minus 1. But you can limit the upper and lower bounds using a bit of show arithmetic and the modulus operator. For instance, if you want to just have a number between 0 and 5, you can use dollar open parentheses twice, random, percent, 6, and then close parentheses twice. This is basically the show arithmetic for doing a modulo division of random modulo 6. You have to use a number one higher than the top of the range in the modulo arithmetic if you want to include the boundary number itself. So if you want to include five, you have to divide by six. And if you want to increase the bottom boundary number, you can just add whatever you want, the bottom boundary to be to the result of the modulo operation. So as an example, the following command will loop through a list of URLs in a file, running the WGet command on each iteration of the loop, and then sleeping a random value between 30 and 60 seconds. So you'd say four space URL, space in, and then dollar parentheses, cat URLs dot txt, close parentheses, semicolon, do, space, WGet, space, dollar URL in double quotes, semicolon, sleep, and then dollar double open parentheses, random, percent, 31 plus 30, double close parentheses, semicolon, done. As an April Fool's gag, you could implement a poor man's annoy a tron that plays a high-pitched noise at 9,000 hertz every three to six minutes using the SOX play command. The command for this is in the show notes. So basically you say, while true, do play the sound, and then semicolon, sleep, double open parentheses, random, percent, four plus three, double parentheses, close, M for the minutes, and then semicolon, done. If you ever want to animate something to do an action that requires a set frame time so that the eye has a chance to see it, or so that the system has a chance to register a mouse move in or key press or something like that, you'll need to use millisecond length sleeps. Fortunately, GNU sleep now accepts millisecond or sub-second length sleeps. It actually didn't for a long time up until around 2010 or something like that, and there was another program that you had to use called sleep, ENC, which is stood for sleep enhanced or sleep in H, something like that, but later they enhanced the GNU sleep command to accept sub-second delays, which is really nice. So for instance, if you wanted to do something at a rate of 30 frames per second, you need to use a sleep value of 0.0333333, of course, repeating forever, but you can just put in three threes, or maybe a little less considering how much processing you have to do per frame. In other words, if the amount of time it takes to run the command is significant, you might have to decrease the delay time so that if you want to really meet that 30 frames per second. I have a bunch of examples of this type of thing on the CLI magic account, and I'll include a link in the show notes to a list of the posts I've made over the years that include sleep commands in them. But some examples are things like making a rainbow in the shell or moving the mouse in a spiral and stuff like that. I hope I've given you some ideas for how you can use the sleep command in your daily life. There are, of course, many, many more uses for this simple command. If you have some of your own ideas or uses that you'd like to share or encourage you to share in a comment on the HPR website, also, HPR can use your help. We're running low on shows and we really need new voices to participate. See the website link give shows for more details. Thanks, and take care. You have been listening to Hacker Public Radio at HackerPublicRadio.org. Today's show was contributed by a HPR listener like yourself. If you ever thought of recording podcasts, then click on our contribute link to find out how easy it really is. Posting for HPR has been kindly provided by an onsthost.com, the Internet Archive and our syncs.net. On this advice status, today's show is released under Creative Commons, Attribution 4.0 International License.