LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   Adding a script to /etc/init.d (https://www.linuxquestions.org/questions/ubuntu-63/adding-a-script-to-etc-init-d-617209/)

neoAKiRAz 01-29-2008 01:44 PM

Adding a script to /etc/init.d
 
Hi! I'm using Kubuntu 7.10, and I was trying to run MLDonkey (manually compiled) as a daemon. In order to do that I took the init.d script from an Ubuntu package, and put it in /etc/init.d. I also copied /etc/default/mldonkey-server with some options, which are read by the init.d script.

After that, I did:

Code:

sudo update-rc.d -f mldonkey-server start 98 2 3 4 5 . stop 20 0 1 6 .
which was the way the posinst script of the package registered the init.d script.

The script is quite simple and it works correctly running it with sudo:

Code:

sudo /etc/init.d/mldonkey-server start
it also works when I manually stop it, and even when the systems shutdown I'm certain the script is called. However, it seems that the system doesn't call the script at startup, since the mlnet daemon doesn't show up in ps -ef.

Maybe the script IS being called, but execution of mlnet is failing for some reason, which would be strange because I can run the script manually... in what log can I check if the script is correctly run? It prints "Starting MLDonkey: mlnet.", so I may be able to find it.

Is there anything else I'm missing?

Thanks a lot!

Disillusionist 01-29-2008 02:19 PM

remove the -f from the command:
Code:

sudo update-rc.d mldonkey-server start 98 2 3 4 5 . stop 20 0 1 6 .
The update-rc.d man page states:
Code:

OPTIONS
      -n    Don’t do anything, just show what we would do.

      -f    Force removal of symlinks even if /etc/init.d/name still exists.

Therefore the -f is removing the symlinks that are needed to automate the start/stop of the daemon

neoAKiRAz 01-29-2008 10:10 PM

You're right, I hadn't read the man page thoroughly. However, now I did

Code:

sudo update-rc.d mldonkey-server start 98 2 3 4 5 . stop 20 0 1 6 .
and the printed message was again

Code:

System startup links for /etc/init.d/mldonkey-server already exist.
Then rebooted, and the result was the same. What else could be going wrong? Is there any log to check? How does init.d work, the sole fact that the script is there and has the following text on it (I guess this is what's done by update-rc.d) is what should make it work?

Code:

### BEGIN INIT INFO
# Provides:          mldonkey-server
# Required-Start:    $network
# Required-Stop:    $network
# Should-Start:      $local_fs
# Should-Stop:      $local_fs
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Server for the mldonkey peer-to-peer downloader.
# Description:      Server for the mldonkey peer-to-peer downloader.
### END INIT INFO

Or are the names of the scripts that must be called somewhere else?

Thanks!

Disillusionist 01-30-2008 01:29 AM

You should have the following symlinks:

/etc/rc0.d/K20mldonkey-server
/etc/rc1.d/K20mldonkey-server
/etc/rc2.d/S98mldonkey-server
/etc/rc3.d/S98mldonkey-server
/etc/rc4.d/S98mldonkey-server
/etc/rc5.d/S98mldonkey-server
/etc/rc6.d/K20mldonkey-server

all should be pointing to /etc/init.d/mldonkey-server

I have a couple of questions to help your investigation

Type: runlevel at a command prompt.

What runlevel are you at?

What is run by the start function in the script?

Does the script contain any log files?

neoAKiRAz 01-30-2008 08:20 AM

I know this doesn't happen in Linux, but I restarted a second time this morning and it was working :S I don't know what might have happened...

The links are there:

Code:

neoakiraz@fcksys:/etc$ ls -l rc* | grep mldonkey
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 K20mldonkey-server -> ../init.d/mldonkey-server
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 K20mldonkey-server -> ../init.d/mldonkey-server
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 S98mldonkey-server -> ../init.d/mldonkey-server
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 S98mldonkey-server -> ../init.d/mldonkey-server
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 S98mldonkey-server -> ../init.d/mldonkey-server
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 S98mldonkey-server -> ../init.d/mldonkey-server
lrwxrwxrwx 1 root root  25 2008-01-19 03:00 K20mldonkey-server -> ../init.d/mldonkey-server

My runlevel is 2. Not sure what's this yet but I'll get into it.
Here is the piece of the script you were looking for, I think:

Code:

case "$1" in
  start|force-start)
    echo -n "Starting $DESC: $NAME"

    if [ "x$LAUNCH_AT_STARTUP" != "xtrue" ] && [ "x$1" = "xstart" ]; then
      echo " configuration file prevent $NAME to be started (use force-start)."
      exit 0
    fi

    if [ -z "$MLDONKEY_DIR" ] || [ ! -d "$MLDONKEY_DIR" ]; then
      if [ -z "$MLDONKEY_DIR" ]; then
        MLDONKEY_DIR="(unset)"
      fi
      echo " $MLDONKEY_DIR is not a valid directory."
      exit 1
    fi

    if [ ! -f "$MLDONKEY_DIR/downloads.ini" ]; then
      echo " $MLDONKEY_DIR/downloads.ini is not a valid file."
      exit 1
    fi

    USER=`/usr/bin/stat --format="%U" "$MLDONKEY_DIR/downloads.ini"`
    WRAPPER_OPTIONS="$WRAPPER_OPTIONS --user $USER"

    start-stop-daemon --start $WRAPPER_OPTIONS \
    --pidfile $PIDFILE --background --exec $EXEC \
    -- -pid $PIDDIR > $LOGFILE 2>&1

    echo "."
  ;;

According to the definition of $LOGFILE, that log is in /var/log/mldonkey/mlnet.log. However that file is empty, and the program is currently logging in ~/.mldonkey/mlnet.log. Is that because only the start-stop-daemon outputs are redirected to the $LOGFILE or because the program later assigns a different file to log into?

The problem is solved but I still had this doubts :) Thanks!

