0

Is it possible to tag a python program run from the command line?

Context: Said command will be run with nohup in the background, and will be killed and restarted at midnight via cron. My intention is to pipe ps into egrep for said tag, grab the pid, and kill -9 before restarting.

minimal, complete, and verifiable example

Start a python web server:

$ nohup python -m http.server 8888 &

Add a tag to the command. Note that -tag is just my imagination at work.. this is what I want:

$ nohup python -m http.server 8888 & -tag "ced72ca0-cd19-11ea-87d0-0242ac130003"

grep for tag:

$ ps aux | egrep "ced72ca0-cd19-11ea-87d0-0242ac130003"

...grab the pid from this, and kill -9

kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • 1
    maybe [this](https://stackoverflow.com/questions/17385794/how-to-get-the-process-id-to-kill-a-nohup-process) can be helpful. in short, save the pid printed by `&` and then use it to kill the process – MarcoLucidi Jul 23 '20 at 19:34
  • 2
    Maybe take a look at `man pkill` – Philippe Jul 23 '20 at 20:30
  • maybe put all code in file with name `ced72ca0-cd19-11ea-87d0-0242ac130003` and run this file and then `ps aux` will show process `ced72ca0-cd19-11ea-87d0-0242ac130003` on list. And you could kill it using `pkill` with this name - `pkill ced72ca0-cd19-11ea-87d0-0242ac130003` – furas Jul 24 '20 at 09:13

2 Answers2

1

Because you're saying that you want to kill the processes through isolated cron jobs at nidnight, I guess that the $! based solutions in the linked questions (like (How to get the process ID to kill a nohup process?)) are no option for you.

In order to identity your HTTP server processes, your idea is to 'tag' them with a unique ID so the cron jobs will find them.
What you could do in your specific case is to make use of the fact that the listening TCP sockets are unique on your given machine, and retrieve the associated pid through netstat.

A bash script along the lines of:

#!/bin/bash
port=${1:-"8888"}
IP=${2:-"0.0.0.0"}

pid=`netstat -antp  2>/dev/null | grep -E "^(\S+\s+){3}$IP:$port\s+\S+\s+LISTEN" | sed -E 's/ˆ(\S+\s+){6}([0-9]+).*$/\2/'`

[[ -n "$pid" ]] && kill -TERM $pid

... that you parameterize with IP and port through your cronjob.

lab9
  • 596
  • 2
  • 8
1

You can put code in file with name ced72ca0-cd19-11ea-87d0-0242ac130003,

#!/bin/bash 

python -m http.server

set it executable

chmod +x ced72ca0-cd19-11ea-87d0-0242ac130003

run it

nohup ced72ca0-cd19-11ea-87d0-0242ac130003 &

and then you can kill it

pkill ced72ca0-cd19-11ea-87d0-0242ac130003

or even using only beginning of filename

pkill ced

EDIT:

Because new script doesn't get any arguments so you can run it with any argument(s) - ie. some tag/word

nohup ced72ca0-cd19-11ea-87d0-0242ac130003 hello_world &

and then you can kill it using -f

pkill -f hello_world

or even using part of word

pkill -f hello

pkill -f world

This way you can even use normal name for script and add tag

nohup my_script ced72ca0-cd19-11ea-87d0-0242ac130003 &

and kill with -f

pkill -f ced72ca0-cd19-11ea-87d0-0242ac130003

or using only part of word

pkill -f ced
furas
  • 134,197
  • 12
  • 106
  • 148