gpt4 book ai didi

bash - Cron bash 脚本让 cron [defunct] 挥之不去

转载 作者:行者123 更新时间:2023-12-04 19:23:46 31 4
gpt4 key购买 nike

我正在使用 cron 运行以下脚本,在/etc/cron.d/mycron 我有以下内容:

*/10 * * * * MyUserThatNeedsToRunTheScript /backup/sshconnect.sh  

但是,查看 ps -aux 我发现一些 [defunct] 进程挥之不去,有什么想法吗?这是否与使用 -f 运行 SSH 有关?
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

干杯!
#!/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 "*******************************"

最佳答案

我认为很难说出究竟是什么导致您的流程失效。
一般来说,我建议使用 autossh而不是 ssh对于这个任务。

我发现以下构造对于设置隧道非常可靠(在 /etc/rc.local 中):

sleep 时 1;做
autossh $选项$服务器
完毕 &

我从未经历过使用此功能的失效。

祝你好运 :-)

关于bash - Cron bash 脚本让 cron [defunct] 挥之不去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3208498/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com