4

ssh升级后nohup进程在终端退出后消失

 2 years ago
source link: https://blog.yinaun.com/posts/24522.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ssh升级后nohup进程在终端退出后消失, ssh版本如下:

# ssh -V
OpenSSH_8.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017

怀疑是OpenSSH的bug。

临时解决办法

终端ssh登录后,再通过:su - 用户 后执行nohup指令,这样再退出终端后,进程仍可保留。

方式2(经验证,还是不行,过几分还是会被SIGKILL干掉)
如果上述方式还不行,可以用strace跟踪一下看看是什么信号导致进程终止的。

# strace -T -tt -e trace=all -p 28842
strace: Process 28842 attached
15:46:35.200201 read(4,

0x7bf1c0, 20) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <82.387507>
15:47:57.587934 --- SIGHUP {si_signo=SIGHUP, si_code=SI_KERNEL} ---
15:47:57.588034 read(4, 0x7bf1c0, 20) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.001049>
15:47:57.589260 --- SIGTERM {si_signo=SIGTERM, si_code=SI_USER, si_pid=1, si_uid=0} ---
15:47:57.589429 +++ killed by SIGTERM +++

这里的情况是被SIGTERM终止,正常情况下,SIGTERM信号是不会传递给nohup子进程的。

临时解决办法:

将要nohup执行的命令加到sh脚本中,然后再sh的前面部分加入SIGTERM信号的拦截:

# cat 1.sh
trap '' SIGTERM ##这里拦截,让它不处理
tail -f gun

# nohup ./1.sh &

原因及最终解决办法

OpenSSH_8.4后加了sshd.socket方式启动sshd服务,而以这种方式启动,使用的服务配置文件为:[email protected]

[root@2]cd /usr/lib/systemd/system/
[root@2 system]# vi [email protected]
[Unit]
Description=OpenSSH per-connection server daemon
Documentation=man:sshd(8) man:sshd_config(5)
Wants=sshd-keygen.service
After=sshd-keygen.service

[Service]
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=-/usr/sbin/sshd -i $OPTIONS
KillMode=process ### 相比sshd.service少了这个设置
StandardInput=socket

加上上述配置,重启sshd.socket方式服务即可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK