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.

  • 4
    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/… Dec 13, 2017 at 21:15
  • 3
    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.
    – ilkkachu
    Dec 13, 2017 at 21:17
  • actually its a script to check if MySQL daemon is running or not... so script takes less than a second to execute. Dec 13, 2017 at 21:24
  • 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 Dec 13, 2017 at 21:28
  • 2
    @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.
    – Thegs
    Dec 13, 2017 at 22:07

3 Answers 3


Use a script with the while loop and nohup.


while :; do
  sleep 1

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.

  • 2
    I did the same thing. It is running perfect... Thanks for the help ;) Dec 13, 2017 at 23:11

Based on the information you have given, the best answer is likely the simplest one. Use a shell script with a infinite loop and a 1 second delay.

# Program that checks if MySQL is running
while [ 1 = 1 ]; do
<your code here>
sleep 1

Have the script run outside a terminal. That way it runs in the background.

For a more specific answer, I'd need to know what command you use and what you do if it's not running. Eg. send a notification or email.

  • 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 Dec 13, 2017 at 21:34
  • 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.
    – TheNH813
    Dec 13, 2017 at 22:01
  • 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.
    – Bruce
    Dec 13, 2017 at 22:32

This routine will continuously run try.sh every second unless it is already running. If already running, it will pause until the former execution ends.


while true; do  
   if [[ ! $(pgrep -x /path-to-script/try.sh) -gt 1 ]]; then  
   sleep 1  

Execute this way:

nohup /path/to/loop_script.sh & > /dev/null

Then hit ^C to regain console.

Must use kill command to stop loop_script.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .