0

How to execute a shell script to start a service by sudo command in Linux via Java Ex:cmd="sudo path/script.sh start"

This Java program will execute commands in linux. Even I am able to do 'sudo ls -lt path' and also 'sudo path/script.sh start'

//Java
public List<String> sshConnection(String usr,String host,int port,String pass)
{
      Session session=null;
      List<String> outputList = new ArrayList<String>();
      try
      {
        JSch jsch = new JSch();
        session = jsch.getSession(usr, host, port);
        session.setPassword(pass);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();  

        //String cmd="sudo ls path";            //working fine
        String cmd="sudo path/script.sh start"; //service is not starting..but getting the exact output as linux

        ChannelExec channelEx=new ChannelExec();

        channelEx = (ChannelExec) session.openChannel("exec");

        if(cmd.contains("sudo"))
        {
            channelEx.setPty(true);
        }

        ((ChannelExec) channelEx).setCommand(cmd);
        channelEx.connect();

        InputStream cmdOp = channelEx.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(cmdOp));

        String printOp;
        while ((printOp = br.readLine()) != null) 
        {
            outputList.add(printOp);
        }
        br.close();

        channelEx.disconnect();

    } 
    catch (Exception ex) 
    {
        System.out.println(ex.getMessage());
    } 
    finally 
    {
        if (channelEx != null) 
        {
            channelEx.disconnect();
        }
    }

    if(session!=null)
    {
        session.disconnect();
    }
    System.out.println("disconnected successfully");
    return outputList;
}

I executed the same command directly in linux and got a output telling the 'service is running' with the process id.When I did grep for the process ID using ps command it displayed the service with the same process ID. But when I executed the same command via the above Java Program,I got the exact output('service is running') as linux with the process ID in the output console.After the java program execution, I did grep for the process ID(from Java output console) in linux,no such process was running with that process ID . I am not able to find where it is going wrong.
Please help !

user2699067
  • 33
  • 2
  • 8
  • Don't think this has anything to do with the Java program since the command seems to have run at the server. – Henry Jan 02 '16 at 12:44

2 Answers2

1

You are starting the process then immediately terminating the process tree.

Try using

channelEx.setPty(false);

Or alternatively adding nohup to your command line. See...

jsch ChannelExec run a .sh script with nohup "lose" some commands

Community
  • 1
  • 1
kervin
  • 11,672
  • 5
  • 42
  • 59
  • Hi Kervin, I tried channelEx.setPty(false).It is not working. setPty is to set the pseudo terminal,without that sudo command won't work.I tried setting true and later false.But nothing happened. I also tried nohup. cmd="nohup sudo path/script.sh start" it didn't work. – user2699067 Jan 04 '16 at 16:13
  • I used sys.out in setPty(true) and setPty(false);During the execution of the single sudo command the terminal got set and reset twice, which means the terminal is disconnecting before it the executes the command. I think this is the point where it is failing. – user2699067 Jan 04 '16 at 16:19
  • Is there any way to wait until the sudo command execution completes before it terminates? Please help! – user2699067 Jan 04 '16 at 16:21
  • Try nohup your shell instead of sudo directly. What error are you getting? Did you also redirect stderr as the link suggests? E.g. **nohup bash -c "sudo path/script.sh start 2>&1 &" && sleep 4** as your command – kervin Jan 04 '16 at 17:12
  • I tried as what you have given(I replaced " with ' since cmd is a String variable).It shows this " nohup: ignoring input and appending output to `nohup.out' " Also service is not starting. – user2699067 Jan 04 '16 at 18:24
  • http://stackoverflow.com/questions/24646320/nohupignoring-input-and-appending-output-to-nohup-out-in-linux-centos I went through this link,here the thing is not about this exception but it should execute the sudo command completely without termination in the middle. I also printed the exit status code before this exception which was 0,means the command executed successfully,but still why it is not starting the actual process which supposed to start by this command – user2699067 Jan 04 '16 at 18:40
  • This is most likely an issue with your script. Also you shouldn't replace the double quotes. Try escaping them with backslash. – kervin Jan 05 '16 at 12:46
  • while executing the code I did grep for the process ID ,the service was running..and it was running till the java program execution stop.Once the Java Program is terminated the process has stopped. But this won't happen in linux machine.Even if exit out from linux and login again the process will be running. – user2699067 Jan 05 '16 at 14:51
  • Hi Kervin, I tried escaping the double quotes still got the same output nohup: ignoring input and appending output to `nohup.out'. Moreover I did grep during execution of Java program for the process but for the nohup command which you sent,but the process didn't start even during Java execution – user2699067 Jan 05 '16 at 14:59
-1

I am able to start the command as the root user and see the process ID after the Java code exits.

  1. Use command channelExec.setCommand("sudo -S -p '' " + cmd);
  2. Run your command with nohup (e.g. nohup start.sh)
  3. Make sure all the environment variables are set for the script using an export command.
William Price
  • 4,033
  • 1
  • 35
  • 54