starting/stopping a background python process wtihout nohup + ps aux grep + kill

  • Last Update :
  • Techknowledgy :

1) nohup

nohup python - u myscript.py > . / mylog.log 2 > & 1 &

Another solution is to write two bash functions that do the job:

mynohup() {
   [
      ["$1" = ""]
   ] && echo "usage: mynohup python_script" &&
      return 0
   nohup python - u "$1" > "${1%.*}.log"
   2 > & 1 < /dev/null &
}

mykill() {
   ps - ef | grep "$1" | grep - v grep | awk '{print $2}' | xargs kill
   echo "process "
   $1 " killed"
}

Now you can do exactly what you told:

mynohup myscript.py # will automatically
continue running in
   # background even
if I log out

# two days later, even
if I logged out / logged in again the meantime
mykill myscript.py

Suggestion : 2

Check process id if it does not return with ps aux | grep <what to kill>,If wanna stop process manually run kill <pid> for & or kill -9 <pid> for nohup,Keep process id if it returns,There are two ways to run process in the background, nohup and & (ampersand).

// this does not collect any output, but still print output to stdout (on console).
// it just does not receive ctrl+c
python test.py &

   // this writes output to specified file, does not write anything to console.
   python test.py > log.txt &

   // this writes error to specified file, but still print normal output to stdout.
   python test.py 2 > log.txt &
nohup python test.py &
grep FINISH nohup.out

Suggestion : 3

 April 09, 2022     bash, linux, nohup, python   

I usually use:

nohup python - u myscript.py & > . / mylog.log & # or should I use nohup 2 > & 1 ? I never remember

to start a background Python process that I'd like to continue running even if I log out, and:

ps aux |grep python
# check for the relevant PID
kill <relevantPID>

Is there a clean method to easily start / stop a Python script? like:

startpy myscript.py # will automatically
continue running in
   # background even
if I log out

# two days later, even
if I logged out / logged in again the meantime
stoppy myscript.py

Another solution is to write two bash functions that do the job:

mynohup() {
   [
      ["$1" = ""]
   ] && echo "usage: mynohup python_script" &&
      return 0
   nohup python - u "$1" > "${1%.*}.log"
   2 > & 1 < /dev/null &
}

mykill() {
   ps - ef | grep "$1" | grep - v grep | awk '{print $2}' | xargs kill
   echo "process "
   $1 " killed"
}

Now you can do exactly what you told:

mynohup myscript.py # will automatically
continue running in
   # background even
if I log out

# two days later, even
if I logged out / logged in again the meantime
mykill myscript.py

Suggestion : 4

The pid in your example, the one that keeps changing, is the process ID of your grep trying to find the PID. That means that your ps and grep is not finding the actual process that you are looking for.,The basic problem is with the command 'ps ax | grep something'. This always lists 'grep ... something' as one of the process, which is in fact the 'grep' started by you. use 'pgrep ' instead to get the pid,If myName is not the process name or for example, is an argument to another (long) command, pkill (or pgrep) might not work as expected. So you need to use the -f option.,Try running your script without the trailing "&", to see what it is doing. Odds are it's failing quietly on you and not actually starting at all.

Use killall:

killall <the_process_name>

Eg: pkill firefox
pkill - 9 firefox