2

my shell script prints segmentation fault when it starts.

watchdog.sh :

#!/bin/bash
while true
do
    processCnt=`ps ax | grep $1 | grep -v grep | grep -v watchdog | wc -l`
    if [ $processCnt -lt 1 ]; then
        $1 
    fi  
    sleep 2
done

Script gets a program name as a parameter.
I also run this script 4 times with different parameter at the same time in run.sh.

run.sh :

./watchdog.sh hqservicedemon &
./watchdog.sh relayservicedemon &
./watchdog.sh rtspservicedemon &
./watchdog.sh httpcontrolservicedemon &

When I run run.sh I get error:

./watchdog.sh: line xx:  7316 segmentation fault (core dumped)

After some tests it looks like OK when running just one watchdog.sh in run.sh.
What am I doing wrong in shell script?
Thank you for any advice.

Edited: My demon has a problem, not a script. After fix demon, script works well.

1
  • 1
    Consider using pgrep instead of piping ps through grep. Commented Dec 8, 2014 at 7:26

1 Answer 1

9

it is probably one of your demon's that is seg faulting and not the script. It is finding that the demon is not running, executes it (maybe without proper arguments?) and seg faults. try to run the four

There are already many tools to keep those things alive without you writing a new script to manage it. If you are using a system that runs upstart you can do this very easily with upstart. other systems have similar ways to do this, i would not write it from scratch myself.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the quick reply. I'll check my demon and upstart tool.
Finally, I found my demon has problem, not script.
Sounds like a correct answer mark is forthcoming :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.