Solved

Bash Script to Start service in AIX

Posted on 2013-05-25
11
687 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:oraclescsa
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
Quick and Fast communication and the result was perfect as desired.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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 …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

743 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

9 Experts available now in Live!

Get 1:1 Help Now