LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding start up script in rcX.d --> Via update-rc SCRIPT defaults .... :-( (https://www.linuxquestions.org/questions/linux-newbie-8/adding-start-up-script-in-rcx-d-via-update-rc-script-defaults-889582/)

jv2112 07-02-2011 03:50 PM

Adding start up script in rcX.d --> Via update-rc SCRIPT defaults .... :-(
 
I am trying to learn how to add start up scripts and I am not quote getting it. So far -->

1. Wrote a basic functional script ( well it works when invoked in bash)
2. ran update-rc Linkup defaults
3. checked sysv-rc & it is set to run in the correct run levels.

-----> No dice.

I have done some research on the LSB style headers that are required but I am not sure I am 100% on target.


Any & all direction would be appreciated. :hattip:

Process commands:

Quote:


sudo vi Linkup # Drafted script

sudo cp -v /home/joe/Scripts/Linkup /etc/init.d/ # moved to the needed directory

sudo update-rc.d Linkup defaults # set up for rcXd sequencing.

sysv-rc-conf : checked if it was working

sudo shutdown -r now # rebooted

No dice ..................................................... :redface:


Script :

Quote:

#! /bin/bash

### BEGIN INIT INFO
# Provides: linkup
# Required-Start: $remote_fs $syslog $ssh $sshfs
# Required-Stop: $remote_fs $syslog $ssh $sshfs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: sshfs - Needed drives
# Description: Lan - Needed drives
### END INIT INFO


# Little-Tux

sshfs amy@192.168.1.6:/ /media/Little-Tux/

# XMBC

sshfs media@192.168.1.3:/ /media/XMBC/


Distro : Debian Testing

aysheaia 07-02-2011 04:55 PM

LSB headers look good. It is rather that your script lacks start and stop functions.

On a system with SysV init scripts, the init process executes the script given on the sysinit action line, and after the script on the runlevel line.
On debian :
Code:

si::sysinit:/etc/init.d/rcS
[...]
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
[...]

/etc/init.d/rcS script invokes /etc/init.d/rc script

Look at /etc/init.d/rc script.
You will see that, according to the runlevel parameter, all scripts in /etc/rc$runlevel/S (for start) or /etc/rc$runlevel/K (for stop) will be executed, with start (or stop) as first parameter.

So, add your own start and stop functions in your script, and all will be fine

jv2112 07-02-2011 05:39 PM

Thanks for the reply and assistance. Can you offer any suggestions on syntax / code ?

I made an attempt but no dice. :newbie:


Quote:


#! /bin/bash

### BEGIN INIT INFO
# Provides: linkup
# Required-Start: $remote_fs $syslog $ssh $sshfs
# Required-Stop: $remote_fs $syslog $ssh $sshfs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: sshfs - Needed drives
# Description: Lan - Needed drives
### END INIT INFO



# Start the service
start() {
/etc/init.d/Linkup &
}

# Restart the service Linkup
stop() {
/etc/init.d/Linkup stop
}



# Little-Tux

sshfs amy@192.168.1.6:/ /media/Little-Tux/

# XMBC

sshfs media@192.168.1.3:/ /media/XMBC/






aysheaia 07-02-2011 06:58 PM

Well, after reading myself, what I said is really true only for the stop case... Sorry for the mistake.

Anyway, it is better to write an SysVInit script according to usual standards (managing parameters start, stop, ...) like this one (not tested) :
Code:

#! /bin/bash
### BEGIN INIT INFO
# Provides: linkup
# Required-Start: $remote_fs $syslog $ssh $sshfs
# Required-Stop: $remote_fs $syslog $ssh $sshfs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: sshfs - Needed drives
# Description: Lan - Needed drives
### END INIT INFO

SCRIPTNAME=linkup
PATH=/sbin:/usr/sbin:/bin:/usr/bin

case "$1" in
  start)
        echo -n "Starting $SCRIPTNAME"
        # Little-Tux
        #you could need to give the absolute path to sshfs
        sshfs amy@192.168.1.6:/ /media/Little-Tux/
        # XMBC
        #you could need to give the absolute path to sshfs
        sshfs media@192.168.1.3:/ /media/XMBC/

        # Alternative, if you need to execute it from another user, for example called toto :
        # Little-Tux
        #you could need to give the absolute path to sshfs
        #/bin/su - toto -c "sshfs amy@192.168.1.6:/ /media/Little-Tux/"
        # XMBC
        #you could need to give the absolute path to sshfs
        #/bin/su - toto -c "sshfs media@192.168.1.3:/ /media/XMBC/"

        echo "$SCRIPTNAME started"
        ;;
  stop)
        echo -n "Stopping $SCRIPTNAME"
        umount -a -t fuse.sshfs

        echo "$SCRIPTNAME stopped"
        ;;

  *)
        echo "Usage: /etc/init.d/$SCRIPTNAME {start|stop}"
        exit 1