neoAKiRAz 01-30-2008 12:43 PM

Sorry, not working again... I don't think I did anything. Restarted 3 times in a row and the executable mlnet is not running. However, I'm not certain that the script is not being run.

What I know is that /var/log/mldonkey/mlnet.log is not even being created. The logfile to which the program switchs to doesn't get created either.
The symlinks are still there...

Here's the whole script, just in case:

Code:

#!/bin/sh
#
# Original file :
#    Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#    Modified for Debian GNU/Linux
#    by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:  @(#)skeleton  1.9.1  08-Apr-2002  miquels@cistron.nl
#
#
# This file has been rewritten by Sylvain Le Gall <gildor@debian.org>
# and Samuel Mimram <smimram@debian.org> for the mldonkey package.
#
### BEGIN INIT INFO
# Provides:          mldonkey-server
# Required-Start:    $network
# Required-Stop:    $network
# Should-Start:      $local_fs
# Should-Stop:      $local_fs
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Server for the mldonkey peer-to-peer downloader.
# Description:      Server for the mldonkey peer-to-peer downloader.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=mlnet
EXEC=/usr/local/bin/$NAME
DESC="MLDonkey"
CONFIG=/etc/default/mldonkey-server
PIDDIR=/var/run/mldonkey
PIDFILE=$PIDDIR/$NAME.pid
LOGFILE=/var/log/mldonkey/$NAME.log

test -x $WRAPPER || exit 0

test -e $CONFIG || exit 0

set -e

. $CONFIG

# /var/run might be on tempfs, see #354701.
if [ ! -d /var/run/mldonkey ]; then
    mkdir -m 755 /var/run/mldonkey
fi
if [ -n "$MLDONKEY_USER" ] && [ -n "$MLDONKEY_GROUP" ]; then
    chown $MLDONKEY_USER:$MLDONKEY_GROUP /var/run/mldonkey
fi

WRAPPER_OPTIONS=""

# Set configuration value, from CONFIG
if [ -n "$MLDONKEY_USER" ] && [ -n "$MLDONKEY_GROUP" ]; then
    WRAPPER_OPTIONS="$WRAPPER_OPTIONS --chuid $MLDONKEY_USER:$MLDONKEY_GROUP"
fi

