0

What would be the correct format for the following, where I want to execute two scripts? The following is only executing the first one for me:

if ps aux | grep -E "[a]ffiliate_download.py|[g]oogle_download.py" > /dev/null
then
    echo "Script is already running. Skipping"
else
    exec "$DIR/affiliate_download.py"
    exec "$DIR/google_download.py"
fi
2
  • Do you mean it's only running the first command when reaching else? Commented May 22, 2015 at 22:53
  • @kaybee99, exec causes the shell to replace itself with the process to be run, so that's normal/expected behavior here. Commented May 22, 2015 at 23:07

1 Answer 1

5

The exec command replaces the current shell process with the program it runs. Since the shell is no longer running, it can't run commands after that.

Just execute the commands normally:

else
    "$DIR/affiliate_download.py"
    "$DIR/google_download.py"
fi
Sign up to request clarification or add additional context in comments.

4 Comments

What is the difference between doing exec "command" and "command" ?
@David542, the difference is that exec replaces the shell's process table entry with the new command -- so the shell is no longer running, and signals sent to its PID are received by the program it's replaced with.
@David542 When you run a program normally, it's run in a new process, and the shell waits for it to finish (or doesn't wait if you put it into the background with &). When you run it with exec, it run's in the shell's process, completely replaceing the shell.
@David542: Without exec when "command" is an external program, the shell does a fork first. This creates a new shell process when it can mess with things like redirections. Then an exec is done to replace the current program with a different one, in the same process. Certain things survive an exec by default, for example the environment block, the file descriptor table, and the uid. See also the env program (man env).

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.