Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Deferred execution of the AT command was mentioned earlier. It can delay execution of a command only once. This article looks briefly at another very unpopular command, sleep, which can also delay the current action for a while.

Before using the sleep command, let’s look at the following set of commands.

$touch new.file; rm new.fileCopy the code

Multiple commands separated by semicolons (;) are executed one by one. That is, the preceding commands are executed after the preceding commands are executed. The results of the preceding commands do not affect subsequent operations. This approach is similar to, but not pipe operations, where the output of the operation of the previous command is used as input for the execution of the following command. The following figure shows the result of executing this command.

The output indicates that the file was indeed created firstnew.fileAnd delete the file. A confirmation message is displayed before the operation.

What if we don’t want to delete files right away and want to delay execution for a while?Use the sleep command.

$touch new.file; sleep 30; rm new.fileCopy the code

The script above says create a new file, suspend it for 30 seconds, and then delete it. Isn’t it easy to use? The syntax is as follows.

sleep [--help] [--version] number[smhd]
Copy the code

By default, the value number is in seconds and can be omitted. We can also add the units m h D for “minutes”, “hours”, and “days”. For example, the script can be changed to

$touch new.file; sleep 30m; rm new.fileCopy the code

After a file is created, pause the file for 30 minutes before deleting it.

These days it’s all the rage to learn nonsense, and we can write an interesting script.

$date; sleep 30m; dateCopy the code

The current time will be displayed after 1 minute delay, and then the time will be displayed again. Is gilding the lily!


If you haven’t seen enough, the next article will cover the interesting use of the crontab command.