LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 10-28-2004, 05:02 AM   #1
vinoth.ilango
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Rep: Reputation: 0
Unhappy Bash scripting


can anyone help on the following problem.


System Configuration : SunOS sun210 5.9

I have two scripts "Master and Restart" script.

- From the master script , I have to start, stop the restart script on certain conditions.

- To achieve the above functionality, I need to get the processID of the restart script. But when i try the following script I am not able to get the processId.

Sample Master Script
====================

./restart.sh &

while true; do
if [ check for some condition ]
then
restartId=`ps -ef | grep "restart.sh" | grep -v "grep" | sed /zap/d | cut -c10-15`
if [ -n "$restartId" ]
then
echo "Killing the restart script"
kill -9 $restartId
fi
fi
sleep 1
done




- When I manually tried to '$ Ps -ef | grep restart.sh' , I could not see any process running.

Is there any better and efficient way to do the above.

Thanks and Regards,

vinoth
 
Old 10-28-2004, 07:15 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
Does the sunOS have the 'killall' command?

I tried to duplicate what you did, but mine worked. However, I kept spelling 'resultId' as resultID.
-----
restart.sh

#!/bin/bash
while true; do echo "test" >testfile; done
-----

master.sh
#!/bin/bash

$(./restart.sh) &
ps
while true; do

restartId=`ps -ef | grep "restart.sh" | grep -v grep | sed /zap/d | cut -c10-15`
if [ -n "$restartId" ]
then
echo "Killing the restart script"
kill -9 $[restartId]
fi
sleep 3
done
-----
result
Code:
me@here:~> ./master.sh
  PID TTY          TIME CMD
 8301 pts/1    00:00:00 bash
14773 pts/1    00:00:00 master.sh
14774 pts/1    00:00:00 restart.sh
14775 pts/1    00:00:00 ps
Killing the restart script
  PID TTY          TIME CMD
 8301 pts/1    00:00:00 bash
14773 pts/1    00:00:00 master.sh
14774 pts/1    00:00:00 restart.sh
14782 pts/1    00:00:00 ps
./master.sh: line 15: 14774 Killed                  ./restart.sh

Last edited by jschiwal; 10-28-2004 at 07:17 AM.
 
Old 10-28-2004, 07:25 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,774

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
Solaris has pgrep and even pkill, so the whole block
Code:
restartId=`ps -ef | grep "restart.sh" | grep -v "grep" | sed /zap/d | cut -c10-15`
if [ -n "$restartId" ]
then
echo "Killing the restart script"
kill -9 $restartId
fi
can be simplified in
Code:
pkill restart.sh
Not sure about the /zap/ thing though, are there some restart.sh you want to keep ?
 
Old 10-29-2004, 01:21 AM   #4
vinoth.ilango
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for your replies, The script works fine.

I missed out the line '#!/bin/bash ', in my restart.sh.

So the script's ProcessID was not available when I did 'ps -ef | grep restart.sh'

when I added the line #!/bin/bash , I was able to get the processID and kill the process.

Again thanks,

Vinoth
 
Old 10-29-2004, 03:13 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,774

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
So you don't like my optimization ?
 
Old 10-29-2004, 04:28 AM   #6
vinoth.ilango
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Hi Jlliagre,

Your optimization doesn't work for the below script ( softwareRedundancyTest.sh) .

I don't know the reason. when I do pgrep the processId is not displayed. even pkill doesn't work.

softwareRedundancyTest.sh
=====================
#!/bin/bash

ps

while true;do
echo "Software Redundancy Test script..."
sleep 1
done

But now I observed that the same script , by renaming the filename it works. I renamed the script as 'soft.sh' . I am able to do pgrep and pkill . :-)

Is there anything to do with the filename length ??

Anyway thanks for your suggestion.

Please advice,

Vinoth
 
Old 10-29-2004, 04:41 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,774

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
Long names are indeed not matched by default, just use
pgrep -f softwareRedundancyTest.sh
to correct that.

- typo corrected

Last edited by jlliagre; 10-29-2004 at 04:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration