creating an init script

Michael Velez mikev777 at hotmail.com
Wed Sep 28 09:40:16 UTC 2005


-----Original Message-----

Hi everybody!

Currently using a Red Hat 7.2 server, I have to launch an application every
startup as a normal, unprivilegied user...
I remember Mandrake and the /etc/init.d/skeleton file which was an example
for writing init script of your own. Unfortunately, I don't know how to
create my own init script under Red Hat.
More explanations:
1° log as user "toto"
2° launch a binary file "start_myapplication"
3° before rebooting, kill the application via "stop_myapplication", not the
kill command!!!
As you can see, I don't want to launch those as ROOT, for security reasons.
I think I can launch the process as "toto" user in the script but don't
exactly remember howto...

Any idea?

Many thanks,
Zelos
---------------------

Zelos,

Something like the below should work.

I need to say several things:

- Study this script and make sure you know exactly what it's doing before
putting it into operation (look up things in the man pages if necessary). In
addition, I did not test this script, I just quickly wrote it in this e-mail
(so there may be some typos although it looks ok).  You may also need to
change things in the logic to suit your needs.

- the chkconfig line (line 3) is useful for using the chkconfig tool.  Add
this script to the /etc/init.d directory (let's call it 'foo') and then run:

chkconfig --add foo

this command will create a S99foo file in rc2.d, rc3.d, rc4.d, and rc5.d
(change the numbers if you want to start the command in different run
levels) to start the script and create a K01foo file in rc0.d, rc1.d, and
rc6.d to kill the script.  Look at the chkconfig man page for further info.

- the start section creates a file called myapplication.  This file is
necessary for RedHat Enterprise Linux (which I use).  I'm not sure about
your version. In RHEL, you need this file because, before stopping
myapplication, the general rc script will check to see whether it exists.
If it doesn't, it won't even call 'foo stop' because it'll assume 'foo
start' never ran, and your application won't get properly stopped.
Likewise, the stop section deletes that file.  Your OS may put these subsys
files in a different directory if they exist at all.  You need to check this
out.

Hope this helps,
Michael


--- START OF SCRIPT ---
#!/bin/bash
#
# chkconfig 2345 99 01
# description: this scripts starts and stops myapplication
#

start()
{
	su - toto -c=start_myapplication
	touch /var/lock/subsys/myapplication
}

stop()
{
	su - toto -c=stop_myapplication
	rm -f /var/lock/subsys/myapplication
}

case "$1" in
   start)
	start
	;;
   stop)
	stop
	;;
   restart)
	stop
	start
	;;
   *)
	echo "Usage: $0 {start|stop|restart}"
esac

--- END OF SCRIPT ---





More information about the redhat-list mailing list