Thursday, 18 April 2013

Scripting the stop and restart of SRC controlled processes on AIX

Most SRC controlled processes support the "refresh -s" command to have them re-read their configuration files after a change.   For example, if you edit the inetd.conf configuration file you just do a "refresh -s inetd" and inetd will re-read the configuration file and you are good to go. 

However, some SRC controlled processes, such as SSHD, do not support this:
# refresh -s sshd
0513-005 The Subsystem, sshd, only supports signal communication.
In order to have a process such as SSHD re-read its configuration file (/etc/ssh/sshd_config), you must do a "stopsrc -s sshd" followed by a "startsrc -s sshd":

# stopsrc -s sshd
0513-044 The sshd Subsystem was requested to stop.
# startsrc -s sshd
0513-059 The sshd Subsystem has been started. Subsystem PID is 3670044.
As you can see when you do the "stopsrc" it just says the process was requested to stop; not that it was stopped.   This implies that there might be a delay before the process actually stops.  

So what a lot of people do in scripts where they need to restart processes like this is something like this:
stopsrc -s sshd
sleep 10
startsrc -s sshd
However; there are some problems with this methodology.  First, if the process takes longer than 10 seconds to stop for some reason, then your startsrc command would fail, and then when the process finally did stop, you would be left with a system without SSHD running.    Second, during this 10 second sleep SSHD isn't running and new connections won't be able to be established which might impact thesystem.

Here is a one liner to request a stop to a SRC process like SSHD, verify it is stopped, and then restart it:
stopsrc -s sshd && until lssrc -s sshd | grep -q inoperative; do echo "Waiting for sshd to stop"; perl -e 'select(undef,undef,undef,.25)'; done && startsrc -s sshd
Basically, what it will do is request the SSHD process be stopped, then use an "until" loop to wait until lssrc -s sshd reports it is inoperative.   While it is waiting, it will sleep for 0.25 seconds before checking again.  perl -e 'select(undef, undef, undef, .25)'  is a easy way to sleep for less than 1 second.   After the process is verified to have stopped, it is started again.    

0 blogger-disqus:

Post a Comment