Java如何判断ssh连接已断开,ssh连接关闭后,通过Java在后台执行bash脚本

I am using java to execute a simple bash script on a remote linux machine.

The bash script named "shortoracle.bash" have this script:

#!/bin/sh

runsql() {

i="$1"

end=$((SECONDS+360))

SECONDS=0

while (( SECONDS < end )); do

echo "INSERT into table_$i (col1) values (CURRENT_TIMESTAMP);" | sqlplus username/password

sleep 1

done

}

for i in $(seq 1 10); do

echo "DROP TABLE table_$i;" | sqlplus username/password

echo "CREATE TABLE table_$i (col1 TIMESTAMP WITH TIME ZONE);" | sqlplus username/password

runsql $i &

done

wait

Simply speaking: create 10 parallel connection that execute queries for 360 seconds.

From my java program i execute the following command:

sshconnection.execute("nohup su - oracle -c './shortoracle.bash'",2000);

The ssh executes the script successfully.

I want,after a timeout of 2 seconds (the second param) to terminate the ssh connection, but for the script to continue to run properly in the background (therefore the nohup, or so i thought), it's not happening:

After 2 seconds when i terminate the sshconnection, the bash program just stops working:

Only 3 of the 10 connections are open.

No more inserts happening.

If i give the connection a longer timeout, all is going well, but i don't want to hangup on this specific connection, i need to move on with the program.

What am i doing wrong here?

解决方案

You should add & after your command, i.e.

sshconnection.execute("nohup su - oracle -c './shortoracle.bash' &",2000);

Since nohup itself will otherwise get disconnected when you close the SSH

connection. With & you run 'nohup' itself in the background and therefore allow it to continue running after you close the SSH connection.

Hope this helps you out!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值