Solved

Bash Script to Start service in AIX

Posted on 2013-05-25
11
690 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
  • 6
  • 5
11 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now