How to make a process run at startup

Hi,

I am trying to make a file run at startup (e.g. runlevel 5). Here is what I done;

Copied file to /etc/init.d/rc5.d with these permissions:

-rwx------ 1 root root 274 Mar 17 08:33 noip2.sh

Contents of this file is:


#! /bin/sh
# . /etc/rc.d/init.d/functions  # uncomment/modify for your killproc
case "$1" in
start)
echo "Starting noip2."
/usr/local/bin/noip2
;;
stop)
echo -n "Shutting down noip2."
killproc -TERM /usr/local/bin/noip2
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0

And the file this is supposed to run is in /usr/local/bin/noip2 with the permissions as:

-rwxr-xr-x 1 root root 80431 Mar 17 08:16 noip2

However, this didn’t worked for me. What can I do?

Edit: I can run shell file by invoking it with terminal. So there is no problem with the file

You’re missing some parts. Your script has to be LSB (and better also chkconfig) compliant. Look at that one:
automatically recompile kernel modules after kernel update.
You can look at any script in /etc/init.d as an example, but that one is simple. There should also be a sample script somewhere … don’t remember how it’s called.

I also have a function (part of a larger script) which makes any foreign init script LSB compliant. I haven’t used it yet on 11.4 as all scripts looked fine. I don’t need to explain it to you, as I know you are good.

function lsb {

for iscript in `find /etc/init.d -maxdepth 1 -type f -executable ! -name "*.local" ! -name "*.orig"` ; do
	 -f ${iscript}.orig ] && continue
	grep -q '### BEGIN INIT INFO' $iscript && continue
	case $(basename $iscript) in
		single|halt|powerfail|rc|boot) continue ;;
		*)
		cp $iscript{,.orig}
		echo "- making $iscript LSB compliant" 
		s=$(basename $iscript)
		sed -n '1,/^^# ]/p' $iscript.orig | sed '$d' > $iscript
		cat >> $iscript << EOFLSB
# LSB compliant service control script
#
### BEGIN INIT INFO
# Provides:			$s
# Required-Start:		\$remote_fs \$all
# Should-Start:			
# Required-Stop:		
# Should-Stop:			
# Default-Start:		3 5
# Default-Stop:			0 1 2 6
# Short-Description:		$s
# Description:			Starts and stops $s
### END INIT INFO

EOFLSB
		sed -n '/^^# ]/,$p' $iscript.orig >> $iscript
		;;
	esac
done
}

IMHO This thread belongs to the development forum.

Thanks for your kind replies, but what you are saying is kind of too advanced for me. I also checked the link you gave, but it really didn’t ring any bells for me. Can you explain it as “Making LSB compliant init script for dummies”, or better yet, if it is not very time consuming to do, can you produce final product using your codes?

On 2011-03-17 09:06, yasar11732 wrote:
>
> Hi,
>
> I am trying to make a file run at startup (e.g. runlevel 5). Here is
> what I done;

Reading material:

man init.d
/etc/init.d/README
/etc/init.d/skeleton
> http://doc.opensuse.org/products/opensuse/openSUSE/opensuse-reference/cha.boot.html#sec.boot.init


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

You script should look like that. That’s what I said that you were missing some parts. Replace the text in red. I thought you would have come to that conclusion after taking a look at the other script I linked to.

#! /bin/sh
# uncomment/modify for your killproc

#: Title       :  bla bla bla
#: Date Created: Thu Mar 17 03:40:41 PDT 2011
#: Last Edit   : Thu Mar 17 03:40:41 PDT 2011
#: Author      : yasar11732
#: Version     : 1.0
#: Description : bla bla bla


# chkconfig: 35 30 70
# description: bla bla bla
#
### BEGIN INIT INFO
# Provides:       this script 
# Required-Start: $syslog
# Required-Stop:
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Short-Description: bla bla bla
### END INIT INFO

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

case "$1" in
start)
echo "Starting noip2."
/usr/local/bin/noip2
;;
stop)
echo -n "Shutting down noip2."
killproc -TERM /usr/local/bin/noip2
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0

On 2011-03-17 11:36, yasar11732 wrote:
>
> Thanks for your kind replies, but what you are saying is kind of too
> advanced for me. I also checked the link you gave, but it really didn’t
> ring any bells for me. Can you explain it as “Making LSB compliant init
> script for dummies”, or better yet, if it is not very time consuming to
> do, can you produce final product using your codes?

It is easier than you think. Just copy and modify the skeleton script.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

/etc/init.d/skeleton

My new script:

