0

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"
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
user2931144
  • 147
  • 2
  • 14
  • Thanks for all the help. However i decided to write the logging directly into the python program. So it writes the log files. Thanks. – user2931144 Jan 07 '15 at 10:08

2 Answers2

0

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).

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • The problem is almost the same, becouse when doing the solution above the start command never exists (doing /etc/rc.d/rpiVent start) and just wait at the Shell until i kill the task. The program should run in Background but output to logfile? And if adding & to the end of the sh script it will start the process but not kill it with /etc/rc.d/rpiVent stop – user2931144 Jan 06 '15 at 11:04
  • If you want to start the program headless (so that it runs even if the shell script terminates) you can use `nohup` UNIX command to detach the process from the running terminal: http://man.cx/nohup – Mikko Ohtamaa Jan 06 '15 at 11:31
  • I just want my program to Work as a service. By start/stop via /etc/rc.d/program start/stop. And at the same time output the stdout/stderr to a logfile. This is at the time not working together. If i do make a rc script that can start and stop. Like the one above i cannot get any output to file. If i make it Work to make a logfile it breaks the start/stop service. primary the stop, will not have control over the pid used. Becouse the pid is Associated with the sh script and not the program/python it self. – user2931144 Jan 06 '15 at 14:13
  • Using daemon does the same. But still the only option is -f to send stdout/stderr to /dev/null – user2931144 Jan 06 '15 at 14:17
  • I think you can use UNIX `tee` command to have both stdout/stderr and log file at the same time. `tee` copies stdout/stderr to log file. Also the script should work like this: If your script runs a child process (python) and the script is killed, also the python process dies. – Mikko Ohtamaa Jan 06 '15 at 22:41
0

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"