0

I have set my script to run using the /etc/init.d method. All works good. The problem is that my script does packet capturing and when it restarts the machine after say the crond is loaded then it loads this script of mine and the packet capture process I guess starting then and it halts from going further. Is there any method where I can force it only to start when Linux is fully loaded ?

Below is how my script looks like.

#!/bin/bash
# chkconfig: 2345 95 05
# myapp daemon
# description: myapp daemon
# processname: myapp

DAEMON_PATH="/usr/local/bin/"
DAEMON=pc1.c
#DAEMONOPTS="-my opts"
NAME=pc1.c
DESC="my packet capture"

PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
    printf "%-50s" "Starting $NAME..."
    insmod /usr/local/pfring/kernel/pf_ring.ko
    /usr/local/bin/pc1.c &
    $! > PIDFILE    
;;
status)
        if [ -f $PIDFILE]; 
        then if [ -d /proc/cat $PIDFILE ]; 
             then echo "Process is running"; 
             fi; 
        else echo "Error! PID file is missing!"; 

        fi

;;
stop)
        printf "%-50s" "Stopping $NAME"
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            printf "%s\n" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\n" "pidfile not found"
        fi
;;
restart)
    $0 stop
    $0 start 
;;
*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac
  • Can you describe what you mean by "fully loaded"? It may simply be that you are starting your init script before the network, or something similar. – mattdm Aug 20 '13 at 17:40
  • @What I mean by fully loaded is that where the os is loaded after all the initialisation process. – biz14 Aug 20 '13 at 17:44
  • In the init system in CentOS 5 and 6, everything is loaded one after the other. You simply need to make sure your service is numbered after all of the initialization you want to happen first. – mattdm Aug 20 '13 at 17:48
  • I have updated my question with the /etc/init.d script. So what should I tweak here. – biz14 Aug 20 '13 at 17:51
  • 2
    See the line # chkconfig: 2345 95 05? The "95" is the order in which it will start. Look at /etc/rc3.d/S* (or rc5.d if you're booting to a GUI) -- everything that's numbered after 95 will load before your service starts. (This is assuming you've enabled this script with the chkconfig command.) – mattdm Aug 20 '13 at 19:21
2

If you would run the command that runs within your init.d script with '&' after the end of the command, it will become a background process, and the next services could start.

If you want the script to run as the final step (it would still block if it does not background) of your boot process, you could run it through /etc/rc.local (or your distro equivalent if you don't have one...)

  • @I dont quite get this method "with '&' after the end of the command, it will become a background process, and the next services could start." When you say background means it will not start immedialtely after my os is loaded ? – biz14 Aug 20 '13 at 17:45
  • @biz14 A background process, in a nutshell (from a user point of view), is a process that starts executing immediately, and returns you your controlling terminal - so you are not 'communicating' with it anymore. So next the commands on the script file continue to operate; If there are none left, the script would end, and then init will run the next script... – Shimi Aug 20 '13 at 17:51
  • @I have updated my question with my /etc/init.d script so what changes to do. I am lost between the init.d and background? – biz14 Aug 20 '13 at 17:52
  • @biz14 /usr/local/bin/myap1.c would become: /usr/local/bin/myap1.c & – Shimi Aug 20 '13 at 17:54
  • Ok I will do the necessary but I want to learn what is difference of having the & I know it will run into a background programme. But what is the key difference with what I am doing now? – biz14 Aug 20 '13 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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