Solved

Bash Script to Start service in AIX

Posted on 2013-05-25
11
704 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

617 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