Fix start when already running
[autosshd_ALT.git] / autosshd / etc / rc.d / init.d / autosshd
blob96d7f68ca620bc2b5414e1547c2f842b2d8b2045
1 #!/bin/sh
3 # autosshd autossh system service
5 # chkconfig: 2345 30 60
7 # description: autosshd run autossh as system service
10 processname="autossh"
11 startname="/usr/share/autosshd/autossh-conf" #need for export var, because start shell by root and run autossh by _autossh user
12 # processname: $processname
15 # Do not load RH compatibility interface.
16 WITHOUT_RC_COMPAT=1
18 # Source function library.
19 . /etc/init.d/functions
21 # Source networking configuration.
22 SourceIfNotEmpty /etc/sysconfig/network
24 # AutoSSHDaemon configuration
25 SYSCONFIGFILE="/etc/sysconfig/autosshd"
26 SourceIfNotEmpty $SYSCONFIGFILE
27 # Load AutoSSHD variables and init_autossh function
28 SourceIfNotEmpty "$startname"
31 ###========= Functions =========
32 #===== help message =====
33 help()
35 echo "
36 Usage: ${0##*/} ACTION [CHANNEL]
37 ACTION - requested action of all or given AutoSSH channel
38 CHANNEL - optional name of AutoSSH channel (with corresponding
39 CHANNEL.* files in $CONFIGDIR)
41 Usefull actions is:
42 start - start all configured channels except listed
43 in AUTOSTART variable in $CONFIGDIR/CHANNEL.conf file
44 stop - stop all running channels
45 restart - restart channels that are configured for autostart
46 reload - restart only running channels
47 list - print list of configured channels
48 status - print status of running channels
49 help - print this message.
50 testssh - test connection configuration by ssh.
53 RETVAL=1
54 return $RETVAL
57 do_run_one()
59 if [ -f $1 ]; then
60 SourceIfNotEmpty $1
62 # skip if in AUTOSTART mode
63 is_yes "$AUTOSTART" || return 0
65 [ -e "$AUTOSSH_LOCKFILE" ] && echo "autossh to $(basename $1 .conf) service is already running." && return 1
67 start_daemon --lockfile "$AUTOSSH_LOCKFILE" \
68 --user $AUTOSSHUSER --displayname "autossh to ${HOST}" -- $startname $1 "${AUTOSSH_OPTIONS}"
69 RETVAL=$?
70 sleep 1
71 return $RETVAL
72 else
73 echo "Configuration file $1 not found"
74 RETVAL=1
75 return $RETVAL
79 do_stop_one()
81 if [ -f $1 ]; then
82 SourceIfNotEmpty $1
83 if [ -s $AUTOSSH_PIDFILE ]; then
84 stop_daemon --pidfile "$AUTOSSH_PIDFILE" --lockfile "$AUTOSSH_LOCKFILE" \
85 --expect-user $AUTOSSHUSER --name $processname --displayname "autossh to ${HOST}" -- $processname
86 RETVAL=$?
87 return $RETVAL
88 else
89 msg_not_running "autossh for `basename $1 .conf`"
90 echo
92 else
93 echo "Configuration file $1 not found"
94 RETVAL=1
95 return $RETVAL
100 show_status()
102 RETVAL=0
103 [ -n "$CHANNELS" ] || running_channels
105 if [ -z "$CHANNELS" ]; then
106 msg_not_running autossh
107 echo
108 RETVAL=1
109 else
110 for CHANNEL in $CHANNELS; do
111 init_autossh $config
112 status --pidfile "$PIDFILEDIR/$CHANNEL.pid" --lockfile "$LOCKFILEDIR/$CHANNEL.lck" \
113 --expect-user $AUTOSSHUSER --name $processname --displayname "autossh to $CHANNEL" -- $processname
114 RETVAL=$(( $RETVAL + $? ))
115 done
117 return $RETVAL
120 start()
122 # run only listed channels
123 if [ -n "$CHANNELS" ] ; then
124 for i in $CHANNELS; do
125 do_run_one $CONFIGDIR/$i.conf
126 init_autossh $config
127 done
128 return 0
131 # run all scheduled channels
132 CHANNELS=$(get_channels_list)
133 for config in $CHANNELS; do
134 init_autossh $config
135 do_run_one $config
136 done
137 return 0
140 stop()
142 [ -n "$CHANNELS" ] || running_channels
144 for config in $CHANNELS; do
145 SourceIfNotEmpty $config
146 init_autossh $config
147 do_stop_one $CONFIGDIR/$config.conf
148 done
149 return 0
152 get_channels_list()
154 /bin/ls $CONFIGDIR/*.conf 2>/dev/null #returns only filenames
157 list_channels()
159 LIST=$(get_channels_list)
160 echo "Configured channels:"
161 for config in $LIST; do
162 SourceIfNotEmpty $config
163 echo " `basename $config .conf`. AUTOSTART: ${AUTOSTART} "
164 done
165 return 0
168 running_channels()
170 LIST=`/bin/ls $PIDFILEDIR/*.pid 2>/dev/null`
171 CHANNELS=""
172 for proc in $LIST; do
173 CHANNELS="$CHANNELS`basename $proc .pid` "
174 done
175 return 0
178 ###====== Main =====
179 RETVAL=0
181 OP=$1
182 shift
183 CHANNELS="$@"
185 is_yes "$NETWORKING" || return 0
187 # See how we were called.
188 case "$OP" in
189 start)
190 start
192 stop|condstop)
193 stop
195 reload|restart)
196 stop
197 sleep 2
198 start
200 condrestart)
201 running_channels
202 if [ -n $CHANNELS ] ; then
203 restart
206 status)
207 show_status
209 list)
210 list_channels
212 help)
213 help
215 testssh)
216 su - -s /bin/sh -c "autosshd-ssh $1" _autossh
219 msg_usage "${0##*/} {start|stop|reload|condrestart|condstop|restart|status|list|help|testssh}"
220 RETVAL=1
221 esac
223 exit $RETVAL