0

I am writing a script that runs occasionally and pulls jobs out of a mysql db. When I do this, I run something like the following:

job="/tmp/blah.sh arg1 arg2 arg3"
eval $job

I need it to move on right after I run the eval, and not wait for the other script to complete though. What is an easy way to do that? I tried

exec $job & 

based on a thread I found here, but that did the opposite, it ran the script, and then just stalled after the "job" completed, and stopped my entire script.

EDIT:

The problem I am running in to with separating my args and script is that jobs has multiple lines and looks like:

/tmp/blah1.sh arg1 arg2 arg3 arg4
/tmp/blah2.sh null null null arg4
/tmp/blah3.sh arg1 null arg3 arg4

So currently I have it running just : eval $jobs : if there is only one line, and if there are multiples, I do a for loop and run each line. What is the best way to run this in a for loop to pull out and separate the args and scripts?

2 Answers 2

6

I wouldn't use eval; instead, separate the command name from the arguments, then invoke the command in a normal fashion.

jobCmd="/tmp/blah.sh"
jobArguments=( "arg1" "arg2" "arg3" )

$jobCmd "${jobArguments[@]}" &
Sign up to request clarification or add additional context in comments.

1 Comment

Always trust chepner for sound advise on BASH. +1
2

Do not use exec - it replaces your running script with what you're invoking.

eval "$job" &

should do the trick.

Update:

  • If the command must be executed via strings stored in variables, @chepner's approach is preferable to eval.
  • Generally, though, any command can be sent to the background as is, just by appending &.
  • Output from jobs run in the background can make the bash prompt seemingly disappear, possibly giving the mistaken appearance that something is still executing. In other words: the script that invoked the background tasks may have ended, but it may appear otherwise. Just press Enter to see if the bash prompt reappears.

1 Comment

Hmm.. It still seems to sit there and watch the stdout of script I pass it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.