i am trying to write a script on my *test machine *to check if the process are running , and if they are not need to start them any idea why this is not working ??
#!/bin/sh -x SERVICES= `ls -la /etc/init.d/ |awk '{print $9 }' |sed -e 's/[^[:alnum:]]//g'`
for i in $SERVICES do if [ $(ps -aux|grep -v grep |grep $i |wc -l) != 0 ]; then . /etc/init.d/$i start else echo " the service $i is running " fi done
On Sat, Mar 15, 2008 at 4:47 PM, Agnello George agnello.dsouza@gmail.com wrote:
i am trying to write a script on my *test machine *to check if the process are running , and if they are not need to start them any idea why this is not working ??
Try this:
#!/bin/sh -x
# In the line below, remove the space after = # Also, the file name is the 8th column, not 9th
SERVICES=`ls -la /etc/init.d/ |awk '{print $8 }' |sed -e 's/[^[:alnum:]]//g'`
for i in $SERVICES do
# It's not ps -aux, it's ps aux # Also, you might want to check for user # that is running the process as well.
if [ $(ps aux|grep -v grep |grep $i |wc -l) != 0 ]; then . /etc/init.d/$i start else echo " the service $i is running " fi done
Agnello George wrote:
i am trying to write a script on my *test machine *to check if the process are running , and if they are not need to start them any idea why this is not working ??
Can you post the errors?
1. ps doesn't require a '-' before parameters. You get a warning message if you do add it. Remove it. 2. $9 in awk gives nothing. Try $8 instead. 3. The if condition starts the service if the process is running. Huh?!? Shouldn't it be the other way around? 4. None of my business, but why do you want to start every service on the system?
Below is a modified version of the script. Works on my system, Fedora Release 8.
#!/bin/sh SERVICES=`ls -l /etc/init.d/ |awk '{print $8 }'` for i in $SERVICES do if [ $(ps aux|grep -v grep |grep $i |wc -l) != 0 ]; then echo RUNNING - $i else echo STOPPED - $i fi done
Agnello George wrote:
i am trying to write a script on my *test machine *to check if the process
*snip*
done
Also, this method is not fool proof. It shows a lot of running services as stopped, eg. iptables, netfs, smolt, wine, autofs, cpuspeed, etc.
On Saturday 15 March 2008 04:47 pm, Agnello George wrote:
there are several ready apps to monitor and restart processes like ps-watcher. use them.
On Saturday 15 Mar 2008 16:47:40 Agnello George wrote:
#!/bin/sh -x
Are you sure you want sh and not bash? sh is a good standard compliant practise, but if you're not going for portability, I'd highly recommend bash. Keep in mind, that on Debian/Ubuntu, sh is now linked to dash instead of bash.
SERVICES= `ls -la /etc/init.d/ |awk '{print $9 }' |sed -e 's/[^[:alnum:]]//g'`
awk and sed together is... hmm, a waste of resources. Two extra processes for no reason. Awk does everything sed can and sed does everything grep can, so stuff like `grep | sed' or `sed | awk' and vice versa is bad.
if [ $(ps -aux|grep -v grep |grep $i |wc -l) != 0 ];
This one is `ouch'. You need this:
http://wooledge.org:8000/ProcessManagement