5

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?

3
  • It's my java program way to establish ssh connection to the remote server, sshcontext if it means anything to you. Commented Dec 9, 2019 at 13:14
  • Yes, but i don't think it's the issue, is it? Commented Dec 9, 2019 at 13:16
  • The issue is not the ssh executor, it works well, think of it as an abstract method for connecting and executing scripts on the remote machine. Commented Dec 9, 2019 at 13:24

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

8 Comments

Is the order of the & is right?, i should do su - oracle before
Ideally I would do it like this: su - oracle -c 'nohup ./shortoracle.bash &' to make sure it only runs in the background for the user oracle. Let me know if that works for you than I will update the answer accordingly :) ( I cant test it locally atm)
It must be with the nohup in the start of the script, it went crazy just now
Then I would write it like this: nohup su - oracle -c './shortoracle.bash' &
Still not working, when 2000 elapsed, it just stops with the inserts...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.