4

I have a few cleanup commands to run for my datacaching scheme. I was thinking a bash script would be an easy way to check if the full clean up job needed to be run. But the cache is very time sensitive, so the check script needs to run every second. What's the best way to do this?

Crontab only goes down to minutes, which makes sense for most applications.

Can I install a bash script as a background service?

On second though, can I just start it with the & command and make it loop forever with a one second sleep?

4 Answers 4

11

The simplest way is watch command. You pass number of seconds to -n option:

 watch -n1 'rand'
5
  • interesting way to do it. I've never heard of watch. What if the scrip takes longer than a second to execute?
    – Beachhouse
    Jan 9, 2013 at 19:04
  • 2
    According to manpage: "Normally, this interval is interpreted as the amout of time between the completion of one run of command and the beginning of the next run. However, with the -p or --precise option, you can make watch attempt to run command every interval seconds." So it should do fine.
    – Nykakin
    Jan 9, 2013 at 19:17
  • is 'rand' the name of the script? so I would execute: & watch -n1 'mem.sh'
    – Beachhouse
    Jan 9, 2013 at 19:20
  • rand is command returning random number. Since you want to execute script you have to set up PATH variable or pass a path like $ watch -n1 './test.sh'
    – Nykakin
    Jan 9, 2013 at 19:27
  • If you want it to run in the background even if you leave the terminal you could try nohup watch -n5 'bash script.sh' &
    – jamietelin
    Aug 5, 2015 at 20:10
7

use sleep . try this while true ; do ./your-script & ; sleep 1; done

7
  • ok, let me edit it and I'll post the full script in a bit.
    – Beachhouse
    Jan 9, 2013 at 17:13
  • Where would I put the 'while true ; do ./your-script & ; sleep 1;' done?
    – Beachhouse
    Jan 9, 2013 at 17:14
  • put it in another script and run it as background process Jan 9, 2013 at 17:19
  • why have two scripts?
    – Beachhouse
    Jan 9, 2013 at 17:20
  • @Beachhouse you can run it by itself or you can put that into a startup script so it starts when the box comes up.
    – h3rrmiller
    Jan 9, 2013 at 17:20
0

Using the following shell script (I’ll call it repeat-cleanup.sh), you could repeatedly run your code with a 1-second delay between each execution.

while true; do
  # Your cleanup code here, or execute a script containing said code
  sleep 1
done

Execute it using double-forking:

( ./repeat-cleanup.sh & )

Alternatively, you can skip the script altogether and start it with a one-liner:

( while true; do ./cleanup.sh; sleep 1; done & )

Explanation

Neither method runs the script every second. Instead, they wait a second between each execution of the script. As long as the script doesn’t take more than a few hundred milliseconds, that shouldn’t be an issue.

Double-forking disowns the process, allowing it to keep running even if you close your shell. That won’t work if you simply background the process with &.

I’ve tested both methods with a trivial script on Alpine Linux, and they worked consistently.

-1

I am using something like that:

do-the-job;
sleep 1; // wait one second till next run
./script-name &; // run same script again on background

But I don't know if it is correct :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.