views:

36

answers:

1

I have two python scripts that have to run simultaneously because they interact with each other. One script is a 'server' script running locally and the other is client script that connects to it via a socket. Normally I just open a couple terminal tabs and run the server script in one and the client in the other. After starting and stopping each script over and over, I wanted to make a bash alias to run both scripts with just one command and came up with this:

gnome-terminal --tab -e "python server.py" --tab -e "python client.py"

However, now the server script is raising an sqlite OperationalError saying that one of my data tables doesn't exist. But when I run the scripts manually everything works fine. I have no clue what is going on, but I thought that maybe running the scripts together wasn't giving the server script enough time to initialize and make its connection to the database. So I put a time.sleep(5) in the client script, but as soon as it starts I get the same error.

Anyone have an idea what could be happening? Or does anyone know of any alternatives for starting two python scripts with one command?

A: 

Try combining the two commands into one:

gnome-terminal --tab -x bash -c "python server.py & sleep 5; python client.py"

I think it is better to put the sleep command (if needed) outside client since there may be situations where the server is already started and the client does not have to sleep.


The -x flag means

-x, --execute
         Execute the remainder of the command line inside the terminal.

The command calls bash:

bash -c "python server.py & sleep 5; python client.py"

bash in turn, has a -c flag which means

-c string If  the  -c option is present, then commands are read from string.  If
         there are arguments after the string, they are assigned to  the  posi‐
         tional parameters, starting with $0.

You might want to experiment with

gnome-terminal --tab -e "python server.py & sleep 5; python client.py"

That might work too. When you run bash first, then your ~/.bashrc is read. Without calling bash, I think by default, /bin/sh is called instead.

If you get

"socket.error: [Errno 98] Address already in use",

it probably means that your server has already been started, and running the server a second time fails.

unutbu
You want to `sleep 5`, not `time 5` in bash. Also, you don't want to send this sleep to background. So: `python server.py python client.py`
eumiro
@eumiro: Oops, thanks.
unutbu
I don't understand why, but your solution works perfectly! What do the -x and -c options do? I don't see those in the man page.
pythonBOI
Also, what would be the difference between your solution and just running the command "python server.py python client.py". It seems like the only difference is that your solution runs the command in its own terminal window.
pythonBOI
One more thing, when I run your command I get "socket.error: [Errno 98] Address already in use", but everything seems to work anyway. Really weird.
pythonBOI