# Freebsd - Starting a sh with rc.d



## plankt (Sep 16, 2009)

Hello

I've tried for 2 days now and I do not get the information regarding rc.d

I've tried to write this startup script at least 10 times and it always fails.

What I'm trying to do is to start 2 programs when the computer starts but I can't even get it to work with one of them.

I usually start it like this:
"cd /usr/local/program"
"./program-name"
"./program2-name"

This was my last try:

```
#!/bin/sh

. /etc/rc.subr

name="program-name"
rcvar=`set_rcvar`

load_rc_config $name

start_cmd="${name}_start"

: ${program-name_enable}="YES"

program-name_start () {
    cd /usr/local/program ; ./program-name
}

run_rc_command "$1"
```
It is saved in "/usr/local/etc/rc.d/" (hope I typed the folder correct, it's the right one)

How would I write a rc.d script to start these 2 programs after boot? And can it be started by the user "Geffan"?

Thanks in advance


----------



## Alt (Sep 16, 2009)

1. Take other rc.d script and do as they doing =) Do not start program by yourself ("cd /usr/local/program ; ./program-name") - it will start when you do 'run_rc_command "$1"'
2. When you run a rc.d script use full path (/usr/local/etc/rc.d/program-name start), not relative (./program-name).


----------



## plankt (Sep 16, 2009)

I tried using the example and reading some others, can't find any good ones tho only the really advanced stuff.

This is my last edit:

```
#!/bin/sh

. /etc/rc.subr

name="program-name"
rcvar=`set_rcvar`
command="/usr/local/program/${name}"

load_rc_config $name
run_rc_command "$1"
```

I've added it to the folder "/usr/local/etc/rc.d" as the name "program-name" but it wont load at start.


----------



## Alt (Sep 16, 2009)

> #!/bin/sh
> #
> 
> # PROVIDE: programname
> ...


Also add to your /etc/rc.conf this:
programname_enable="YES"


----------



## plankt (Sep 16, 2009)

Sorry for second post but I didn't see an edit button anywhere.

I tested starting the program using "/usr/local/program/program-name start" but I need to be in the same folder for it to run since it loads config files and can't find them if I'm in another directory when starting it.


----------



## phoenix (Sep 16, 2009)

Write a wrapper shell script that does the cd and program start.  Put that in the same directory as the program.  And then edit the rc script to run the wrapper script.


----------



## Alt (Sep 16, 2009)

You can add this
programname_chdir="/blah/blah/"


----------



## phoenix (Sep 17, 2009)

Oooh, didn't know about that.     Thanks for the tip.


----------



## plankt (Sep 17, 2009)

This is what I have now but I don't see why it wouldn't start.
The program is supposed to start and keep running until the computer shuts off, this is the file in /usr/local/etc/rc.d/:

```
#!/bin/sh
#

# PROVIDE: name-of_program (does it matter if the name has an underscore in it?)
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown

. "/etc/rc.subr"

name-of_program_enable=${name-of_program_enable:-"NO"}

name=name-of_program
rcvar=`set_rcvar`
load_rc_config $name
name-of_program_chdir="/usr/local/myprogram/"
command=/usr/local/myprogram/name-of_program

run_rc_command "$1"
```

Should I see any errors  if it doesn't work? Currently I have the computer in another room doing everything by putty, I could hook it up to a display if that helped.


----------



## Alt (Sep 17, 2009)

> Also add to your /etc/rc.conf this:
> programname_enable="YES"



If you dont do this it will not start automatically.
You can try to force start it manually:
/usr/local/etc/rc.d/programname forcestart


----------



## plankt (Sep 17, 2009)

I have added the enable in rc.conf when you wrote it and when I try to forcestart it says "Permission denied." even tho I'm root user by using 'su' command.


----------



## Alt (Sep 17, 2009)

Can you copypaste all errors?


----------



## DutchDaemon (Sep 17, 2009)

Is /usr/local/etc/rc.d/programname actually executable?


----------



## phoenix (Sep 17, 2009)

You can also add *-x* to the first line of the rc script (#!/bin/sh -x) to get it to echo out everything that it is doing.  Then you can look to see where it is erroring out.


----------



## plankt (Sep 17, 2009)

It gives me this message:

```
name-of_program_enable=YES: not found
```
And the rc.d file is the last one I posted above


----------



## Lazarusrat (Sep 17, 2009)

plankt said:
			
		

> ```
> #!/bin/sh
> # PROVIDE: name-of_program (does it matter if the name has an underscore in it?)
> ```


I don't think the underscore matters, but I believe the hyphen might. Try replacing all your instances of name-of with name_of.


----------



## plankt (Sep 18, 2009)

Thank you all for the help, with the removal of hyphens it didn't say that it couldn't be found and when looking I noticed the file permissions weren't set right. I chmod the file so it had like the rest and now it loads on start.

I have one slight problem if someone knows what to do.

The program is a server so it listens to a port all the time and when it does it stops the startup of the computer, is it possible to run as an alternative process or what it's called? Or rather, it's that it's a GUI that stops it. I used to bypass it taking over the command window with this:

```
./programname >> textfile.txt
```
Is there a way to write it to a logfile instead of showing it or alternatively not show it at all?

And is it possible to start as a non root user?


----------

