FreeBSD rc script with output to file

1.3k views Asked by At

I have this script below where I start a python program.
The python program outputs to stdout/terminal. But I want the program to be started via rc script silently.

I can start the and stop the program perfectly. And it also creates the log file, but dosent fill anything to it. I tried a lot of different ways. Even with using daemon as starter.

Where is my problem?

#!/bin/sh
# REQUIRE: DAEMON
# KEYWORD: shutdown
. /etc/rc.subr

location="/rpiVent"

name="rpiVentService"
rcvar=`set_rcvar`
command="$location/$name"
#command_args="> $location/$name.log" // Removed
command_interpreter="/usr/bin/python"

load_rc_config $name
run_rc_command "$1"
2

There are 2 answers

5
Mikko Ohtamaa On

Piping with > is a feature of the shell and not an actual part of the command line. When commands are programmatically involved, the arguments given them cannot contain shell directives (unless the parent process has special support for shell, like with Python subprocess.Popen(shell=True) (doc).

What in this case you can do is that you can wrap your command (/rpiVent/rpiVentService) to a shell script then invoke this shell script in FreeBSD rc script::

Creat /rpiVent/run.sh:

 #!/bin/sh
 /rpiVent/rpiVentservice > /rpiVent/rpiVentService.log 

and then use this is a command (no args needed).

0
Clément Moulin - SimpleRezo On

The correct way to do this is probably by "overriding" the start command using start_cmd variable, like this:

#!/bin/sh
# REQUIRE: DAEMON
# KEYWORD: shutdown
. /etc/rc.subr

location="/rpiVent"

name="rpiVentService"
rcvar=`set_rcvar`

load_rc_config $name

command="$location/$name"
command_interpreter="/usr/bin/python"

start_cmd=rpivent_cmd

rpivent_cmd()
{
   $command_interpreter $command >$location/$name.log
}

run_rc_command "$1"