Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using linux 'screen' command from python

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

like image 830
Shan Avatar asked May 07 '26 07:05

Shan


1 Answers

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"])
like image 58
AChorostian Avatar answered May 08 '26 22:05

AChorostian