Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm running CentOS 5.5 and have created an init script. The start of the script contains:

# Source function library.
if [ -f /etc/init.d/functions ]; then
        . /etc/init.d/functions
fi

The start bit works fine, but I'm having problems with the status and stop part:

status)
echo -n "Status of script: "
status "/bin/sh /path/to/script.sh"
RETVAL=$?
echo
;;

In PS AUX the command appears as "/bin/sh /path/to/script.sh" and I can see the PID. But when I run:

./my_init_script status

It says the script is not running. Should I be using the /bin/sh bit, or just the full script path, or just the script filename?

The stop part of the init script utilises "killproc". Again what command should I be using to successfully kill all occurrances of the script?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

If it's like the corresponding function on Ubuntu, you should omit the "/bin/sh" part.

status "$DAEMON"

You will probably want to use something like:

killproc -p "$PIDFILE" "$DAEMON"

Near the top of your script define those variables:

DAEMON="/path/to/yourscriptname.sh"
PIDFILE="/var/run/yourscriptname/yourscriptname.pid"

And, of course, you'll need to make sure that /var/run/yourscriptname exists.

Use the existing scripts in /etc/init.d as models for your script.

share|improve this answer
    
My status line reads just that and I've specified $DAEMON - it's what the start part uses (and works), though the full start command is: "nohup $DAEMON >> /path/to/logfile &" In PS AUX I can see the command running. Any ideas? –  Reado Aug 2 '10 at 15:16
    
@reado: Try it with just the script name without the directory. I don't have a Centos/RH system to check it on at the moment. Are you starting your script with sh? What happens if you start it without it (use #!/bin/sh at the top of the script, chmod +x it and start it with /path/to/scriptname)? Does status work in that case? Have you compared your init script to existing ones? –  Dennis Williamson Aug 2 '10 at 16:11
    
I'm not actually using a PID file - it's my first time writing these things and thought I wouldn't need one. Should I have one? If so, how would I append that to the existing startup method? Is there a command I can run to grab the PID of a newly create process and write the output into a file of my choosing? –  Reado Aug 3 '10 at 15:26
    
@reado: Like I said I don't have a Centos system available right now to check this stuff on and I recommend that you follow existing scripts as models. It may be that if you set the PIDFILE variable that init will do the right thing for you. You may also need a block of comments at the beginning of the file in a particular format. Again, existing scripts will show you the way. –  Dennis Williamson Aug 3 '10 at 18:17
    
Sorted!! I created the PID file functionality in my script, saved it into /var/run and modified both stop and status to use -p "$PIDFILE" "$DAEMON". Now both work! Thanks a lot, much appreciated! :-) –  Reado Aug 4 '10 at 9:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.