Python sleep(): Add Delay In A Seconds To Suspend Execution Of A Script

I am a new Python user. I would like to delay execution for five seconds in a Python script. How do I add a time delay in Python on Unix/Linux? Is there a sleep command in python like the Unix/Linux sleep utility to suspend execution of a bash script?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsPython time
You need to import and use a module called time. This module provides various time-related functions for Python users.


Python sleep syntax

The syntax is as follows
import time

The time.sleep() suspend execution of a script for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

Example: Put delay in a Python script

Create a script called

# The following will run infinity times on screen till user hit CTRL+C
# The program will sleep for 1 second before updating date and time again.
import time
print "*** Hit CTRL+C to stop ***"
## Star loop ##
while True:
        ### Show today's date and time ##
	print "Current date & time " + time.strftime("%c")
        #### Delay for 1 seconds ####

Save and close the file. Run it as follows:
$ chmod +x
$ ./

Sample outputs:

Fig.01: Python sleep() demo program output

Fig.01: Python sleep() demo program output


  1. Set an infinite loop using while True:
  2. Get the current date and time using strftime() and display on screen.
  3. Finally, add 1 second delay in a script using sleep(1).
  4. Continue this procedure till user interrupts.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

2 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.