if [ -n "$MLDONKEY_DIR" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --chdir $MLDONKEY_DIR"
fi

if [ -n "$MLDONKEY_GROUP" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --group $MLDONKEY_GROUP"
fi

if [ -n "$MLDONKEY_UMASK" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --umask $MLDONKEY_UMASK"
fi

if [ -n "$MLDONKEY_NICENESS" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --nicelevel $MLDONKEY_NICENESS"
fi

case "$1" in
  start|force-start)
    echo -n "Starting $DESC: $NAME"

    if [ "x$LAUNCH_AT_STARTUP" != "xtrue" ] && [ "x$1" = "xstart" ]; then
      echo " configuration file prevent $NAME to be started (use force-start)."
      exit 0
    fi

    if [ -z "$MLDONKEY_DIR" ] || [ ! -d "$MLDONKEY_DIR" ]; then
      if [ -z "$MLDONKEY_DIR" ]; then
        MLDONKEY_DIR="(unset)"
      fi
      echo " $MLDONKEY_DIR is not a valid directory."
      exit 1
    fi

    if [ ! -f "$MLDONKEY_DIR/downloads.ini" ]; then
      echo " $MLDONKEY_DIR/downloads.ini is not a valid file."
      exit 1
    fi

    USER=`/usr/bin/stat --format="%U" "$MLDONKEY_DIR/downloads.ini"`
    WRAPPER_OPTIONS="$WRAPPER_OPTIONS --user $USER"

    start-stop-daemon --start $WRAPPER_OPTIONS \
    --pidfile $PIDFILE --background --exec $EXEC \
    -- -pid $PIDDIR > $LOGFILE 2>&1

    echo "."
  ;;

  stop)
    echo -n "Stopping $DESC: $NAME"
    start-stop-daemon --stop --oknodo --pidfile $PIDFILE
    echo "."
  ;;

  force-reload|restart)
    $0 stop
    $0 start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-reload|force-start}" >&2
    exit 1
  ;;
esac

exit 0

Any idea??

Disillusionist 01-30-2008 03:00 PM

runlevel 2 is fine, so long as one of the startup symlinks was in rc2.d:
Code:

ls -l /etc/rc2.d/S98mldonkey-server
When you run:
Code:

sudo /etc/init.d/mldonkey-server start
What, if any, output do you get?

Does the mlnet process start?
Code:

ps -ef|grep mlnet
Can you post the config file?
CONFIG=/etc/default/mldonkey-server

Is LAUNCH_AT_STARTUP=true

neoAKiRAz 01-30-2008 11:33 PM

I rebooted one more time, and this time it worked... I don't know what's with it. The outputs are:

Code:

neoakiraz@fcksys:~$ sudo /etc/init.d/mldonkey-server start
Starting MLDonkey: mlnet.

neoakiraz@fcksys:~$ sudo /etc/init.d/mldonkey-server stop
Stopping MLDonkey: mlnet.

After manually running the script with start, the mlnet process appears to run:

Code:

neoakiraz@fcksys:~$ ps -ef | grep mlnet
1000    13822    1 19 03:18 ?        00:00:03 /usr/local/bin/mlnet -pid /var/run/mldonkey
1000    13825 13664  0 03:18 pts/1    00:00:00 grep mlnet

The config file is the following:

Code:

MLDONKEY_DIR=/home/neoakiraz/.mldonkey
MLDONKEY_USER=neoakiraz
MLDONKEY_GROUP=neoakiraz
MLDONKEY_UMASK=0022
MAX_ALIVE=48
LAUNCH_AT_STARTUP=true
MLDONKEY_NICENESS=15

