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
.
Related videos on Youtube
![Prashant Luhar](https://lh6.googleusercontent.com/-f4lOSQzA0ko/AAAAAAAAAAI/AAAAAAAAQD4/3OO-AyInblc/photo.jpg?sz=256)
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, 2022Comments
-
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 over 6 yearsI would focus on using
&
at the end of the command and also would read some aboutdisown
andnohup
- see here for details unix.stackexchange.com/questions/3886/… -
ilkkachu over 6 yearsStarting 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 over 6 yearsactually its a script to check if MySQL daemon is running or not... so script takes less than a second to execute.
-
Prashant Luhar over 6 yearsI read about
disown
andnohup
. So if I destroy the terminal on which the script is running it will destroy the program as well. @GeorgeVasiliou -
Thegs over 6 years@PrashantLuhar not with
nohup
, no. Usingnohup [command] &
makes the command ignore the terminal's attempts to kill child processes upon the terminal's logout. -
XPMai over 4 yearsI recommend using that command in conjunction with screen, see: linux.die.net/man/1/screen
-
-
Prashant Luhar over 6 yearsActually 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 over 6 yearsPut 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 over 6 yearsRun this bash script in background, like
./script.sh &
, and then run commanddisown
, then you can safely close your terminal. Another way is usingscreen
. -
Prashant Luhar over 6 yearsI did the same thing. It is running perfect... Thanks for the help ;)