3

I am running several processes over a cluster. I start every process separately using screen command. It allows me to disconnect from the cluster and when connected view my processes. Starting all the screens one by one is a painful job. I am wondering if we could do it with a python script. The scrip opens the new shell creates the screen runs the process and disconnects. Writes info about all the started processes in a text file like process id starting commands etc.

Secondly, I would like to stop the processes, I would like to put the pid to file and just run a command which will kill all the mentioned processes.

for example

the smaple inut file looks like

   process_name      command
      123            python batch_training.py

I would like to start the screen with the name given in process_name and the commend will be executed in the corresponding frame.

Thanks

4 Answers 4

3

If you want to run your command without opening screen session, you should also use -dmS options with screen. So if you want to do that with python, Your code could look like this:

import subprocess

subprocess.call(["screen", "-dmS", "screen_name_1", "top"])
subprocess.call(["screen", "-dmS", "screen_name_2", "top"])
subprocess.call(["screen", "-r"])
Sign up to request clarification or add additional context in comments.

Comments

2

Can you give the screen object a command in python?

Like:

from os import system

command = 'screen ' + '/dev/ttyUSB0 38400'
result = system(command)

result('ATZ')???

Comments

0

Try to use "os.system()" with standart Linux commands. e.g.:

os.system("screen nano")

Comments

0

In general, in python, you can run system commands by running

from os import system

and then

system('whatever_shell_command')

So in your case you would type:

from os import system
system('screen')

Unfortunately (this is only sort of related) you can't run more then one command together, so

system('shell_command ', argument)

will not work.

so if you want to do that, you will need to concatenate the two strings:

full_command = 'shell_command ' + argument 
system(full_command)`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.