Linux Shell Script For Process Monitoring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7207248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell Script For Process Monitoring
提问by Tobis
This
This
#!/bin/bash
if [ `ps -ef | grep "91.34.124.35" | grep -v grep | wc -l` -eq 0 ]; then sh home/asfd.sh; fi
or this?
or this?
ps -ef | grep "91\.34\.124\.35" | grep -v grep > /dev/null
if [ "$?" -ne "0" ]
then
sh home/asfd.sh
else
echo "Process is running fine"
fi
Hello, how can I write a shell script that looks in running processes and if there isn't a process name CONTAINING91.34.124.35 then execute a file in a certain place and I want to make this run every 30 seconds in a continuous loop, I think there was a sleep command.
Hello, how can I write a shell script that looks in running processes and if there isn't a process name CONTAINING91.34.124.35 then execute a file in a certain place and I want to make this run every 30 seconds in a continuous loop, I think there was a sleep command.
回答by Kheldar
Use cron for the "loop every 30 seconds" part.
Use cron for the "loop every 30 seconds" part.
回答by roymustang86
Either way is fine, you can save it in a .sh file and add it to the crontab to run every 30 seconds. Let me know if you want to know how to use crontab.
Either way is fine, you can save it in a .sh file and add it to the crontab to run every 30 seconds. Let me know if you want to know how to use crontab.
回答by Benoit
Try this:
Try this:
if ps -ef | grep "91\.34\.124\.35" | grep -v grep > /dev/null
then
sh home/asfd.sh
else
echo "Process is running fine"
fi
No need to use test
. if
itself will examine the exit code.
No need to use test
. if
itself will examine the exit code.
回答by Matteo
you can't use cron since on the implementation I know the smallest unit is one minute. You can use sleep but then your process will always be running (with cron it will started every time).
you can't use cron since on the implementation I know the smallest unit is one minute. You can use sleep but then your process will always be running (with cron it will started every time).
To use sleep just
To use sleep just
while true ; do
if ! pgrep -f '91\.34\.124\.35' > /dev/null ; then
sh /home/asfd.sh
fi
sleep 30
done
If your pgrep
has the option -q
to suppress output (as on BSD) you can also use pgrep -q
without redirecting the output to /dev/null
If your pgrep
has the option -q
to suppress output (as on BSD) you can also use pgrep -q
without redirecting the output to /dev/null
回答by Shawn Chin
First of all, you should be able to reduce your script to simply
First of all, you should be able to reduce your script to simply
if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
To run this every 30 seconds via cron (because cron only runs every minute) you need 2 entries - one to run the command, another to delay for 30 seconds before running the same command again. For example:
To run this every 30 seconds via cron (because cron only runs every minute) you need 2 entries - one to run the command, another to delay for 30 seconds before running the same command again. For example:
* * * * * root if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
* * * * * root sleep 30; if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
To make this cleaner, you might be able to first store the command in a variable and use it for both entries. (I haven't tested this).
To make this cleaner, you might be able to first store the command in a variable and use it for both entries. (I haven't tested this).
CHECK_COMMAND="if ! pgrep '91\.34\.124\.35' > /dev/null; then ./your_script.sh; fi"
* * * * * root eval "$CHECK_COMMAND"
* * * * * root sleep 30; eval "$CHECK_COMMAND"
p.s.The above assumes you're adding that to /etc/crontab
. To use it in a user's crontab (crontab -e
) simply leave out the username (root
) before the command.
p.s.The above assumes you're adding that to /etc/crontab
. To use it in a user's crontab (crontab -e
) simply leave out the username (root
) before the command.
回答by Ranjithkumar T
You can save your script in file name, myscript.sh
You can save your script in file name, myscript.sh
then you can run your script through cron,
then you can run your script through cron,
*/30 * * * * /full/path/for/myscript.sh
or you can use while
or you can use while
# cat script1.sh
#!/bin/bash
while true; do /bin/sh /full/path/for/myscript.sh ; sleep 30; done &
# ./script1.sh
Thanks.
Thanks.
回答by podshumok
I would suggest using watch
:
I would suggest using watch
:
watch -n 30 launch_my_script_if_process_is_dead.sh
回答by BillyBigPotatoes
I have found deamonizing critical scripts very effective.
I have found deamonizing critical scripts very effective.
http://cr.yp.to/daemontools.html
回答by jschnasse
You can use monit
for this task. See docu. It is available on most linux distributions and has a straightforward config. Find some examples in this post
You can use monit
for this task. See docu. It is available on most linux distributions and has a straightforward config. Find some examples in this post
For your app it will look something like
For your app it will look something like
check process myprocessname
matching "91\.34\.124\.35"
start program = "/home/asfd.sh"
stop program = "/home/dfsa.sh"
If monitis not available on your platform you can use supervisord.
If monitis not available on your platform you can use supervisord.
I also found this question very similar Repeat command automatically in Linux. It suggests to use watch
.
I also found this question very similar Repeat command automatically in Linux. It suggests to use watch
.