1

My problem: I run a lot of python scripts in background on a remote machine. Some of them need many days. When I type top -U username to check which script ended and which didn't I get a long list looking like that:

54658 user   20   0 7963464   2.9g  13684 R 100.3  0.8   5921:33 python3                                                                          
54982 user   20   0 6243164   1.2g  13696 R 100.3  0.3   5920:46 python3 
98740 user   20   0  737256 508340   5040 S   0.0  0.1  5226:56 python

The problem is that, as you can see, each process id has, as associated command, python (or python3). This makes impossible to understand the correspondence between process id and script. I would really like to have displayed the name of the process there (as it is when running an executable written in C). Is there some way to obtain it?

Please note that:

1) As suggested here I tried making the script an executable itself via chmod +x scriptname and it didn't get any better. I still see python3

2) If I do ps -U username -o pid,cmd as suggested here I get a looooong list of processes and this is not really handy.

It seems weird to me but I have been searching for it and it looks like there is no other question like this, so it shouldn't be a duplicate. If it is, please link me to the answer.

GRquanti
  • 527
  • 8
  • 23
  • How are you executing your python scripts? Do your process objects have any access to the cmdline args? – Maximilian Burszley Nov 10 '18 at 01:19
  • 1
    `ps -U uname -o pid,cmd|egrep 'progName1|progName2|...` ? Good luck. – shellter Nov 10 '18 at 01:28
  • @TheIncorrigible1I execute script via `python3 name.py &`. They usually don't have access to cmdline argument. – GRquanti Nov 10 '18 at 01:56
  • @shellter thank you very much. It's not exactly what I am searching for but it still a great improvement. Especially if `'prog1|prog2...'` is substituted with `*.py` – GRquanti Nov 10 '18 at 01:57
  • 2
    if you add the `-c` option to your `top`, as `top -c -U username`, you should be able to see the command line that you ran to start the script like `python3 name.py`. – John Anderson Nov 10 '18 at 02:08
  • @John Thank you very much, this is exactly what I was searching for! If you write it as an answer I will accept it! – GRquanti Nov 10 '18 at 02:39

2 Answers2

3

The top command has numerous options to adjust what it displays. In your case, you probably want to use the -c option to see the actual command line that was used to start your script. So, try top -c -U username.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
0

A simple grep would help you

ps -axo pid,cmd | grep [p]ython

You can even filter based on how you run your scripts

ps -axo pid,cmd | egrep '^[0-9]+ python .*'

Note that this only filters the word python from the output of ps.

ssemilla
  • 3,900
  • 12
  • 28
  • Do you have a formatting requirement or do you just need to check if the scripts are still running? – ssemilla Nov 10 '18 at 02:28
  • Just check is script is running. However @John Andrerson gave the correct answer in a comment to the questione. Thank you in any case, your answer is useful! – GRquanti Nov 10 '18 at 02:40