I have a python script which I want to start using a rc(8) script in FreeBSD. The python script uses the #!/usr/bin/env python2
pattern for portability purposes. (Different *nix's put interpreter binaries in different locations on the filesystem).
The FreeBSD rc scripts will not work with this.
Here is a script that sets up a test scenario that demonstrates this:
#!/bin/sh
# Create dummy python script which uses env for shebang.
cat << EOF > /usr/local/bin/foo
#!/usr/bin/env python2.7
print("Hello foo")
EOF
# turn on executable bit
chmod +x /usr/local/bin/foo
# create FreeBSD rc script with command_interpreter specified.
cat << EOF > /usr/local/etc/rc.d/foo
#!/bin/sh
#
# PROVIDE: foo
. /etc/rc.subr
name="foo"
rcvar=foo_enable
command_interpreter="/usr/local/bin/python2.7"
command="/usr/local/bin/foo"
load_rc_config \$name
run_rc_command \$1
EOF
# turn on executable bit
chmod +x /usr/local/etc/rc.d/foo
# enable foo
echo "foo_enable=\"YES\"" >> /etc/rc.conf
Here follows a console log demonstrating the behaviour when executing the rc script directly. Note this works, but emits a warning.
# /usr/local/etc/rc.d/foo start
/usr/local/etc/rc.d/foo: WARNING: $command_interpreter /usr/local/bin/python2 != python2
Starting foo.
Hello foo
#
Here follows a console log demonstrating the behaviour when executing the rc script using the service(8) command. This fails completely.
# service foo start
/usr/local/etc/rc.d/foo: WARNING: $command_interpreter /usr/local/bin/python2 != python2
Starting foo.
env: python2: No such file or directory
/usr/local/etc/rc.d/foo: WARNING: failed to start foo
#
Why does the service foo start
fail?
Why does rc warn about the interpreter? Why does it not use the interpreter as specified in the command_interpreter
variable?