Hi!
I'm running the below script with cron, in /etc/cron.d/mycron I have the following:
*/10 * * * * MyUserThatNeedsToRunTheScript /backup/sshconnect.sh
However, looking at ps -aux I find that a few [defunct] processes are lingering, any ideas? Does this have to with SSH being run with -f ?
5 0 1598 641 20 0 2552 1068 pipe_w S ? 0:00 CRON
4 1001 1599 1598 20 0 0 0 exit Zs ? 0:00 [sh] defunct
5 1001 1601 1598 20 0 0 0 exit Z ? 0:00 [cron] defunct
1 1001 1605 1 20 0 5148 884 poll_s Ss ? 0:00 /usr/bin/ssh -T -f -N -L7777:127.0.0.1:3306 [email protected]
Cheers!
#!/bin/bash
# Creates an SSH tunnel to allow local access to a remote mysql server.
# Requires ssh keys for the user running the script or the user that CRON is setup under
echo "*******************************"
echo `date`
user=tunnel
server=100.123.124.125
remote_port=3306
local_port=7777
createTunnel() {
/usr/bin/ssh -T -f -N -L${local_port}:127.0.0.1:${remote_port} ${user}@${server}
if [[ $? -eq 0 ]]; then
echo ${local_port} Tunnel to ${server} created successfully
else
echo An error occurred creating tunnel ${local_port} to ${server} RC was $?
fi
}
## Run the mysqladmin status command remotely. If it returns non-zero, then create a new connection
echo Verifying Database Connection
echo "----------------------------------"
/usr/bin/mysqladmin -u test -ptest123 -h127.0.0.1 -P${local_port} status
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
exit
else
echo Tunnel already exists
exit
fi
echo "*******************************"