Solved Creating service: problem with pid & output redirection

I created a service:
Bash:
#!/bin/sh

# PROVIDE: reklama
# REQUIRE: DAEMON
# KEYWORD: shutdown

. /etc/rc.subr

name="reklama"
rcvar=reklama_enable

start_cmd="${name}_start"

pidfile="/var/run/${name}.pid"

reklama_start()
{
        /usr/local/bin/mpg123 -a /dev/dsp4.0 --loop -1 /usr/home/ishayahu/boroda/Vivaldi.mp3 & > /dev/null 2>&1
        echo ${rc_pid} > ${pidfile}
}

load_rc_config $name


run_rc_command "$1"

And I have two problems with it:
1) it creates empty pid file:
Code:
root@012-music:/home/ishayahu # cat /var/run/reklama.pid
2) it doesn't send output to /dev/null:
Code:
root@012-music:/home/ishayahu # service reklama onestart
root@012-music:/home/ishayahu # High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
        version 1.29.3; written and copyright by Michael Hipp and others
        free software (LGPL) without any warranty but with best wishes

Directory: /usr/home/ishayahu/boroda/
Playing MPEG stream 1 of 1: Vivaldi.mp3 ...

MPEG 1.0 L III cbr128 44100 j-s

What I'm doing wrong?
 
First problem, no pid: How do you expect that to work? You are looking at the variable rc_pid, but nothing sets that variable. One option is to use the daemon utility, as yuri suggested. Another one is to use correct shell syntax: After you start a background command (which you do with the "&" after mpg123), in the next line use $!; that's the variable that /bin/sh sets to the PID of the most recent background process.

Second problem: Your sh command line is all screwed up. You need to think about the correct order of three things you write there: (a) a trailing & to make the command run in background, (b) the stdout redirection "> /dev/null" to send output to nowhere, and (c) the "2>&1" redirection of stderr to stdout, so that also goes to nowhere. To figure out the correct order, just read "man sh". In your case, you correctly have "> /dev/null" before "2>&1", but the "&" has to come at the very end so the whole pipeline should be "mpg123 ... > /dev/null 2>&1 &"
 
Back
Top