Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-20-2017, 01:33 PM   #1
Registered: Aug 2012
Posts: 789

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/ 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
# 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

SCRIPT="/usr/bin/php /var/www/socket/server.php 2222 0"
//SCRIPT="/usr/bin/php /var/www/socket/server.php 2223 1"
PROG="Socket Server"

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo 'Service already running' >&2
    return 1
  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
  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) ];
       echo "$PROG is running"
       echo "$PROG is not running"
    return $RETVAL

case "$1" in
    echo "Usage: $0 {start|stop|restart|status}"
Old 05-20-2017, 02:40 PM   #2
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 4,740

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
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
Registered: Aug 2002
Posts: 24,731

Rep: Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596Reputation: 5596
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
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.


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:15 AM.

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