Here I modified some things. First the mldonkey working dir. Then the user and group the process will be run under. They recommend (at least is the default option) to create a new 'mldonkey' user/group and run it under those. However if I did that I couldn't use the working directory I wanted. I could move it to /var/lib/mldonkey, but the incoming, temp and share have to be in the home partition, because it's where the free space is. So I could create a folder with mldonkey permisions inside my /home/neoakiraz home directory. But it's kind of messy. Maybe I could create a home folder for mldonkey, but I really didn't see the point (or I didn't understood the need) of creating another user, since mine is the only real account. I also modified the niceness, because I noticed how sometimes the process it's about 5-7% of my CPU, so I lowered it's priority.

I hope I explained my self well :rolleyes: Thanks for your help... and patience!

Greetz

Disillusionist 01-31-2008 02:05 AM

When running daemon processes, it is considered safer to have a user & group that is only used for that daemon.

This helps you to protect your system in the event that the daemon process has a security flaw, as the daemon is run as a user that has no access to anything else (whether it's system files or your own personal data).

This is one of the things that helps keep UNIX / Linux secure.

I'm glad that the daemon seems to be working normally now, the only other suggestion I considered was to create a seperate logfile to ensure that the script is reaching set points during system startup.

It is also possible that the daemon is starting during boot, but then terminating at some point.

If you are still experiencing problems, you might want to take a look at your system logs.

neoAKiRAz 01-31-2008 11:34 AM

Quote:

When running daemon processes, it is considered safer to have a user & group that is only used for that daemon.
Great to know that. So I'll try creating the user and group again. And then I guess I could use /var/log/mldonkey as the program working directory. But as the free space is on the /home partition, I'm note sure what to do about the incoming, shared and temp directories. Should I create a home folder for mldonkey (/home/mldonkey) and the three directories there?

And in that case, will I be able, using my user, to move the downloaded files from /home/mldonkey (owned by mldonkey) to my home directory? Does the owner of those files get changed to my user...? Is that what MLDONKEY_UMASK is about?

Quote:

It is also possible that the daemon is starting during boot, but then terminating at some point.
I wanted to check that, but what I haven't been able to find is where the output of the script (when it is called at startup) gets logged. I tried something like:

Code:

cat /var/log/* | grep MLDonkey
or just grep Starting (trying to find any other daemons) and nothing shows up. Any idea about this?

Disillusionist 01-31-2008 03:22 PM

Quote:

Originally Posted by neoAKiRAz (Post 3041268)
But as the free space is on the /home partition, I'm note sure what to do about the incoming, shared and temp directories. Should I create a home folder for mldonkey (/home/mldonkey) and the three directories there?

By all means, create a home directory for mldonkey and use this if this is where you have space available.

Quote:

Originally Posted by neoAKiRAz (Post 3041268)
And in that case, will I be able, using my user, to move the downloaded files from /home/mldonkey (owned by mldonkey) to my home directory? Does the owner of those files get changed to my user...? Is that what MLDONKEY_UMASK is about?

You are correct, but you could set a umask of 0002 and add yourself to the mldonkey group.

To check if the script is starting at boot-up, I would suggest makeing a few changes to the startup script (additional logging)

Code:

#!/bin/sh
#
# Original file :
#    Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#    Modified for Debian GNU/Linux
#    by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:  @(#)skeleton  1.9.1  08-Apr-2002  miquels@cistron.nl
#
#
# This file has been rewritten by Sylvain Le Gall <gildor@debian.org>
# and Samuel Mimram <smimram@debian.org> for the mldonkey package.
#
### BEGIN INIT INFO
# Provides:          mldonkey-server
# Required-Start:    $network
# Required-Stop:    $network
# Should-Start:      $local_fs
# Should-Stop:      $local_fs
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Server for the mldonkey peer-to-peer downloader.
# Description:      Server for the mldonkey peer-to-peer downloader.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=mlnet
EXEC=/usr/local/bin/$NAME
DESC="MLDonkey"
CONFIG=/etc/default/mldonkey-server
PIDDIR=/var/run/mldonkey
PIDFILE=$PIDDIR/$NAME.pid
LOGFILE=/var/log/mldonkey/$NAME.log
STARTLOG=/tmp/$NAME_startup.log

echo "`date` - Running $DESC $1" >> $STARTLOG

test -x $WRAPPER || exit 0

test -e $CONFIG || exit 0

set -e

. $CONFIG

# /var/run might be on tempfs, see #354701.
if [ ! -d /var/run/mldonkey ]; then
    mkdir -m 755 /var/run/mldonkey
fi
if [ -n "$MLDONKEY_USER" ] && [ -n "$MLDONKEY_GROUP" ]; then
    chown $MLDONKEY_USER:$MLDONKEY_GROUP /var/run/mldonkey
fi

WRAPPER_OPTIONS=""

# Set configuration value, from CONFIG
if [ -n "$MLDONKEY_USER" ] && [ -n "$MLDONKEY_GROUP" ]; then
    WRAPPER_OPTIONS="$WRAPPER_OPTIONS --chuid $MLDONKEY_USER:$MLDONKEY_GROUP"
fi

if [ -n "$MLDONKEY_DIR" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --chdir $MLDONKEY_DIR"
fi

if [ -n "$MLDONKEY_GROUP" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --group $MLDONKEY_GROUP"
fi

if [ -n "$MLDONKEY_UMASK" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --umask $MLDONKEY_UMASK"
fi

if [ -n "$MLDONKEY_NICENESS" ]; then
  WRAPPER_OPTIONS="$WRAPPER_OPTIONS --nicelevel $MLDONKEY_NICENESS"
fi

case "$1" in
  start|force-start)
    echo -n "Starting $DESC: $NAME"
    echo -n "Starting $DESC: $NAME" >> $STARTLOG

    if [ "x$LAUNCH_AT_STARTUP" != "xtrue" ] && [ "x$1" = "xstart" ]; then
      echo " configuration file prevent $NAME to be started (use force-start)."
      exit 0
    fi

    if [ -z "$MLDONKEY_DIR" ] || [ ! -d "$MLDONKEY_DIR" ]; then
      if [ -z "$MLDONKEY_DIR" ]; then
        MLDONKEY_DIR="(unset)"
      fi
      echo " $MLDONKEY_DIR is not a valid directory."
      exit 1
    fi

    if [ ! -f "$MLDONKEY_DIR/downloads.ini" ]; then
      echo " $MLDONKEY_DIR/downloads.ini is not a valid file."
      exit 1
    fi

    USER=`/usr/bin/stat --format="%U" "$MLDONKEY_DIR/downloads.ini"`
    WRAPPER_OPTIONS="$WRAPPER_OPTIONS --user $USER"

    start-stop-daemon --start $WRAPPER_OPTIONS \
    --pidfile $PIDFILE --background --exec $EXEC \
    -- -pid $PIDDIR > $LOGFILE 2>&1

    echo "."
  ;;

  stop)
    echo -n "Stopping $DESC: $NAME"
    echo -n "Stopping $DESC: $NAME" >> $STARTLOG

    start-stop-daemon --stop --oknodo --pidfile $PIDFILE
    echo "."
  ;;

  force-reload|restart)
    $0 stop
    $0 start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-reload|force-start}" >&2
    exit 1
  ;;
esac

echo "Checking for mlnet process:" >> $STARTLOG

ps -ef|grep ml[n]et >> $STARTLOG

exit 0


neoAKiRAz 02-02-2008 10:00 AM

Thanks a lot! I now run mlnet as the mldonkey user, found out about umask, groups and some more about permissions... :)

And also found the reason why the mlnet daemon would "sometimes" run and other times not. It seems that my webcam fried, so it caused some "usb operations" such as lsusb to hang for a pretty long time. In my case, one of the init.d scripts prior to mldonkey-server called logitech_applet, which configures my Logitech mouse. This program hanged for a while and that caused the rest of the scripts to delay their execution.

The only thing I noticed was that when I move files from /home/mldonkey to my home directory, as my user, the owner remains as mldonkey. Is this the expected behavior?

Thanks a lot! :)

Disillusionist 02-02-2008 12:03 PM

As you are moving the files the permissions will stay the same, however you can use chown to reset them if you wish:

Code:

chown neoakiraz:neoakiraz myfile
or for a whole directory and its contents:
Code:

chown -R neoakiraz:neoakiraz mydir

neoAKiRAz 02-03-2008 09:58 AM

Yes, could use chown. But I thought there was an automatic way to do it... if there's not I guess the files are supposed to stay like that :)
Thanks a lot for all your help!


All times are GMT -5. The time now is 10:08 PM.