Dolphin
Dolphin

Reputation: 38641

what is the time about nohup command execute in shell?

I write a shell (CentOS) script command like this:

count=`ps -ef | grep ${APP_NAME} | grep -v "grep" | wc -l`
if [[ ${count} -lt 1 ]]; then
  cd ${APP_HOME}
  nohup ${JAVA_HOME}/bin/java -Xmx256M -Xms128M -jar -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=5016 ${APP_HOME}/${APP_NAME} >> /dev/null &
  sleep 5
else
  echo "process aready exists!"
  exit 1
fi

I execute the script in terminal, the output is:

++ ps -ef
++ wc -l
++ grep -v grep
++ grep soa-report-consumer-service-1.0.0-SNAPSHOT.jar
+ count=0
+ [[ 0 -lt 1 ]]
+ cd /data/jenkins/soa-report-consumer
+ sleep 15
+ nohup /opt/dabai/tools/jdk1.8.0_211/bin/java -Xmx256M -Xms128M -jar -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=5016 /data/jenkins/soa-report-consumer/soa-report-consumer-service-1.0.0-SNAPSHOT.jar

The question is: Is the sleep command executed before nohup command? Why echo sleep command result first?

Upvotes: 0

Views: 309

Answers (1)

dwmorrin
dwmorrin

Reputation: 2734

It's because you put the nohup command in the background with &. There is no output immediately from the command when you put it in the background as it is running in a separate shell, and your current shell immediately goes to the sleep command. By the time you return from sleep, the nohup background process has returned and outputs the value.

If you remove the & (and thus run the commands in the same shell) you will see the order changes.

Upvotes: 1

Related Questions