Я запускаю приведенный ниже сценарий с помощью cron, в /etc/cron.d/mycron у меня есть следующее:
*/10 * * * * MyUserThatNeedsToRunTheScript /backup/sshconnect.sh
Однако, глядя на ps -aux, я обнаружил, что несколько [defunct] процессы затягиваются, есть идеи?Это связано с тем, что SSH запускается с -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 tunnel@100.123.124.125
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 "*******************************"