【问题标题】:find pid of ftp transfer in bash script在bash脚本中查找ftp传输的pid
【发布时间】:2025-08-29 13:45:02
【问题描述】:

我想在 bash 脚本中获取 ftp 的 pid...在 solaris 上运行

这是我的脚本:

#!/usr/bin/bash
...
ftp -inv $FTPDEST <<EOF
user $USER $PASS
put $file
EOF

我想获取 ftp 命令的 pid,这样我就可以在检查它是否挂起并杀死它.. 我有一个服务器崩溃,因为当 ftp 切断连接时,大约有 200 个 ftp 进程打开。由于某种原因,ftp 进程保持打开状态。

谢谢 马里奥

【问题讨论】:

  • 如果你想杀死每个 ftp 进程,类似pkill ftp 就足够了。但这只是一个假设,还是您想终止脚本启动的唯一 ftp 进程?
  • 我只想杀死脚本启动的 ftp 进程,如果在脚本结束时它还没有退出..

标签: bash ftp solaris pid


【解决方案1】:

这似乎是您所描述的,但可能不是您真正需要的。这是一种黑客行为......

#!/usr/bin/bash
trap 'exit 0' SIGUSR1   # this is the normal successful exit point

 # trigger the trap if ftp in a background process completes before 10 seconds    
(ftp -inv $FTPDEST <<-EOF 2>>logfile
user $USER $PASS
put $file
EOF 
kill -s SIGUSR1 $PPID ) &   # last line here shuts process down.  and exits with success

childpid=$!    # get the pid of the child running in background
sleep 10       # let it run 10 seconds

kill $childpid # kill off the ftp command, hope we get killed of first
wait
exit 1    # error exit ftp got hung up

父级等待 10 秒,而 ftp 子级在不到 10 秒内完成以成功退出。
成功意味着子进程向父进程发送 SIGUSR1 信号,然后父进程通过陷阱退出。

如果子进程耗时过长,父进程会终止慢速 ftp 子进程并错误退出。

【讨论】: