加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 后台shell脚本在ssh注销后无法访问目录,即使使用nohup

发布时间:2020-12-14 00:31:48 所属栏目:Linux 来源:网络整理
导读:我想在服务器计算机的后台运行一个 shell脚本,并从ssh连接启动该shell脚本.即使我使用nohup运行后台进程脚本,但只要我关闭ssh连接(并且不久),后台脚本就会因目录无法访问错误而失败. runInBackground.sh: #!/bin/bash...nohup ./run.sh /dev/null 2 local/e
我想在服务器计算机的后台运行一个 shell脚本,并从ssh连接启动该shell脚本.即使我使用nohup运行后台进程脚本,但只要我关闭ssh连接(并且不久),后台脚本就会因目录无法访问错误而失败.

runInBackground.sh:

#!/bin/bash
...
nohup ./run.sh > /dev/null 2> local/errorLog.txt < /dev/null &

run.sh:

#!/bin/bash
...
while [ true ] ; do
    ...
    cd optaplanner-examples
    mvn exec:exec // calls java process
    cd ..
done

所以当我运行runInBackground.sh时,一切都运行好几个小时,直到我断开我的ssh连接.
一旦我退出,errorlog.txt填满:

java.io.FileNotFoundException: /home/myUser/server/optaplanner-simple-benchmark-daemon/local/output/
./run.sh: line 64: /home/myUser/server/optaplanner-simple-benchmark-daemon/local/processed/failed_machineReassignmentBenchmarkConfig.xml: No such file or directory
fatal: Could not change back to '(unreachable)/server/optaplanner-simple-benchmark-daemon/local/optaplannerGitClone/optaplanner': No such file or directory
ls: cannot access /home/myUser/server/optaplanner-simple-benchmark-daemon/local/input: No such file or directory
ls: cannot access /home/myUser/server/optaplanner-simple-benchmark-daemon/local/input: No such file or directory
ls: cannot access /home/myUser/server/optaplanner-simple-benchmark-daemon/local/input: No such file or directory
... // 1000+ more of that ls error

(Full source code)

解决方法

好吧,它不一定是加密的主目录,但可能是一个自动挂载的主目录(例如通过NFS等).它在会话启动时挂载,并在退出时卸载.加密的家庭目录只是使用这种技术的可能原因之一.

主要问题是决定用户是否需要家庭目录的规则是什么.我希望它可以是一个分配的pty.你可以通过启动一个非交互式SSH会话来测试它是否真的是真的没有伪终端:ssh -T user @ host ls / home / myUser / server.我可以预期,在这种情况下,您将无法获得正确的目录列表.

然后我会使用像screen这样的程序来延长超出SSH会话限制的交互式会话生命周期.

服务器可能会使用其他一些机制来为交互式SSH会话提供主目录.例如.监视utmp中列出的交互式会话在这种情况下,只要您需要服务,就需要一个能够保留记录的程序.也许您可以使用自动重新建立的SSH会话.例如,我使用以下systemd单元自动保留来自不同专用网络中的一个工作站的ssh隧道:

[Unit]
Description=A tunnel to SOME_HOST
PartOf=sshd.service
Requires=network.service

[Service]
ExecStart=/usr/bin/ssh -N -q -R 2222:localhost:22 SOME_HOST
Restart=on-failure
RestartSec=5
User=tunnel
Group=tunnel

[Install]
WantedBy=sshd.service
WantedBy=network.service

发生故障时,systemd会自动重启设备并重新建立SSH会话.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读