LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2014, 11:53 AM   #1
sreeharsha1988
LQ Newbie
 
Registered: Apr 2014
Posts: 17

Rep: Reputation: Disabled
bash command for delay


Hi can any one please suggest me a command or procedure to create a delay between two commands in bash script.

I have been trying "sleep" but it is not working for delays in the order of micro seconds which I have verified using an oscilloscope. I am precisely trying to create a delay of 104 microseconds.
 
Old 06-02-2014, 11:59 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,211

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
Im not sure what a microsecond is in decimal format, but sleep will do that.

Code:
time (sleep 1)
time (sleep .1)
time (sleep .01)
time (sleep .001)
and so on.
 
Old 06-02-2014, 01:31 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,844
Blog Entries: 12

Rep: Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010
I'm not sure you can generate a precise delay in the microseconds range using a general purpose OS.
 
Old 06-02-2014, 01:43 PM   #4
djtoltz
Member
 
Registered: Nov 2003
Location: Eastern North Carolina, USA
Distribution: Mandrake
Posts: 51

Rep: Reputation: 20
sleep will put your thread back in the execution queue, allowing other applications to execute until the sleep time has expired. Sometime after that, the execution of your thread will resume. Unless you're using a real time OS (RTOS), you are not guaranteed to be given the CPU exactly when you expect. It depends on how many threads are executing and how heavy the load on your system.
 
Old 06-02-2014, 01:46 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,352

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
10E-6 or 0.000104

In addition since linux isn't a real time operating system precisely maybe a bit difficult.
 
Old 06-02-2014, 01:54 PM   #6
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,874

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
I'm sure I am wrong but as I understand it precise timing (of the type requested) isn't generally available? Perhaps a real time kernel?
For audio I think the move is to abandon the deliberately precise since the digits of precision are now so low?
Real Time kernels are still out there though then you just need to tame everyting else.,,

Last edited by 273; 06-02-2014 at 02:00 PM.
 
Old 06-02-2014, 01:57 PM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,712

Rep: Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747
This is not a RT OS. You won't come even close to accuracy of microseconds.

Even if you would use a RT OS, sleeping 104 usecs is nonsense. You can only do that if you are sure your task keeps executing. And usually you can't. With a normal time slice in the magnitude order of milliseconds you can't expect a task to wake up after 104 us.

The correct way how to do this is to program an interrupt timer and let it expire after exactly 104 us, taking into account the overhead of the interrupt handler.

Everything else (like polling the RT clock) has an inaccuracy of several time slices.

The amateur way to do this is to lock the task in running state, and make a delay loop in software. You'll get you timing, but it is the worst way conceivable.

jlinkels
 
Old 06-03-2014, 03:11 AM   #8
sreeharsha1988
LQ Newbie
 
Registered: Apr 2014
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jlinkels View Post
This is not a RT OS. You won't come even close to accuracy of microseconds.

Even if you would use a RT OS, sleeping 104 usecs is nonsense. You can only do that if you are sure your task keeps executing. And usually you can't. With a normal time slice in the magnitude order of milliseconds you can't expect a task to wake up after 104 us.

The correct way how to do this is to program an interrupt timer and let it expire after exactly 104 us, taking into account the overhead of the interrupt handler.

Everything else (like polling the RT clock) has an inaccuracy of several time slices.

The amateur way to do this is to lock the task in running state, and make a delay loop in software. You'll get you timing, but it is the worst way conceivable.

jlinkels
How can I create a loop for delay especially how do I know the time consumed by one iteration of a loop. Can you please tell me how to find out the time a simple loop takes to process.
 
Old 06-03-2014, 03:27 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,800

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
using bash and the date command you can get the current time by date +%s.%N (that is nanoseconds), so elapsed time can be calculated by a single subtraction
Code:
d1=$(date +%s.%N)
your_command
d2=$(date +%s.%N)
# calculate d2-d1
bc <<< "$d2 - $d1"
but it will not be really accurate and will not be reproducible at all.
 
Old 06-03-2014, 08:16 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,844
Blog Entries: 12

Rep: Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010
Quote:
Originally Posted by sreeharsha1988 View Post
How can I create a loop for delay especially how do I know the time consumed by one iteration of a loop. Can you please tell me how to find out the time a simple loop takes to process.
You cannot. What jlinkels is talking about is pure embedded programming like on a microcontroller; where there is no operating system running. You want 104 MICRO seconds you're never going to get anywhere near that using BASH or some general process loop. I would not recommend attempting to modify the kernel for this type of thing.

What are you trying to do? Example, say you're communicating serially to something, so say you're manually clocking bits; therefore the 104 uSec is about a 9600 bps clock rate. That what you're trying to do here? Or are you trying to clock bits for an I2C or SPI interface? If you absolutely have to generate a pulse or signal at this delay and the requirements are that it be repeatable, then sorry general purpose Linux is not going to be capable of helping you. Certainly you can take a stab at rewriting the kernel scheduler, probably doing a lot of assembly language programming, to attempt to attain this. Many would rather instead grab a hobby board containing an embedded microcontroller and doing what they need. The real question is what are the requirements here, clocking, signalling, or something else?
 
  


Reply

Tags
delay


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set boot delay using bash shell henryyao Linux - Newbie 4 08-22-2013 04:20 PM
[SOLVED] How to Delay output per letter in bash? jjjj12212 Programming 3 02-02-2012 11:29 PM
[SOLVED] Delay Problem With SU command z99 Linux - Newbie 3 06-09-2011 11:16 PM
bash script - I want some time delay between each command is executed bbmak Linux - Newbie 2 12-21-2007 04:35 AM
how-to: repeat OR iterate shell OR bash command delay OR interval admarshall Linux - General 5 07-18-2005 11:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration