Link to home
Start Free TrialLog in
Avatar of gs_kanata
gs_kanataFlag for Canada

asked on

How to make sure the spawned shell script has fully started?

I have the following code snippet which start a Unix command in the background via the SSH session.

        monitorCommand = "nohup " + monitorCommand + " > " + logFile + " &";
        logger.info("Starting TE monitor '{}'", monitorCommand);
        Cli ssh = TE.getNewCli();
        String response = ssh.send(monitorCommand);
        ssh.disconnect();
        pid = Pid.parse(response, "TE monitor");
        setPid(pid, ssh);
        return pid;

Since spawned remote returns the Unix process id instantly as it is running in the background. However, the process does not start fully before the SSH session is closed. I found that the process was abandoned as I could not find the process manually. If I add sleep for 10 seconds before closing the SSH session, I could find the process survive its launching. The reason for its slow starting maybe due to log directory is mounted to several servers. Since this is library code and works for most process launching, I am wondering whether there is a way to delay the pid returning from the Unix to make sure the process is fully started. For the following statement, could it be "blocked" for a while? :-)

String response = ssh.send(monitorCommand);
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial