Advertisement

Red Hat 9.0 - Starting a program when the system starts

Started by April 15, 2004 08:17 AM
11 comments, last by Evil Steve 20 years, 5 months ago
Hey all, I have a couple of program i wrote - an SMTP server and a proxy. Usually, i start my server, log in as root, and do nohup ./smtp&, but i thought there must be an easier way to do this. I don''t need to run the program as a daemon or anything, i just want it to start when the system starts so i don''t need to log in and bugger about. Anyone know how i do this? Cheers, Steve
init scripts.

Red HAt, iirc, uses sysv init scripts right? (somebody?)

so that means basically, you''d be ploping a script called something like mysmtpserver.rc into somehting like /etc/rc.d/init

the script would have to respond to start, stop and restart commands.

Then you symlink from /etc/rc.d/rc.3 (or whatever run levels you want your program to run at) to that file. There''s generally some naming convention, like S50.mysmtpserver to specify what order the script are run in.

I suggest you take a look at the apache one to see how to write it. apache, iirc, has a very simple one, since it jsut passes the command off to apachectl

I''m typing this off memory. Lots of errors, to be sure. since i''ve never used red hat.
Advertisement
The kernel calls inittab when it is is finished loading. Inittab has references to /etc/rc.d in it for the different runlevels. I would say that is the place to start for system level request like this, as opposed to saying something like /etc/profile.d. While they may be named different from distro to distro, the behaviour is the same.

Start peeking in /etc/rc.d/rc.{_HINT_}
Again, I''ve seen these vary from distro to distro
i.e:
/etc/rc.d/rc.M might be for module loading regardless of runlevel
/etc/rc.d/rc.local -- same as above or below
/etc/rc.d/rc.boot might be for boot time scripts, again regardless of runlevel, for user land OS configuration
/etc/rc.d/rc.httpd would be for starting your http daemon
etc, etc, etc

quote: Original post by Anonymous Poster
/etc/rc.d/rc.M might be for module loading regardless of runlevel
/etc/rc.d/rc.local -- same as above or below
/etc/rc.d/rc.boot might be for boot time scripts, again regardless of runlevel, for user land OS configuration
/etc/rc.d/rc.httpd would be for starting your http daemon
etc, etc, etc

I think you use slackware too much. Why anyone uses slackware, I just don''t know, anymore...
evil steve,

to do this you need to make a shell script and place it into the (rc.3?) directory. there are tons of articles on the web about it if you need help.

-brad
-brad
Yeah, i had a look around /etc/rc.d/ but i wasn''t sure if there was an easier way (like how you can add a line to autoexec.bat in windows)

So, can i just copy the apache init script and modify it then?

Heres what my apache script looks like (/etc/rc.d/rc3.d/K15httpd):
#!/bin/bash## Startup script for the Apache Web Server## chkconfig: - 85 15# description: Apache is a World Wide Web server.  It is used to serve \#	       HTML files and CGI.# processname: httpd# pidfile: /var/run/httpd.pid# config: /etc/httpd/conf/httpd.conf# Source function library.. /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/httpd ]; then        . /etc/sysconfig/httpdfi# This will prevent initlog from swallowing up a pass-phrase prompt if# mod_ssl needs a pass-phrase from the user.INITLOG_ARGS=""# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server# with the thread-based "worker" MPM; BE WARNED that some modules may not# work correctly with a thread-based MPM; notably PHP will refuse to start.# Path to the apachectl script, server binary, and short-form for messages.apachectl=/usr/sbin/apachectlhttpd=${HTTPD-/usr/sbin/httpd}prog=httpdRETVAL=0# check for 1.3 configurationcheck13 () {	CONFFILE=/etc/httpd/conf/httpd.conf	GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"	GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"	GONE="${GONE}AccessConfig|ResourceConfig)"	if grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then		echo		echo 1>&2 " Apache 1.3 configuration directives found"		echo 1>&2 " please read /usr/share/doc/httpd-2.0.40/migration.html"		failure "Apache 1.3 config directives test"		echo		exit 1	fi}# The semantics of these two functions differ from the way apachectl does# things -- attempting to start while running is a failure, and shutdown# when not running is also a failure.  So we just do it the way init scripts# are expected to behave here.start() {        echo -n $"Starting $prog: "        check13 || exit 1        daemon $httpd $OPTIONS        RETVAL=$?        echo        [ $RETVAL = 0 ] && touch /var/lock/subsys/httpd        return $RETVAL}stop() {	echo -n $"Stopping $prog: "	killproc $httpd	RETVAL=$?	echo	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd /var/run/httpd.pid}reload() {	echo -n $"Reloading $prog: "	check13 || exit 1	killproc $httpd -HUP	RETVAL=$?	echo}# See how we were called.case "$1" in  start)	start	;;  stop)	stop	;;  status)        status $httpd	RETVAL=$?	;;  restart)	stop	start	;;  condrestart)	if [ -f /var/run/httpd.pid ] ; then		stop		start	fi	;;  reload)        reload	;;  graceful|help|configtest|fullstatus)	$apachectl $@	RETVAL=$?	;;  *)	echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"	exit 1esacexit $RETVAL

If i modify that, do i need to do stuff for start and stop? My SMTP server just enters a loop and responds to SIGTERM and SIGKILL signals to exit

Thanks for all the replies,
Steve
Advertisement
quote: Original post by C-Junkie
I think you use slackware too much. Why anyone uses slackware, I just don''t know, anymore...


Just since Slackware 2.3 I''m not being judged am I?
But to keep this somewhat related, I think* we introduced SystemV init compatability around 7.0 to be like the other distro''s. So we can the option do the things the slacker way, or the RH, SuSE, Mandrake, {your flavor here}

(*I can''t recall absolutely, it''s been awhile)
Look for a local.start file. In Gentoo it''s /etc/conf.d/local.start, but Red Hat will probably have it in a different place. That should be executed by the local rc-script on startup, so you could start your program there.
Evil Steve,

"... Scriptfile... too, big...." You should do a google search for a proxy startup script or something like that. That way you don''t have all that extra junk in there to ween out. (but to stay on topic, yes, just modify it to use your stuff. Also make sure there is a "start" "stop" in the script. the redhat script thingy only runs scripts with those).

-brad
-brad
you have to create a script, place it into /etc/rc.d/init.d
then do /sbin/chkconfig --add /etc/rc.d/init.d/scriptname

it will be added into your rc.x folders (rc.5 is the normal startup)

another way is to put your smtp into /etc/rc.d/rc.local
just place your command in there

This topic is closed to new replies.

Advertisement