# [solved]Basic runscript

## defenderBG

I need an init runscript, that runs a java program (unlike bash, where you can just execute any script from everywhere in java you need to be in the root of the program and execute it from there, which means in PATH_TO_MKN)

I jave up after a few unsuccesfull tries,

what i need extra is starting the daemon as user X, saving deleting pid, thats it.

I tried:

```

#!/sbin/runscript

PATH_TO_MKN="/home/orlin/workspace/myKeyNote/MyKeyNote"

JAVA="java"

CONFIG=etc/mykeynote/MyKeyNote.conf

pidfile=/var/run/mykeynote.pid

executable="cd $PATH_TO_MKN && $JAVA keynote.Main $CONFIG"

start(){

#     ebegin "Starting MyKeyNote"

     start-stop-daemon --start --chdir "/home/orlin/workspace/myKeyNote/MyKeyNote" --exec "java keynote.Main etc/mykeynote/MyKeyNote.conf" --pidfile /var/run/mykeynote.pid

     eend $?

}

stop(){

     ebegin "Stopping MyKeyNote"

     kill "echo $pid"

     rm $pid -rf

     eend $?    

}

```

but failed with: 

/sbin/start-stop-daemon: stat java keynote.Main etc/mykeynote/MyKeyNote.conf: no such file or directory

no... it is the right directory and the path to the executable is right as well...

pps: i have tried already to google and always had a stupid error, so pls don't send me links, just an answer!Last edited by defenderBG on Wed Jan 23, 2008 5:47 am; edited 1 time in total

----------

## Sadako

"etc/mykeynote/MyKeyNote.conf"

Are you not missing a forward slash before that?

start-stop-daemon can handle starting the process under a particular user, and I think you already have enough for it to handle the pid file for you...

You should be using start-stop-daemon to stop the daemon too, but I'll assume you gave up before doing that part.

----------

## defenderBG

i haven't installed the config to /, it is located in /home/orlin/workspace/myKeyNote/MyKeyNote/etc/mykeynote...

the init script is not in /etc/init.d as well... is there a problem whit that? (for now i get the same error no matter if it is in /etc/init.d or in the other dir)

when my program is somewhat more finished i will make an install and move all the files to their corresponding places.

and yes... i gave up in solving it myself (this is why there is this thread :/ )

----------

## Sadako

 *defenderBG wrote:*   

> i haven't installed the config to /, it is located in /home/orlin/workspace/myKeyNote/MyKeyNote/etc/mykeynote...
> 
> the init script is not in /etc/init.d as well... is there a problem whit that?
> 
> when my program is somewhat more finished i will make an install and move all the files to their corresponding places.
> ...

 

Okay, so how about for the moment using the full path in the script, ie;

```
/home/orlin/workspace/myKeyNote/MyKeyNote/etc/mykeynote/MyKeyNote.conf
```

What are you assuming the current working directory according to the script will be?

That's really not something you should rely upon...

I would stick the init script in /etc/init.d and test it from there, as there really isn't any reason for not doing so.

----------

## timeBandit

 *defenderBG wrote:*   

> I tried:
> 
> ```
> start-stop-daemon --start --chdir "/home/orlin/workspace/myKeyNote/MyKeyNote" --exec "java keynote.Main etc/mykeynote/MyKeyNote.conf" --pidfile /var/run/mykeynote.pid
> ```
> ...

 

You can't pass arguments to the daemon simply by quoting the argument of the --exec switch. By doing so, you told start-stop-daemon it should launch a program named "java keynote.Main etc/mykeynote/MyKeyNote.conf"--treating that entire string as the program filename, spaces and all--which obviously did not exist. Hence, the error message from stat.

The right way: Use the -- switch to separate start-stop-daemon arguments from program arguments.

```
start-stop-daemon --start --chdir "/home/orlin/workspace/myKeyNote/MyKeyNote" --pidfile /var/run/mykeynote.pid --exec /usr/bin/java -- keynote.Main etc/mykeynote/MyKeyNote.conf
```

----------

## defenderBG

timeBandit, thanks

now I am able to start the program... however i assume i have done something wrong in the stop part...

```
#!/sbin/runscript

PATH_TO_MKN="/home/orlin/workspace/myKeyNote/MyKeyNote"

JAVA="/usr/bin/java"

CONFIG=etc/mykeynote/MyKeyNote.conf

pidfile=/var/run/mykeynote.pid

executable="cd $PATH_TO_MKN && $JAVA keynote.Main $CONFIG"

start(){

#     ebegin "Starting MyKeyNote"

     start-stop-daemon --start --user orlin --pidfile $pidfile  --chdir "$PATH_TO_MKN" --exec $JAVA -- keynote.Main $CONFIG --daemon &

     true

     eend $?

}

stop(){

     ebegin "Stopping MyKeyNote"

     start-stop-daemon --stop --user orlin --pidfile $pidfile --chdir "$PATH_TO_MKN" --exec $JAVA -- keynote.Main $CONFIG

     eend $?

}

```

