开发者

Cron bash script leaves cron [defunct] lingering

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 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 "*******************************"


I think it's hard to tell what exactly caused your processes to defunct. Generally I'd suggest using autossh instead of ssh for this task.

I have found the following construct very reliable for setting up tunnels (in /etc/rc.local):

while sleep 1; do
    autossh $options $server
done &

I have never experienced defuncts using this.

Good luck :-)


Have you tried putting an exit statement at the end of your script below the echo? That might help...


In unix and similar systems, zombies (defunct processes) are caused by the parent process failing to accept the SIGCHLD signal generated by a child process when it terminates.

Or, to put it another way, the problem is not located in your defunct process, the problem is located in the parent process that spawned it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