how to create startup script?

Discussion in 'Programming/Scripts' started by shamuk80, Apr 4, 2008.

  1. shamuk80

    shamuk80 New Member

    Hi All

    I have installed tomcat on RHEL4/5. It doesnt create start/stop scripts in /etc/rc.d/init.d/ --> /etc/init.d/.

    Now i am stoping and starting tomcat by
    #/opt/tomcat/bin/startup.sh
    #/opt/tomcat/bin/shutdown.sh

    but i want to configure this in a script in init.d/ directory.

    I tired my self by writing a script after seeing the others in /etc/rc.d/init.d/

    #!/bin/bash
    #
    # description: Jakarta Tomcat Java Servlets and JSP server
    # processname: tomcat

    start_tomcat=/opt/tomcat/bin/startup.sh
    stop_tomcat=/opt/tomcat/bin/shutdown.sh

    # Source function library.
    . /etc/rc.d/init.d/functions

    # Source networking configuration.
    . /etc/sysconfig/network

    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 0

    start() {
    echo -n "Starting tomcat: "
    su -c ${start_tomcat} - tomcat
    echo "done."
    }
    stop() {
    echo -n "Shutting down tomcat: "
    # Add stop flag file
    touch $CATALINA_HOME/temp/.manualStop
    su -c ${stop_tomcat} - tomcat
    echo "done."
    }

    # See how we were called
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    sleep 10
    start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    esac

    exit 0



    but i am getting error if i run this script.

    [[email protected] /]/etc/init.d/tomcat stop
    -bash: /etc/init.d/tomcat: /bin/sh^M: bad interpreter: No such file or directory.

    /etc/init.d/tomcat start/stop/restart.


    Can any body help me to see whats wrong here?
    Thanks.
     
  2. KenJackson

    KenJackson New Member

    You created this file in a Windows editor!

    I know because if the ^M character. Windows editors like notepad end each line with a "\r\n" sequence instead of the Unix-world's "\n". And "\r" is the same character as ^M.

    Logic would say that Linux should consider the ^M to be whitespace and ignore it, but it doesn't. So it sees /bin/sh^M (which doesn't exist) instead of /bin/sh .

    So just edit it in a Linux editor and delete those ^M's.
     
  3. shamuk80

    shamuk80 New Member

    Hi ya,

    I have typed it now in vi editor by removing the previous one but getting the same error. "-bash: /etc/init.d/tomcat: /bin/sh^M: bad interpreter: No such file or directory." Even i replaced /bin/bash to /bin/sh but it didnt work and the same error "bash: /etc/init.d/tomcat: /bin/bash^M: bad interpreter: No such file or directory."

    Any body can see this problem?
     
  4. KenJackson

    KenJackson New Member

    Try this command:

    dos2unix /etc/init.d/tomcat

    If the dos2unix command isn't already installed, install the dos2unix package.
     
  5. shamuk80

    shamuk80 New Member

    Hi

    I used the same command dos2unix /etc/rc.d/tomcat,
    but still getting the same error "-bash: /etc/init.d/tomcat: /bin/sh^M: bad interpreter: No such file or directory." Even i replaced /bin/bash to /bin/sh but it didnt work and the same error "bash: /etc/init.d/tomcat: /bin/bash^M: bad interpreter: No such file or directory."

    please help me to solve this issue.
     
  6. KenJackson

    KenJackson New Member

    Be careful that you aren't editing one file and executing another. I've done that before.

    For example, in your comment you said you ran the command on /etc/rc.d/tomcat. But that's not the script that is used to start the service, and it's not the one mentioned in the error message, /etc/init.d/tomcat.

    Even if that was just a typo in the message or there is some symbolic link trickery, this problem sounds like the kind of problem I've had before where I kept editing and changing and trying new things but always always got the exact same result. In those situations I usually discovered I wasn't editing the right file.

    One last thing you might look for is the file /etc/init.d/tomcat5. That's what my Mandriva system uses.
     

Share This Page