Detection script of process running ??

Dear All,

I'm trying to run two different programs (client & server) that exchange data through socket communications over network. I just need to run client and a script (cshell) in bl from the client program that runs server in the background when it is needed. But, I 'm getting a problem as there is an iteration in the client program, it runs and stops server everytime it is called by client.
Could someone please help me to write a script thats preferably runs server, in another terminal (or machine), once only, until the client stops???????

Thanking you in advance for your support
azzedineAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

manav_mathurCommented:
[quote]
I just need to run client and a script (cshell) in bl from the client program that runs server in the background when it is needed.
[/quote]

I dont get this line.

What I am assuming here is you have a problem where you really need to have concurrent servers(one server can service multiple clients) whereas what you have designed is an iterative server(a server which can process only one client)

If this is the problem, better study up concurrent server design techniques. The basic idea is

-one instance of server listens on a particular port.
-client connects, server accepts and forks creating a child process.
-child services client and exits. In the meanwhile, server can process additional requests.

other issues?? post 'em.
Manav
0
Peter-DartonCommented:
If your server process communicates entirely through a network socket, you want to run it in the background, probably all the time.
Most server programs will automatically disconnect themselves from the environment they were started in using fork(), and they'll also set themselves to catch SIGHUP as well (either ignoring it, or taking that as a sign they should re-read their configuration files).
However it sounds like your server process doesn't do that (if you wrote it yourself, consider making it do so).

An example script that should suitably disconnect the server from the environment in which it was started is as follows:
#!/bin/sh
run_server()
{
  if [ "$4" != "" ]
  then
    server arguments >/dev/null 2>&1 </dev/null &
  else
    run_server $*  >/dev/null 2>&1 </dev/null &
  fi
  return 0
}
run_server

where "server arguments" should be replaced by a full path to your server and any arguments you want to pass to it.


Your client can do something like
#!/bin/sh
SERVER_PORT=12345
server_is_not_running()
{
  if netstat -an | grep "\\*\\.${SERVER_PORT} .*LISTEN\$" >/dev/null
  then
    return 1
  else
    return 0
  fi
}
if server_is_not_running
then
  run-the-run-server-script
  sleep 3
  if server_is_not_running
  then
    echo "Error - can't start server" >&2
    exit 1
  fi
fi
do-the-client-stuff


The next trick would be to have the server program close down if it's not been used for a while, but that's probably best done in the server program itself, not the client end, as the client end only knows when it closes, and probably doesn't know if it's going to be called again soon.
If you really really can't modify the server program, then you could write a script that'd go around and kill the server if it wasn't in use, but there are all sorts of nasty race conditions to test for (i.e. the client starting up just after the kill script has checked that nobody was using it)

To run the server on another machine, look at using rsh or ssh.  You'll need to ensure you've set up rlogin or ssh to let you in on that other machine without asking for a password.  You'll probably want to use the -n argument.

Note: The above scripts are written in Bourne shell syntax, as nobody in their right mind writes scripts in C-shell - C-shell is ok as an interactive shell, but it's not good for scripts (e.g. executing both sides of an if-then-else construct, or the wrong bits of switch constructs, is unable to handle redirection properly - the list goes on and is well documented elsewhere.  Fortunately there's nothing to stop you running bourne shell scripts from a cshell session)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannCommented:
> .. client program that runs server in the background when it is needed.
sounds like your server program is obsolete 'cause it can be done in the client directly
Or explain what exactly is your problem please.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

azzedineAuthor Commented:
Dear Everyone,

First, I'd like to thank you for your replies. That is very kind of you.
I think we are confusing few things here. Let me just to clarify my question then.
In my case, there is actually two different environments that I need to connect them to exchange data through sockets over internet to each other until the simulation period is over. These environments are Fortran package and Matlab. So I interfaced client program to Fortran package and the Server program to Matlab side. However as users, we just need to deal with the master only (Fortran package) which becames Client and Matlab is a Server.

The problem is when Client calls a script that runs a Matlab: Matlab starts and stops at every time step, which makes the simulation slow. Here is an example:

------------client .c --------
{
.........
// I call the script "xxx" the script that runs sever, I mean matlab.
system("xxx");
...........
}

---------xxx----------
#!/bin/csh
matlab -nodisplay -nosplash -r server&

---------------------------------

I'd be glad if someone 'd help me to write a script which detects if Matlab process is running then run just server program otherwise start Matlab that runs server program also.

Thanking you in advance for your help
0
Peter-DartonCommented:
---------xxx----------
#!/bin/sh
nohup matlab -nodisplay -nosplash -r server >/dev/null 2>&1 </dev/null &
--------------------------------

