Controlling the next statement after running the eval command

I wrote a bash script to start the service in the background and exit the command line script. But after running the eval command, control does not move on to the next statement.

COMMAND="nohup java -jar jenkins.war" echo "starting service" eval $COMMAND echo "service running" exit 

echo "service running" and the exit will never happen. I want to start the process in the background and return to the command line while the service is still running. How to do it?

+7
source share
4 answers

Please do not put the command with your arguments in a string. Put your command and its arguments in an array (and don't use uppercase variable names, this is a terribly bad practice) and don't use eval . eval evil!

 command=( nohup java -jar jenkins.war ) echo "starting service" "${command[@]}" 2> /dev/null & echo "service running" 

& must have a command running in the background.

+12
source

Try:

 command="java -jar jenkins.war" echo "starting service" $command </dev/null >jenkins.out 2>jenkins.err & echo "service running" 
+3
source

Add an ampersand to the end of the call:

 eval $COMMAND & 
+2
source

eval does not solve this problem, you need the "run-in-background" symbol, i.e. '&' (without quotes).

Rewrite your string as

  #eval $COMMAND $COMMAND & 

Depending on what you really have in $ COMMAND, you may need to return eval .

In addition, depending on your need, starting the process in the background will generate some jobs output, i.e.

 [1] 10079 

To the std error of your console. You need additional syntax to redirect it to / dev / null. Here is an example:

 COMMAND="sleep 10" { $COMMAND & ; } 1>/dev/null 2>&1 

Using a process group ( { .... ; } should not cause any problems, but it can be.

Ihth

+1
source

All Articles