Resides as: -rwxr-xr-x 1 root root 1283 Mar 17 13:20 noip2 (in /etc/init.d)


#! /bin/sh
# . /etc/rc.d/init.d/functions  # uncomment/modify for your killproc

### BEGIN INIT INFO
# Provides:     noip
# Required-Start:       network
# Should-Start:
# Required-Stop:        network
# Default-Start:        5
# Default-Stop: 0 1 2 6
# Short-Description: noip2 daemon for reporting ip changes
# Description: Reports ip address on change, so that dns record can be updated
### END INIT INFO
NO_IP_BIN = /usr/local/bin/noip2
test -x $NO_IP_BIN || {echo "No ip isn't installed";
        if "$1" = "stop" ]; then exit 0;
        else exit 5; fi}

. /etc/rc.status
rc_reset

case "$1" in
start)
echo -n "Starting noip2."
/sbin/startproc $NO_IP_BIN
rc_status -v
;;
stop)
echo -n "Shutting down noip2."
/sbin/killproc $NO_IP_BIN
rc_status -v
;;
try-restart|condrestart)
if test "$1" = "condrestart"; then
echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
fi
$0 status
if test $? = 0; then
$0 restart
else
rc_reset
fi
rc_status
;;
restart)
$0 stop
$0 start
rc_status
force-reload)
echo -n "Reload service noip"
/sbin/killproc -HUP $NO_IP_BIN
rc_status -v
;;
reload)
echo -n "Reload service noip"
/sbin/killproc -HUP $NO_IP_BIN
rc_status -v
;;
status)
echo -n "Checking for service noip"
/sbin/checkproc $NO_IP_BIN
rc_status -v
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
rc_exit


lrwxrwxrwx 1 root root 8 Mar 17 13:25 K13noip2 → …/noip2
lrwxrwxrwx 1 root root 8 Mar 17 13:25 S13noip2 → …/noip2

Is there anything else I need to check? Like some config files maybe?

You should not make those links, only the script file itself in /etc/init.d.
Then you can go to YaST > Software > Software services (runlevel) and you will find the name of your script in the list. Start from there and YaST will make all links accvording to your specifications in the LSB section.

I am getting syntax error with above script: it says, unexpected end of file. Could’nt figure out what is wrong with it :S

On 2011-03-17 12:36, yasar11732 wrote:
>
> My new script:
>
> Resides as: -rwxr-xr-x 1 root root 1283 Mar 17 13:20 noip2 (in
> /etc/init.d)
>

The lack of indentation makes it hard to read.

> Code:
> --------------------

> restart)
> $0 stop
> $0 start
> rc_status

Missing “;;”

>
> lrwxrwxrwx 1 root root 8 Mar 17 13:25 K13noip2 → …/noip2
> lrwxrwxrwx 1 root root 8 Mar 17 13:25 S13noip2 → …/noip2

Don’t create the links yourself, they will fail and probably deleted by
yast. Use yast, chkconfig or insserv.

> Is there anything else I need to check? Like some config files maybe?

Did you read the documentation I provided?


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

Your script has no lay-out at all and is thus very difficult to read for human beings. I guess thaa even you can not see at aglance in your own script if every “if” has it’s “fi”, etc.
One normaly uses the TAB character to give structure to a program:


if  somecondition ]]
then
        dosomething
        domore
else
       dosemethingelse
fi

The shell normaly gives such an error as you got when it is at the end of the script and a construct is still unfinished (like the last “fi” missing in an “if”).

I advise you to create a good programmimg style and when you apply that to the script, I guess you will see the error yourself. When not, post the improved script here.

I basically wrote your script and posted it in #6. I don’t know why you didn’t trust me and copied the skeleton with all the cases you don’t need.

Anyway if you change your mind, take this script and enable it with:

insserv -d yourscript

and watch for error messages. It works. It might not if you have other services (like mysql) needing network at runlevel 2. But that won’t be your fault nor mine. *

  • well, it will if you fix that bug, as I did, but it’s another problem …

As the time I saw your script, I had already completed my version, so I used mine instead. I fixed my end of file error. Now it workes like a charm now.

Thanks for all who have been so helpfull in this.

Best Regards,

Yaşar Arabacı

Ohh, just one more thing,

now this program runs as nobody? Why is that? Should it be fixed, or does it good this way?

On 2011-03-17 14:36, yasar11732 wrote:
>
> Ohh, just one more thing,
>
> now this program runs as nobody? Why is that? Should it be fixed, or
> does it good this way?

Have you properly inserted it as I told you?


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)