jen
jen

Reputation: 15

Run python script on boot-up CentOS

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.

Upvotes: 1

Views: 1082

Answers (1)

heemayl
heemayl

Reputation: 42017

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 &

Upvotes: 1

Related Questions