LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Start process on boot (https://www.linuxquestions.org/questions/linux-newbie-8/start-process-on-boot-4175604861/)

NotionCommotion 04-28-2017 08:46 AM

Start process on boot
 
Trying to start a process on boot for Centos6, and am really struggling. If I have an error, will it be displayed on boot or is it logged somewhere? What am I doing wrong? Thank you

Code:

sudo chmod +x /etc/init.d/test_soap
sudo chkconfig --add test_soap
sudo chkconfig test_soap on


/etc/init.d/test_soap
Code:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Test SOAP Simulator
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:    $local_fs $network $named $time $syslog
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start SOAP Simulator
# Description:      Initiate a given PHP file every 5 seconds to simulate a SOAP server.
### END INIT INFO

SCRIPT= /usr/local/bin/test_soap
RUNAS=root

PIDFILE=/var/run/test_soap.pid
LOGFILE=/var/log/test_soap.log

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

status() {
    if ps -p $$ > /dev/null
    then
      echo "$prog is running"
    else
      echo "$prog is not running"
    fi
    RETVAL=$?
    return $RETVAL
}

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

/usr/local/bin/test_soap
Code:

/usr/bin/php /var/www/datalogger/test_soap.php

r3sistance 04-28-2017 09:30 AM

Code:

SCRIPT= /usr/local/bin/test_soap
pretty sure there shouldn't be a space there. Anyways, no sysvinit doesn't actively check/test if processes are still running and it will be down to yourself to set-up any logging. I personally use either logger or tee -a depending on if you want to use syslog or not.

NotionCommotion 04-28-2017 10:01 AM

Thanks r3sistance, I will give it a try when I am able, however, as described in my latest post, have bigger problems :(


All times are GMT -5. The time now is 10:51 AM.