Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script how to sleep in new process then execute a command

Tags:

linux

bash

So, I was wondering if there was a bash command that lets me fork a process which sleeps for several seconds, then executes a command.

Here's an example:

sleep 30 'echo executing...' & 

^This doesn't actually work (because the sleep command only takes the time argument), but is there something that could do something like this? So, basically, a sleep command that takes a time argument and something to execute when the interval is completed? I want to be able to fork it into a different process then continue processing the shell script.

Also, I know I could write a simple script that does this, but due to some restraints to the situation (I'm actually passing this through a ssh call), I'd rather not do that.

like image 947
David Mulder Avatar asked Mar 29 '12 23:03

David Mulder


People also ask

How do I sleep in a bash script?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.

How do you keep a shell script sleeping?

/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.

How do you delay a shell script?

Use the sleep command. Example: sleep . 5 # Waits 0.5 second.


Video Answer


1 Answers

You can do

(sleep 30 && command ...)& 

Using && is safer than ; because it ensures that command ... will run only if the sleep timer expires.

like image 78
Idelic Avatar answered Sep 24 '22 03:09

Idelic