Solved

Bash Script to Start service in AIX

Posted on 2013-05-25
11
693 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 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…

839 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