# rc.d Script to start perl script



## sebile (Aug 5, 2012)

Hey 

I'm currently trying to write a simple rc.d script to start sockso (a music server written in Java) at boot time.
To start the java program in the background I've a perl script in /usr/local/etc/rc.d/sockso.pl 

To start the program I can use [CMD=""]perl sockso.pl start[/CMD], to stop it `perl sockso.pl stop`.

I've written a simple rc.d script 


```
#!/bin/sh
#
# PROVIDE: sockso
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# sockso_enable (bool): Set to NO by default.
#                       Set it to YES to enable it.

. /etc/rc.subr

name="sockso"
rcvar=${name}_enable
sockso_enable=${sockso_enable-"NO"}


# command="/usr/local/etc/rc.d/sockso.pl"
# command_interpreter="/usr/local/bin/perl"

start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config $name


sockso_start(){
echo "Starting sockso"
cd /usr/local/etc/rc.d
perl sockso.pl start

}

sockso_stop(){
echo "Stopping sockso"
cd /usr/local/etc/rc.d
perl sockso.pl stop
}

run_rc_command "$1"
```

When I call the script with the command `sh sockso start` it works.

But when I try `service sockso start` it doesn't work. All I get is the output of echo but the program doesn't start.

Does anyone have a clue what could be wrong in my script?

Thank you!


----------



## wblock@ (Aug 5, 2012)

Uncomment the command and command_interpreter lines, and use those variables in the code.


----------



## sebile (Aug 5, 2012)

I tried is this way (changes in red):


```
#!/bin/sh
#
# PROVIDE: sockso
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# sockso_enable (bool): Set to NO by default.
#                       Set it to YES to enable it.

. /etc/rc.subr

name="sockso"
rcvar=${name}_enable
sockso_enable=${sockso_enable-"NO"}


# command="/usr/local/etc/rc.d/sockso.pl"
# command_interpreter="/usr/local/bin/perl"

start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config $name


sockso_start(){
echo "Starting sockso"
[color="DarkRed"]$command_interpreter $command start[/color]

}

sockso_stop(){
echo "Stopping sockso"
[color="DarkRed"]$command_interpreter $command stop[/color]
}

run_rc_command "$1"
```

The perl script is called but the java program doesn't start via `service sockso start`.


To complete the picture I'll also post the perl script.


```
#!/usr/local/bin/perl

###########################################################
###########################################################
#
#  Edit this constant to point to your Sockso directory
#

use constant SOCKSO_DIR => "/var/sockso/sockso";

#
###########################################################
###########################################################

use strict;

my $cmd = shift();

if ( $cmd eq "start" ) { start(); }
elsif ( $cmd eq "stop" ) { stop(); }
elsif ( $cmd eq "restart" ) { restart(); }
 else { usage(); }

sub start {

        chdir( SOCKSO_DIR );
        system( 'sh linux.sh --nogui > /dev/null 2>&1 &' );

}

sub stop {

        my $pid = `ps x | grep sockso.jar | grep -v grep`;
        $pid =~ s/^\s*(\d+) .*/$1/;

        if ( $pid ) {
            `kill -9 $pid`;
        }

}


sub restart {
        stop();
        sleep( 2 );
        start();
}

sub usage {
        print <<EOF;

Usage: sockso (start|stop|restart)

EOF
}
```


----------



## phoenix (Aug 6, 2012)

Why the extra abstraction layer? Why not just write a sh equivalent as the rc.d script?


----------



## SirDice (Aug 6, 2012)

I don't get that either. Why write a shell script that starts/stops a perl script that starts/stops a service?

Skip the perl bit, it's just confusing clutter.


----------



## wblock@ (Aug 6, 2012)

In message #3, the lines are still commented.


----------



## sebile (Aug 6, 2012)

*Solved*

You're all right :stud To give you some explanation I used the perl script because in another forum someone mentioned how he got this to work on his freenas server and posted the perl script...

After working through "Practical rc.d scripting in BSD" and the man page of rc.subr and some forum posts about java processes and rc.d scripts I've got it to work using the following script:


```
!/bin/sh
#
# PROVIDE: sockso
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# sockso_enable (bool): Set to NO by default.
#                       Set it to YES to enable it.

. /etc/rc.subr

name="sockso"
rcvar=${name}_enable
sockso_enable=${sockso_enable-"NO"}
pidfile="/var/run/${name}.pid"
procname="/usr/local/openjdk7/bin/java"

start_cmd="sockso_start"

sockso_start()
{
        echo "Starting Sockso"
        JAVA="/usr/local/openjdk7/bin/java"
        cd /var/sockso/sockso
        ${JAVA} -jar sockso.jar --nogui > /dev/null 2>&1 &
        echo $! > /var/run/sockso.pid
}


load_rc_config $name
run_rc_command "$1"
```


The biggest problem was the path to the java executable, which wasn't found when using `service sockso start`. 

Now everything is working as I imagined. Thank you for your help and comments on my problem  .


----------



## lme@ (Aug 15, 2012)

But you shouldn't use a hardcoded path to the java version. Please use

```
JAVA_VERSION=1.7 /usr/local/bin/java ...
```


----------

