Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a bash script from a Windows machine in Java code

Tags:

java

bash

I have a bash script that restores my database. The database is on a remote Linux server, and my Java code is on Windows. How can I run the script?

like image 978
user506246 Avatar asked Dec 17 '25 02:12

user506246


2 Answers

What do you mean by restore? If you want just load dump of your DB maybe create backup DB and then just copy rows to target database. If you really need to run this scrip easiest way will be to connect to remote server via ssh and launch that script. Use Putty or some ssh java lib to make a connection and send command to run. More info about putty here

like image 142
janisz Avatar answered Dec 19 '25 14:12

janisz


Try something like this:-

 Process p = Runtime.exec("ssh myhost");
 PrintStream out = new PrintStream(p.getOutputStream());
 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream());

 out.println("ls -l /home/me");
 while (in.ready()) {
 String s = in.readLine();
 System.out.println(s);
 }
 out.println("exit");

p.waitFor();

From the source thread

like image 30
Rahul Tripathi Avatar answered Dec 19 '25 16:12

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!