LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2017, 01:33 PM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 770

Rep: Reputation: Disabled
Passing argument to chkconfig and having multiple process instances


Note that this is NOT a php or ssl question.

The following script will start a sockets server. The first argument passed to server.php will specify the port to be listened to, and the second argument will specify whether standard tcp or ssl is used. I start the system upon boot using chkconfig --levels 235 socketserver on and can start, stop, etc it using something like /etc/init.d/socketserver start.

Now, I want to have the server listen in both modes at the same time. Furthermore, I don't wish to hardcode the port. I was thinking of just doing /etc/init.d/socketserver start 2222 0 or /etc/init.d/socketserver start 2223 1, and using $2 and $3 in the script to send to server.php. But this won't work as it will have the same /var/run/socket_server.pid. Furthermore, I am not sure how I could pass the arguments to chkconfig --levels 235 socketserver on.

How can this be best accomplished?

filename: /etc/init.d/socketserver
Code:
#!/bin/sh
### BEGIN INIT INFO
# Provides:          Socket Server
# 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 Socket Server
# Description:       Start Socket Server
### END INIT INFO

SCRIPT="/usr/bin/php /var/www/socket/server.php 2222 0"
//SCRIPT="/usr/bin/php /var/www/socket/server.php 2223 1"
RUNAS=Michael
PIDFILE=/var/run/socket_server.pid
LOGFILE=/var/log/socket_server.log
PROG="Socket Server"

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  #redirct I/O so non-root user doesn't need special write position to log file.
  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
  su -c "$CMD" $RUNAS  3>"$LOGFILE" >"$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 [ -f "$PIDFILE" ] && ps -p $(cat $PIDFILE) >/dev/null;
    #if [ -d /proc/$(<$PIDFILE) ];
    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
 
Old 05-20-2017, 02:40 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,555

Rep: Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088
In SysV init, there is no mechanism for passing additional arguments to the script during the automatic start/stop for each runlevel. When manually controlling a service with "service {name} start|stop|whatever" you can include additional arguments, but the automatic actions will never pass anything but a single "start" or "stop" argument.

The usual way to include settings that you do not wish to hard-code in the script is to source a file that contains those settings, such as the various network-related service scripts that all contain a line, ". /etc/sysconfig/network". You can code your script to start or stop multiple service instances automatically based on your config file, and still retain the ability to manually start or stop then individually based on additional arguments passed via "service socketserver {start|stop|restart|status} [arg, ...]".

Last edited by rknichols; 05-20-2017 at 03:37 PM. Reason: s/amnually/manually/
 
Old 05-20-2017, 02:47 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,616

Rep: Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169
Basically "chkconfig --levels xyz myservice on" command configures what run levels the desired service automatically starts and stops. With the old init system chkconfig would create the appropriate links in the /etc/rcx.d directories i.e. KXYmy_service and/or SXYmy_service that point to the actual init script in /etc/init.d.

You can not pass actual daemon arguments via chkconfig. Instead of hard coding options most services use a configuration file of some kind and normally init scripts do not use command line arguments. I see no reason why your start up script could not start multiple instances so just create a pid/log files for each if desired.

To late again.

Last edited by michaelk; 05-20-2017 at 02:49 PM.
 
Old 05-20-2017, 04:36 PM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
a bit hacky (but I find sysvinit is like that anyways), the only simple way I could see to do this is to create a symbolic link and use the value of $0 to determine which configuration to use... but this really is something that systemd handles way better tho.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing multiple argument to expect script sumeet inani Linux - Newbie 11 05-06-2014 08:12 AM
Passing Single Argument to Multiple Scripts prravin1 Programming 13 02-29-2012 06:49 AM
sendmail-2: Wrong number of instances of process sendmail:, expected instances equal maxymaxymaxymaxymaxy Linux - Newbie 1 06-15-2011 10:51 AM
crontab - Multiple Instances of same Process pier0w Red Hat 3 01-10-2007 05:45 AM
preventing multiple process instances the_dayi Programming 2 07-14-2003 05:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration