0

I have the java code as the following:

    public class Example{
        public static void main(String args[]){
            //Done something here
            // Start process A
            somefunction();
       }


        public static void somefunction(){
            // Done some implementation
            System.out.println("Completed");
        }
    }    

I have a process A (a linux script) that runs for about 20 minutes. This process doesn't effect my current program in any way.

The following I am trying to do: 1. Trigger to run the process. 2. I dont want to wait for that process to complete. 3. Immediately start with somefunction() after triggering of the process.

I just want to trigger/run the process A and don't care if any output it gives when it gets completed.

I have looked the following link : Run a external application in java but don't wait for it to finish

but was not able to run the process, successfully.

I tried to run some shorter command like : "sleep 10; mv /home/file /home/file1;", for "name" argument in the above link. (Command description: This command just sleep for 10 seconds and the rename file to file1. I did not happen. (FYI, I am using RedHat).)

The code runs successfully but I can't see any rename of the file after 10 seconds.

How do I do it? Some sample code will be really helpful.

Thanks.

Community
  • 1
  • 1
Shubham
  • 33
  • 12
  • If you have a Linux script, couldn't you run it on the command line? There are techniques to put it also background process. – M. Reif Sep 22 '15 at 10:47
  • 1
    you really should'nt use a programming language to start script-language files - that'll always produce unexpected results and is error-prone. Either do everything within one language OR create/use fixed interfaces between these languages - starting processes and passing parameters is not what people call a "interface". – specializt Sep 22 '15 at 10:53
  • Are you looking for this type of solution? 1) Have a cron tab in Linux 2) The cron tab will start a java process and run it in background (by using & operator in a shell script) 3) Start a new thread to start process A. – Ravindra babu Sep 22 '15 at 11:01
  • I think we need to see the code that is failing (or an equivalent SSCCE). Why? Because, I suspect that your real problem is NOT what you think it is. For instance if you run `Runtime.exec("echo hi ; sleep 100");` it will output `hi ; sleep 100`. – Stephen C Sep 22 '15 at 11:12
  • @specializt why on earth are you upvoting your own comments/ answers everwhere on the stackoverflow? BTW: your comment is useless here as OP's question is perfectly valid. – G. Demecki Sep 25 '15 at 13:13
  • @G.Demecki What you're accusing me of is impossible. Welcome to SO, you have much to discover. – specializt Sep 25 '15 at 13:15
  • @specializt impossible? Having another account would be enough. You know that. Just look at this [post](http://stackoverflow.com/a/25674424/1037316). Aren't you ashamed? – G. Demecki Sep 25 '15 at 13:21
  • @G.Demecki Could it be that you're suffering from some form of schizophrenia? Your link clearly shows a normal conversation and SOMEone agreed with mine - as it happens quite frequently since the internet is full of internet superheroes and i cannot let outright lies and false claims stand uncorrected. I think you need a hobby. Hobbies can ease mental pain. Just saying. – specializt Sep 25 '15 at 14:18

2 Answers2

1

I think that the solution may be as simple as this:

public class Example{
    public static void main(String args[]){
        String[] command = new String[] {
            "/bin/sh", "-c", "sleep 10; mv /home/file /home/file1"
        };
        Process process = new ProcessBuilder(command).start(); 
        somefunction();
        process.waitFor();
   }

... or the equivalent using "cmd" on Windows.

In short, I suspect that your real problem is that you are feeding shell command syntax directly to ProcessBuilder which doesn't understand it!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You need to stop your application exiting before the process has finished, you can start the process before you call somefunction(), but when somefunction() completes, wait until the process completes before exiting. E.g.

public class Example{
    public static void main(String args[]){
        //Done something here
        // Start process A
        Process process = new ProcessBuilder(name).start(); 
        somefunction();
        process.waitFor(); //stop Java from exiting before the process finishes
   }


    public static void somefunction(){
        // Done some implementation
        System.out.println("Completed");
    }
}
John McClean
  • 5,225
  • 1
  • 22
  • 30