5

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.

Time4Tea
  • 2,288
  • 5
  • 23
  • 54
Prashant Luhar
  • 163
  • 1
  • 1
  • 8
  • 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 https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and – George Vasiliou Dec 13 '17 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 '17 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. – Prashant Luhar Dec 13 '17 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 – Prashant Luhar Dec 13 '17 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 '17 at 22:07
  • scripts take less than a second, until they don't, and then things can get really ugly – thrig Dec 13 '17 at 22:25
  • I recommend using that command in conjunction with screen, see: https://linux.die.net/man/1/screen – XPMai Sep 26 '19 at 03:03

3 Answers3

7

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.

NarūnasK
  • 2,276
  • 4
  • 25
  • 35
0

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.

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

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.

George Udosen
  • 1,807
  • 13
  • 26
TheNH813
  • 109
  • 6
  • 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 – Prashant Luhar Dec 13 '17 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 '17 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 '17 at 22:32
0

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.

loop_script.sh: 




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

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

DanieleGrassini
  • 2,769
  • 5
  • 17
John
  • 1