That should allow matlap to continue on in the background (I'm guessing that it terminates the moment the terminal from which it was started closes).

To detect if it's running, well there are two options here.
If it always listens on a particular socket number, you can check that something is listening on that socket (as described in my earlier comment - the "server_is_not_running" function).
If not, you can always use "ps" (with arguments, e.g. "ps -ef") to list all processes and grep for the matlab server process.  If you don't know what this is, post the output of "ps -ef" both before and after running the server and we can figure out an appropriate search pattern to use.

I am assuming, however, that it isn't matlab itself that's deciding to do the start/stop business.
It could be that when you run this "matlab -server" business, matlab doesn't start everything up, but just starts a small program that then listens out for incoming connections and starts the full matlab program to deal with each one.
If that is the case, things will get an awful lot more complicated (try buying extra memory for the computer - it'd be a more efficient use of your time).
0
ahoffmannCommented:
> ..  starts and stops at every time step ..
has this something to do with your communication between client and server? I don't understand what you mean by "time step"

Also: your system() starts a script (xxx) which the starts another program in background, means that the script retunrs immediately. Could this be your problem? Dou you need to wait til the program started by the script finished?
0
azzedineAuthor Commented:
Thank you a lot for you your replies.
Peter, your idea works well but now it doesn't display the data exchanged between Matlab and Fortran package at all. In Matlab script, I print the sent and received data to the screen to just check if there are correct.
Previously, I did something like that:
#!bin/sh
if pgrep matlab>/dev/null 2>&1
then
      RUN server &
fi
 nohup matlab -nodisplay -nosplash -r server &

---------------------------//------------
But, it doesn't recognize the matlab command RUN. Could someone please help with that?

Thanking you again for your help
0
Peter-DartonCommented:
I'm not surprised that script didn't recognise the command "RUN".
"RUN" is not a unix command (for a start, nearly all unix commands are lower case, second there isn't a command "run" in any unix I've ever met).

It sounds like you're confusing shell scripts with Matlab commands.
I know nothing of Matlab, so I can't help you with Matlab commands.

I can tell you that the script above will execute the command "RUN server &" if matlab is already running (which will fail as there's no command "RUN" in your path), and then start matlab in server mode regardless of whether or not it was already running.
What you probably wanted was something like
  #!bin/sh
  if pgrep matlab>/dev/null 2>&1
  then
    echo "Matlab already running"
  else
    echo "Starting Matlab"
    nohup matlab -nodisplay -nosplash -r server &
    sleep 3
  fi

As for not showing the data exchanged between Matlab and Fortran, well I thought you'd stated earlier that this was done through socket connections over the network, not simply writing text to what it considers to be stdout.
In general, server programs do not read or write to stdout.  In general, they do all their I/O through socket connections - a client connects to it, asks it to do something, and it sends the results back to the client through the same connection.

I think I'd need a much fuller explanation of what these client and server processes are and what their I/O requirements are - how to they communicate, when do they communicate, when do they open or close their connections etc.
At present, I think we're talking about two different issues, which is why the solutions I'm suggesting don't match the issue you're trying to solve.
0
azzedineAuthor Commented:
Thanks a lots for your reply.

The RUN command is a Matlab command. Actually we can run a matlab script either with RUN namescript or with namescript only. the mather isn't about "echo" but in matlab script the is printf function to diplay the data exchanged. When I use your line above, I don't display anything in background. Is that possible to diplay content of printf function?

Thank you once again for your help
0
Peter-DartonCommented:
If the RUN command is a Matlab command, you'd need to execute that command from within Matlab, not from within a shell script.  (A shell script isn't the same as a Matlab script)

That matlab no longer spews text to the session from which it was started was expected.
Generally, if you run a server, the server reads and writes via network socket connections only.
It sounds like you don't use those.

AIUI, you're saying that Matlab has a printf command, that this outputs to stdout, and that you require this output.  In particular, this output is not sent by networked sockets, hence can't be dealt with as a standard client/server manner.
This complicates matters, but it might still be possible to work around it.

Three questions:

1) The output you want from Matlab - does you "client" program need to read this at any point?

2) Where exactly _do_ networked socket communications get involved here?
(your original question stated they were used)

3) If you start matlab manually using the command "matlab -nodisplay -nosplash -r server &" and then run your client program, does it all work as expected (aside from the output from matlab being mixed up with the output from the client program)
You could try
  matlab -nodisplay -nosplash -r server > matlab.out &
and run the client stuff, then once the client is done, take a look at the contents of the file matlab.out
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.