[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Detection script of process running ??

Posted on 2004-11-17
10
Medium Priority
?
309 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:azzedine
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12605341
[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
 
LVL 2

Accepted Solution

by:
Peter-Darton earned 200 total points
ID: 12605366
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12608209
> .. 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:azzedine
ID: 12612942
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
 
LVL 2

Expert Comment

by:Peter-Darton
ID: 12613036
---------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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12613751
> ..  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
 

Author Comment

by:azzedine
ID: 12664769
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
 
LVL 2

Expert Comment

by:Peter-Darton
ID: 12665022
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
 

Author Comment

by:azzedine
ID: 12665325
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
 
LVL 2

Expert Comment

by:Peter-Darton
ID: 12665471
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question