public static String getProcessPid( Session session, String userName, String grep1, String grep2 )
            throws JSchException, IOException {
        String command = "ps aux | grep \"" + grep1 + "\" | grep -v grep";
        String remoteResponseStr = launchACommand(session, command);
        if (remoteResponseStr.length() == 0) {
            return null;
        }

        List<String> pidsList = new ArrayList<>();
        String[] linesSplit = remoteResponseStr.split("\n");
        for( String line : linesSplit ) {
            if (!line.contains(grep2)) {
                continue;
            }
            String[] psSplit = line.split("\\s+");
            if (psSplit.length < 3) {
                throw new JSchException("Could not retrieve process data. Result was: " + line);
            }

            String user = psSplit[0];
            if (userName != null && !user.equals(userName)) {
                continue;
            }
            String pid = psSplit[1];

            try {
                Integer.parseInt(pid);
            } catch (Exception e) {
                throw new JSchException("The pid is invalid: " + pid);
            }

            pidsList.add(pid);
        }
        if (pidsList.size() > 1) {
            throw new JSchException("More than one process was identified with the given filters. Check your filters.");
        } else if (pidsList.size() == 0) {
            return null;
        }
        return pidsList.get(0);
    }
public static String getProcessPid ( Session session , String userName , String grep1 , String grep2 ) throws JSchException , IOException { String command = "ps aux | grep \"" + grep1 + "\" | grep -v grep" ; String remoteResponseStr = launchACommand ( session , command ) ; if ( remoteResponseStr . length ( ) == 0 ) { return null ; } List < String > pidsList = new ArrayList < > ( ) ; String [ ] linesSplit = remoteResponseStr . split ( "\n" ) ; for ( String line : linesSplit ) { if ( ! line . contains ( grep2 ) ) { continue ; } String [ ] psSplit = line . split ( "\\s+" ) ; if ( psSplit . length < 3 ) { throw new JSchException ( "Could not retrieve process data. Result was: " + line ) ; } String user = psSplit [ 0 ] ; if ( userName != null && ! user . equals ( userName ) ) { continue ; } String pid = psSplit [ 1 ] ; try { Integer . parseInt ( pid ) ; } catch ( Exception e ) { throw new JSchException ( "The pid is invalid: " + pid ) ; } pidsList . add ( pid ) ; } if ( pidsList . size ( ) > 1 ) { throw new JSchException ( "More than one process was identified with the given filters. Check your filters." ) ; } else if ( pidsList . size ( ) == 0 ) { return null ; } return pidsList . get ( 0 ) ; }
Get the pid of a process identified by a consequent grepping on a ps aux command.

<P><b>THIS WORKS ONLY ON LINUX HOSTS</b></p>

@param session the jsch sesssion to use.
@param userName an optional username to check the process on.
@param grep1 the first grep filter.
@param grep2 the second grep filter, done in the java code.
@return  the process pid or <code>null</code> if no process was found.
@throws JSchException
@throws IOException