esac

exit 0


BUT, what could still be a problem is :
- sshfs command may not be in the PATH during init (so change the PATH variable or give the full path)
- Do you have created a public key with no passphrase between the local root user (which should be the one executing the script during init) and the amy user of 192.168.1.6 ?
Otherwise, the script will wait for an interactive user input, which cannot be done at boot time
- Same question and remark, this time between the local root user and the media user of 192.168.1.3

Remarks :
- If the sshfs command is not intended to be executed by the root user, you will have to use something like su -c, as shown in the script
- What miss in this script is a mechanism to forbid to execute several times this script (ans so mounting several times the same filesystems)

aysheaia 07-03-2011 05:03 AM

Edit #1 :
umount -a -t fuse.sshfs
could be replaced by
fusermount -u /media/Little-Tux/
fusermount -u /media/XMBC/
in stop function

Edit #2 :
Rather than using an init script, you could directly add relevant lines in /etc/fstab
For example, with yourlocaluid and yourlocalgid to be replaced :
sshfs#amy@192.168.1.6:/ /media/Little-Tux/ fuse user,uid=yourlocaluid,gid=yourlocalgid,exec,idmap=user,noauto,allow_other 0 0
sshfs#media@192.168.1.3:/ /media/XMBC/ fuse user,uid=yourlocaluid,gid=yourlocalgid,exec,idmap=user,noauto,allow_other 0 0

Edit #3 :
mmh, the /etc/fstab solution does not work (the network must not be up. I added noauto option, so that it does not automatically mount at boot time

jv2112 07-04-2011 05:47 AM

Thanks for taking the time to help me. I appreciate the effort. I tried your modified version and still no dice.

Quote:


BUT, what could still be a problem is :
- sshfs command may not be in the PATH during init (so change the PATH variable or give the full path)
- Do you have created a public key with no passphrase between the local root user (which should be the one executing the script during init) and the amy user of 192.168.1.6 ?
Otherwise, the script will wait for an interactive user input, which cannot be done at boot time
- Same question and remark, this time between the local root user and the media user of 192.168.1.3

Remarks :
- If the sshfs command is not intended to be executed by the root user, you will have to use something like su -c, as shown in the script
- What miss in this script is a mechanism to forbid to execute several times this script (ans so mounting several times the same filesystems)
07-02-11 06:39 PM

I shared my key with the machines in question & I can ssh or sshfs without a password. However I just thought that I would need to do that for root?


............ I am going to give that a try.....:scratch:

jv2112 07-04-2011 06:19 AM

I tried generating a key (ssh-keygen ) for root then passing to the ".ssh/authorized_keys" of the two machines and :rolleyes: it worked, sort of.

:scratch: But it would only let root access. I could not even change owner or mode as root. So I then took the route of changing the script to your second suggestion ( su to another user to process the commands) and it worked like a charm :D



Thanks again for all of your help. :hattip:


All times are GMT -5. The time now is 02:27 PM.