when i execute the start... it shows everything ok...

when i execute the stop part... it shows:

* WARNING:  mykeynote.old has not yet been started.

Can you help me get this working

(the exec ... &

true &

is used to somewhat get the java to daemonize, even if i close in/out/err it still does not demonize)

----------

## timeBandit

Java is a bit of a nuisance for two reasons: (1) it won't daemonize itself, (2) the process executable name is not the same as the command name. So you need to game start-stop-daemon a little to get the behavior you want.

start-stop-daemon --start --pidfile $pidfile --exec $JAVA --background --make-pidfile -- ...

This forces the JVM process into the background and forces start-stop-daemon to create the PID file (since Java won't).

start-stop-daemon --stop --pidfile $pidfile --name java

This looks for a process match by confirming the process name in /proc/$(<$pidfile)/stat is java. You need this because a running JVM's process name is neither $JAVA (which you used to start it), nor the file linked from /proc/$(<$pidfile)/exe (which the --exec switch checks).

Finally, to avoid the "foo has not been started" warning, the init script must exist in /etc/init.d and you have to start it from there, to ensure a proper symlink gets created in /var/lib/init.d/started.

PS: Get rid of the & and true, also.  :Wink: 

----------

## defenderBG

well... the script starts ok. it even stops the service right... just that i get the error: (but the program is stopped)

/etc/init.d/mykeynote stop

 * Service mykeynote stopping                                                                                           [ !! ]

 * ERROR:  mykeynote failed to stop

and the pid is still there...

upmoast weard is that restart works kind of ok and... the service is still running as root and not user orlin... :/ (the shutdown of the program is 15 sec... pure waiting)

Hyperius orlin # /etc/init.d/mykeynote restart

 * Service mykeynote stopping                                                                                           [ !! ]

 * ERROR:  mykeynote failed to stop

Hyperius orlin # /etc/init.d/mykeynote restart

 * Service mykeynote stopping                                                                                           [ ok ]

 * Service mykeynote stopped

 * Service mykeynote starting                                                                                           [ ok ]

 * Service mykeynote started

```
#!/sbin/runscript

PATH_TO_MKN="/home/orlin/workspace/myKeyNote/MyKeyNote"

JAVA="/usr/bin/java"

CONFIG=etc/mykeynote/MyKeyNote.conf

pidfile=/var/run/mykeynote.pid

executable="cd $PATH_TO_MKN && $JAVA keynote.Main $CONFIG"

start(){

#     ebegin "Starting MyKeyNote"

     start-stop-daemon --start --pidfile $pidfile --make-pid --background --user orlin  --chdir "$PATH_TO_MKN" --exec $JAVA -$

     eend $?

}

stop(){

#     ebegin "Stopping MyKeyNote"

     start-stop-daemon --stop --pidfile $pidfile --name java

     eend $?

}

```

----------

## defenderBG

well... i managed to get it solved... so let's report it:

the problem with stopping was, that my program needs about 15 sec to stop and normaly the start-stop-daemon does not wait that long... so i had to add --retry 20 (just going on the safer side) and the problem was solved.

to set the daemon running with specific rights you need not to user --user but --chuid...

so... this is the final working script  :Smile: 

```

#!/sbin/runscript

PATH_TO_MKN="/home/orlin/workspace/myKeyNote/MyKeyNote"

JAVA="/usr/bin/java"

CONFIG=etc/mykeynote/MyKeyNote.conf

pidfile=/var/run/mykeynote.pid

USER="orlin:users"

TIMEOUT="20"

start(){

#     ebegin "Starting MyKeyNote"

     start-stop-daemon --start --pidfile $pidfile --make-pid --background --chuid "$USER"  --chdir "$PATH_TO_MKN" --exec $JAV$

     eend $?

}

stop(){

#     ebegin "Stopping MyKeyNote"

     start-stop-daemon --stop --pidfile $pidfile --retry $TIMEOUT --name $JAVA

     eend $?

}

```

ps: many thanks to timeBandit... without his help i would have given up a long time ago. if you are to come in europe write me so that i can buy you a beer  :Wink: 

----------

## timeBandit

 *defenderBG wrote:*   

> ps: many thanks to timeBandit... without his help i would have given up a long time ago. if you are to come in europe write me so that i can buy you a beer 

 

Glad I could help, though I'm sorry I went silent on your last two problems (effective user & stopping the daemon).  :Embarassed:  Sometimes I can't immediately act on posts and quite forget to revisit them. But I'll rarely forget a good beer.   :Wink:  Cheers!

----------

