Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bash Script to Start service in AIX

Posted on 2013-05-25
11
Medium Priority
?
710 Views
Last Modified: 2013-05-25
I am trying to automate the service restart whenever it is inoperative mode but it is giving error. Current Status of sendmail service is inoperative.

Script

if [[! -z `lssrc -s sendmail|grep inoperative|awk '{print $3}'` == 'inoperative' ]]
then
startsrc -s sendmail
else
echo "Hello"
fi


Error
testscript.sh[2]: [[!:  not found.
Hello
0
Comment
Question by:oraclescsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39196226
The basic mistake is the missing space between "[[" and "!". Correct syntax:

if [[ ! -z ...

But besides that your script is a bit over-engineered. Why not just

if lssrc -s sendmail | grep -q "inoperative" ; then
    startsrc -s sendmail
      else
         echo "sendmail is active"
fi
0
 

Author Comment

by:oraclescsa
ID: 39196230
I have used your script but actually it is starting the sendmail service even though

it is in "active" status. I want to start the service only if it is inoperative mode.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39196237
Please post your version!

The script works for me exactly as designed.

Don't get confused by the message "sendmail is active". As you can see, this message comes from my script and not from SRC.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:oraclescsa
ID: 39196240
AIX Version is 5.3.

I am not getting Sendmail is Active message. It is starting the sendmail service and not going in else block of the script.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39196242
Could it be that your sendmail subsystem immediately becomes inactive again, maybe due to some configuration error in sendmail.cf?

What do you see with "lssrc -s sendmail"?

And please post the whole script!

Additionally, run "cat -v scriptname"

Do you see strange characters ^M at the line ends?
0
 

Author Comment

by:oraclescsa
ID: 39196250
lssrc -s sendmail
Subsystem         Group            PID          Status
 sendmail         mail             458892       active


My script

if lssrc -s sendmail | grep "inoperative" | awk '{print $3}' ; then
    startsrc -s sendmail
      else
    echo "sendmail is active"
fi


when i run the above script it is showing
0513-029 The sendmail Subsystem is already active.
Multiple instances are not supported.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39196252
What is this  "awk" meant for? I didn't suggest using it.
Instead, I suggested using "grep -q" which suppresses grep's output - we don't need it because we rely on the return status.

"if" checks the status of the last command in the pipe, and since "awk '{print $3}'" is always successful  you'll always get a "true" status, so you'll never reach the "else" branch.


if lssrc -s sendmail | grep -q "inoperative"  ; then
    startsrc -s sendmail
      else
    echo "sendmail is active"
fi
0
 

Author Comment

by:oraclescsa
ID: 39196254
When i am using

lssrc -s sendmail | grep -q "inoperative"
No result

lssrc -s sendmail | grep -q "active"
No result

That is the reason i was using awk using takes the 3rd column from the result.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39196295
"No result" is not quite correct. "No output" instead is desired. See my last post.

Try

lssrc -s sendmail | grep -q "inoperative"
echo $?

and

lssrc -s sendmail | grep -q "active"
echo $?

and you'll see what I mean.  We use the returncode (to be seen in $?) for the evaluation.
0
 

Author Comment

by:oraclescsa
ID: 39196331
lssrc -s sendmail | grep -q "inoperative"
echo $?
> 1

lssrc -s sendmail | grep -q "active"
echo $?
> 0

Yes the script runs fine. It was my mistake when i ran the script the status was inoperative.
0
 

Author Closing Comment

by:oraclescsa
ID: 39196332
Quick and Fast communication and the result was perfect as desired.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question