while true; do foo; sleep 2; done
By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.
$ while true
> do
> echo "hello"
> sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do echo "hello"; sleep 2; done
Answer from Stefano Borini on Stack Overflowwhile true; do foo; sleep 2; done
By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.
$ while true
> do
> echo "hello"
> sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do echo "hello"; sleep 2; done
It's also possible to use sleep command in while's condition. Making one-liner looking more clean imho.
while sleep 2; do echo thinking; done
Videos
With the & inside the loop it will start a new process in the background and as fast as it can do it again without waiting for the first process to end. Instead I think you want to put the loop into the background, so put the & on the loop itself like
while /bin/true; do
something_in_the_background
done &
# more stuff
while : ; do something ; done &
Earlier Bourne shells didn't have
trueandfalseas built-in commands.truewas instead simply aliased to:, andfalseto something likelet 0.&at the end of the line backgrounds the process:is the null command, as described by "help :":No effect; the command does nothing. Exit Status: Always succeeds.
You can stop and put your job in background while it's running using ctrl+z. Then you can kill your job with:
$ kill %1
Where [1] is your job number.
Check the exit status of the command. If the command was terminated by a signal the exit code will be 128 + the signal number. From the GNU online documentation for bash:
For the shell’s purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status.
POSIX also specifies that the value of a command that terminated by a signal is greater than 128, but does not seem to specify its exact value like GNU does:
The exit status of a command that terminated because it received a signal shall be reported as greater than 128.
For example if you interrupt a command with control-C the exit code will be 130, because SIGINT is signal 2 on Unix systems. So:
while [ 1 ]; do COMMAND; test $? -gt 128 && break; done
You can use an infinite loop in bash:
while true ; do
# Your code here.
sleep 30;
done
You can also schedule a periodic run of the checking program by cron.
Yes, you need an infinite loop with a sleep of 30 seconds. The following snippet will do:
#!/bin/bash
while true
do
# do any stuff you want
echo "doing my thing"
# sleep for 30 seconds
sleep 30
done
But I think that you will soon find that doing it in a bash script is probably not what you want to do. Tasks like this usually require some form of a daemon.
To answer your modified question, here is a variation of your script that should work as expected:
#!/bin/bash
while true
do
clear
size=$(ls -l /var/spool | wc -c)
sleep 30
newsize=$(ls -l /var/spool | wc -c)
if [ $size -lt $newsize ]
then
echo "You've got mail!"
else
echo "Sorry no mail yet"
fi
sleep 30
done
If you get a blinking cursor you are probably not actually in a loop. You are probably expecting input. Try Ctrl+D. If that doesn't work then open a new terminal and ps aux | grep command where command is the name of the script you wrote and then kill the pid that is returned.
As suggested by this answer or this more detailed one to similar questions on unix.stackexchange.com :
- Press Ctrl+Z to stop the job, and send it to the background
kill %%to kill the "current" or "last stopped" job