1

Trying to run a python script on boot-up of CentOS. My /etc/init.d/example-script looks like this:

case $1 in 
    start)
        $(python /etc/bin/example-script.py &)
    ;;
    stop)
        echo "filler text"
    ;;

When I try service example-script start, it runs the script, but does not execute in the background. example-script.py has a while(True) loop, so maybe this is why?

But when I type python /etc/bin/example-script.py & in Terminal, it does execute in the background. It's only when this command is inside a bash script that it fails.

1
  • This is probably more of a CentOS admin question than a Python programming question, so you should probably ask on whichever SE site is appropriate for that. (Also, I suspect you'll need to specify which version of CentOS.) Commented Mar 20, 2018 at 17:38

1 Answer 1

1

That's because you have spawned a subshell by putting the command inside command substitution syntax ($()) i.e. command substitution runs the command in subshell; and you're backgrounding the command in that subshell, not the original script's shell.

Moreover, you are not doing anything with the STDOUT, so inviting another subshell seems pointless. Remove the command substitution:

python /etc/bin/example-script.py &
Sign up to request clarification or add additional context in comments.

Comments

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.