Use the sleep command.
Example:
sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.
One can also employ decimals when specifying a time unit; e.g. sleep 1.5s
Use the sleep command.
Example:
sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.
One can also employ decimals when specifying a time unit; e.g. sleep 1.5s
For those looking for the Bash equivalent of Windows Powershell/CMD's pause command.
In Bash use read with option -p specifying a prompt like:
read -p "Press enter to continue"
The error is because & is being passed to the /bin/sleep command as an argument, whereas you are using it in the context of telling the shell to execute the command in the background.
If you want to use '&' or '&&' you can use os.system:
os.system('sleep 1 && echo foo && sleep 5 && echo bar')
or to run nonblocking (in the background) you can use the standard &:
os.system('sleep 1 && echo foo && sleep 5 && echo bar &')
You can't do what you are wanting to do, force a python script to run in the background every time. Python also can't run a task directly in the background. You might look into Threading, which might accomplish what you want, but without knowing why you want to go back to the command prompt, it's difficult. I think what you might want is a threading timer specifically, an example of which can be found here.
from threading import Timer
def hello():
print "hello, world"
t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
This code can help. It runs the python script in a random minute between the interval you want to. Also this bash script should be added to crontab file to be scheduled at 02.00PM.
#!/bin/bash
maxtime=$((4*60*60))
delay=
RANDOM%maxtime))
(sleep $((delay)); /usr/bin/python /path/to/yourscript.py) & #background the sleep process, then run your script
You could use cron to run the bash script @1400, and the bash script would have a random number of minutes to sleep less than 240 (4 hours * 60 minutes). When sleep runs out, call the python.
I assume you know about $RANDOM?
#!/bin/bash
sleep
RANDOM % 240 ))m
./mypython.py
To account for new constraints in the edit:
#!/bin/bash
sleep
RANDOM % 240))m
sleep
RANDOM % 60))
./mypython.py
Undoubtedly, whatever memory is consumed by Python and the startup of your script will stay in memory for the duration of the sleep, but since you have written the code you can organise things to minimise the memory usage until the sleep is over.
As to cpu performance, I'm sure that you will incur no overhead for the duration of the sleep.
At least the minimum memory of a python process will be used (if not swapped out, but swapping also slows your computer, uses disks, and should be avoided if that's a problem). Not sure if you can sleep in python before importing the needed modules.
However, you can just sleep randomly before starting your program:
crontab -l
* 0 * * * (sleep $((RANDOM\%50)); python program.py)
The \ is needed here since just % otherwise means newline in cronjobs. You also might need SHELL=/bin/bash in the top of the crontab to make $RANDOM available. Change to sleep $(($$\%50)) to use the PID if changing SHELL is not an option.