Fix lck and log file
[autosshd_ALT.git] / autosshd / etc / rc.d / init.d / autosshd
blobff3b067d8d58fb4f0914cf47ec97919c17c1b925
1 #!/bin/sh
3 # autosshd autossh system service
5 # chkconfig: 2345 30 60
7 # description: autosshd run autossh as system service
9 # processname: autossh
12 # Do not load RH compatibility interface.
13 WITHOUT_RC_COMPAT=1
15 # Source function library.
16 . /etc/init.d/functions
18 # Autosshd variables
19 AUTOSSH_LOGLEVEL=4
20 AUTOSSH_POLL=120
21 AUTOSSH_GATETIME=0
22 AUTOSSH_DEBUG=yes.
23 #FIX call in cycle for all conf
24 init_autossh(){
25 AUTOSSH_PIDFILE=${PIDFILEDIR}/$(basename $1 .conf).pid
26 AUTOSSH_LOCKFILE=$LOCKFILEDIR/$(basename $1 .conf).lck
27 AUTOSSH_LOGFILE=/var/lib/autosshd/$(basename $1 .conf).log
30 # Source networking configuration.
31 SourceIfNotEmpty /etc/sysconfig/network
33 # AutoSSHDaemon configuration
34 SYSCONFIGFILE="/etc/sysconfig/autosshd"
35 SourceIfNotEmpty $SYSCONFIGFILE
38 ###========= Functions =========
39 #===== help message =====
40 help()
42 echo "
43 Usage: ${0##*/} ACTION [CHANNEL]
44 ACTION - requested action of all or given AutoSSH channel
45 CHANNEL - optional name of AutoSSH channel (with corresponding
46 CHANNEL.* files in $CONFIGDIR)
48 Usefull actions is:
49 start - start all configured channels except listed
50 in MANUAL variable in $CONFIGDIR/CHANNEL.conf file
51 stop - stop all running channels
52 restart - restart channels that are configured for autostart
53 reload - restart only running channels
54 list - print list of configured channels
55 status - print status of running channels
56 help - print this message.
58 RETVAL=1
59 return $RETVAL
62 get_channels_list()
64 /bin/ls $CONFIGDIR/*.conf 2>/dev/null
67 list_channels()
69 LIST=$(get_channels_list)
70 echo "Configured channels:"
71 for config in $LIST; do
72 SourceIfNotEmpty $config
73 echo " `basename $config .conf`. Manual: ${MANUAL} "
74 done
75 return 0
78 running_channels()
80 LIST=`/bin/ls $PIDFILEDIR/*.pid 2>/dev/null`
81 CHANNELS=""
82 for proc in $LIST; do
83 CHANNELS="$CHANNELS`basename $proc .pid` "
84 done
85 return 0
88 do_run_one()
90 if [ -f $1 ]; then
91 SourceIfNotEmpty $1
93 # skip if in manual mode
94 is_no $MANUAL || return 0
96 start_daemon --pidfile "$AUTOSSH_PIDFILE" --make-pidfile --lockfile "$AUTOSSH_LOCKFILE" --user $AUTOSSHUSER --name autossh --displayname "autossh to ${HOST}" -- autossh ${AUTOSSH_OPTIONS}
97 RETVAL=$?
98 sleep 1
100 # strange hack: overwrite pidfile??
101 #ps ax | grep -v grep | grep ${AUTOSSH_PORT} | awk {'print $1'} > "$AUTOSSH_PIDFILE"
102 return $RETVAL
103 else
104 echo "Configuration file $1 not found"
105 RETVAL=1
106 return $RETVAL
110 do_stop_one()
112 if [ -f $1 ]; then
113 SourceIfNotEmpty $1
114 if [ -s $AUTOSSH_PIDFILE ]; then
115 #ParentPID=${PIDFILEDIR}/${HOST}.pid
116 #ps ax | grep -v grep | grep "parent of $(cat $AUTOSSH_PIDFILE)" | awk {'print $1'} > $ParentPID
117 stop_daemon --pidfile "$AUTOSSH_PIDFILE" --lockfile "$AUTOSSH_LOCKFILE" --expect-user $AUTOSSHUSER --name autossh --displayname "autossh to ${HOST}" -- autossh
118 RETVAL=$?
119 rm -f "$AUTOSSH_PIDFILE"
120 #rm -f "$ParentPID"
121 return $RETVAL
122 else
123 msg_not_running "autossh for `basename $1 .conf`"
124 echo
126 else
127 echo "Configuration file $1 not found"
128 RETVAL=1
129 return $RETVAL
134 show_status()
136 RETVAL=0
137 [ -n "$CHANNELS" ] || running_channels
139 if [ -z "$CHANNELS" ]; then
140 msg_not_running autossh
141 echo
142 RETVAL=1
143 else
144 for CHANNEL in $CHANNELS; do
145 status --pidfile "$PIDFILEDIR/$CHANNEL.pid" --lockfile "$LOCKFILEDIR/$CHANNEL.lck" --expect-user $AUTOSSHUSER --name ssh --displayname "autossh to $CHANNEL" -- autossh
146 RETVAL=$(( $RETVAL + $? ))
147 done
149 return $RETVAL
152 start()
154 # run only listed channels
155 if [ -n "$CHANNELS" ] ; then
156 for i in $CHANNELS; do
157 init_autossh $i.conf
158 do_run_one $CONFIGDIR/$i.conf
159 done
160 return 0
163 # run all scheduled channels
164 CHANNELS=$(get_channels_list)
165 for config in $CHANNELS; do
166 SourceIfNotEmpty $config
167 init_autossh $config
168 do_run_one $config
169 done
170 return 0
173 stop()
175 [ -n "$CHANNELS" ] || running_channels
177 for config in $CHANNELS; do
178 SourceIfNotEmpty $config
179 init_autossh $config
180 do_stop_one $CONFIGDIR/$config.conf
181 done
182 return 0
185 init_autossh
187 ###====== Main =====
188 RETVAL=0
190 OP=$1
191 shift
192 CHANNELS="$@"
194 is_yes "$NETWORKING" || return 0
196 # See how we were called.
197 case "$OP" in
198 start)
199 start
201 stop)
202 stop
204 reload|condreload)
205 stop
206 sleep 2
207 start
209 restart)
210 stop
211 sleep 2
212 start
214 status)
215 show_status
217 list)
218 list_channels
220 help)
221 help
224 msg_usage "${0##*/} {start|stop|reload|restart|status|list|help}"
225 RETVAL=1
226 esac
228 exit $RETVAL