-1

I use SSH to connect to Linux, maybe run a Linux script multiple times, and use nohup to suspend these processes, and then close the SSH connection. After the next SSH connection to Linux, how can I distinguish between different scripts and get different PIDs?

This Linux script will always print content on the screen. I use Python's paramiko library, SSH to Linux, run the script and use nohup to suspend the process and redirect the output to the file. This process may be multiple times. How to distinguish the starting process, find its PID and kill it. It is best not to modify the Linux script because the script is not written by me.

I use the script name to find the process number, get a lot of PIDs, I can't distinguish them.

3 Answers3

1

You could parse the output of ps -eo pid,lstart,cmd which shows the process id, start time and path, e.g.:

  PID                  STARTED CMD
    1 Mon Jun 19 21:31:08 2017 /sbin/init
    2 Mon Jun 19 21:31:08 2017 [kthreadd]
    3 Mon Jun 19 21:31:08 2017 [ksoftirqd/0]

== Edit ==

Be aware that if the remote is macOS the ps command does not recognize the cmd keyword, use comm or command instead, e.g.: ps -eo pid,lstart,comm

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
0

Use ps command to check running process. For checking only shell scripts , You an do something like this:-

ps -eaf |grep .sh

This will give you all the information about running shell scripts only, easily you can distinguish b/w running scripts.

In place of ".sh" you can give file name also, than you will get information only about that running file.

Rohit Gupta
  • 443
  • 3
  • 17
0

maybe change the command you run to do something like:

nohup command.sh &
echo "$! `date`" >> runlog.txt
wait

i.e. run the command in the background, append its PID to a log (you might want to include more identifying information here or use a format that's more easily machine readable), then wait for it to finish

another variant would be to run tmux (GNU screen) on the server and run commands in an existing session:

tmux new-window command

which would also let you "reconnect" to running scripts later to check output / terminate

Sam Mason
  • 15,216
  • 1
  • 41
  • 60