How can I run a particular script every second?

10,438

Use a script with the while loop and nohup.

the_script.sh:

while :; do
  /path-to-script/try.sh
  sleep 1
done

Run the_script.sh immune to hangups:

nohup /path/to/the_script.sh > /dev/null

Replace /dev/null with some file path if you care what's in the stdout.

Share:
10,438

Related videos on Youtube

Prashant Luhar
Author by

Prashant Luhar

Mantra for life = Code game sleep repeat; I have been passionate about technology since I was a young child and ‘I am hardworking as well as detail-oriented person.

Updated on September 18, 2022

Comments

  • Prashant Luhar
    Prashant Luhar almost 2 years

    I have tried using crontab, but it is limited to minutes. Is there any other option?

    I have also tried something like:

    watch -n 1 sh /path-to-script/try.sh
    

    But whenever I close the terminal it stops. I want something which continuously works in the background.

    • George Vasiliou
      George Vasiliou over 6 years
      I would focus on using & at the end of the command and also would read some about disown and nohup - see here for details unix.stackexchange.com/questions/3886/…
    • ilkkachu
      ilkkachu over 6 years
      Starting every second or starting a second after the previous one stopped? The latter is trivial, the first is a bit tougher. In the first case, you'd also want to consider what to do if one invocation takes longer than a second for some reason.
    • Prashant Luhar
      Prashant Luhar over 6 years
      actually its a script to check if MySQL daemon is running or not... so script takes less than a second to execute.
    • Prashant Luhar
      Prashant Luhar over 6 years
      I read about disown and nohup. So if I destroy the terminal on which the script is running it will destroy the program as well. @GeorgeVasiliou
    • Thegs
      Thegs over 6 years
      @PrashantLuhar not with nohup, no. Using nohup [command] & makes the command ignore the terminal's attempts to kill child processes upon the terminal's logout.
    • XPMai
      XPMai over 4 years
      I recommend using that command in conjunction with screen, see: linux.die.net/man/1/screen
  • Prashant Luhar
    Prashant Luhar over 6 years
    Actually I have tried this too, but whenever I close the terminal it terminates the script..so the real problem here is how to run the script in background. And yes if its not running I send an email using mailx command
  • TheNH813
    TheNH813 over 6 years
    Put the script code in ~/.local/bin/notifymysqldown or /usr/bin/notifymysqldown and add it to one of the login startup scripts like this "notifymysqldown&". Whenever you log in the command will execute, in the background without a terminal window. To kill it off you'd need to open a task manager and look for notifymysqldown.sh and end it. You'd also want to add a if then else section so the script pauses a few minuted if it does go down so it doesn't send a email every second.
  • Bruce
    Bruce over 6 years
    Run this bash script in background, like ./script.sh &, and then run command disown, then you can safely close your terminal. Another way is using screen.
  • Prashant Luhar
    Prashant Luhar over 6 years
    I did the same thing. It is running perfect... Thanks for the help ;)