LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   kill with nohup (https://www.linuxquestions.org/questions/linux-software-2/kill-with-nohup-603149/)

ust 11-28-2007 09:52 PM

kill with nohup
 
I would like use kill with nohup function , I think it should use "kill -TERM" but if I want to also use kill level , for example "kill -8" , can advise how can I combine these function ? I tried "kill -TERM -8" is not work .

gilead 11-28-2007 10:20 PM

Have a look at the man page for kill. Basically the syntax is kill -s 15 pid or kill -s SIGTERM pid. For a list of signals run kill -l

matthewg42 11-29-2007 12:56 AM

The numbers and the names are alternatives. For example, these two commands do exactly the same thing:
Code:

kill -9 12345
kill -KILL 12345

nohup prevents a process receiving SIGHUP signal when the parent shell is closed (and sends SIGHUP to all child processes).

ust 11-29-2007 01:04 AM

Quote:

Originally Posted by matthewg42 (Post 2974146)
The numbers and the names are alternatives. For example, these two commands do exactly the same thing:
Code:

kill -9 12345
kill -KILL 12345

nohup prevents a process receiving SIGHUP signal when the parent shell is closed (and sends SIGHUP to all child processes).

thx reply ,

SIGHUP signal mean kill signal ?
if yes , is it "kill -s SIGTERM" ?

thx

matthewg42 11-29-2007 01:34 AM

SIGHUP is the "hang-up" signal which is sent to children of login shells when the shell terminates. Think of the old set-up where a user logs into an old physical terminal like a vt100... each user is connected to the computer via a serial port. If the terminal device is turned off, or disconnects, the user is considered to be "hanging up" the serial line. That's the origin of the name anyhow.

Most programs will terminate if they receive SIGHUP (it is the default behaviour if the program does not explicitly handle the signal). By running with nohup, the nohup program traps the signal and ignores it, thus preventing the program from terminating.

SIGTERM (terminate) is an instruction to terminate a process. Think of it like a polite request for a program to close now. This gives the program a chance to terminate cleanly (e.g. if it's half way through some operation which it might want to finish first).

SIGINT (interrupt) is sent to a process when the user presses control-c (and the process is in the foreground in a terminal). Most applications close then they receive this signal.

The default behaviour for all these signals happens to be to terminate the program, but the program may choose to ignore the signal, or even trigger some other action instead of termination.

SIGKILL is a special in that a program cannot trap the signal - the process cannot avoid termination if it is sent SIGKILL - the OS just kills it immediately.

You may have noticed when you shut down in some distros you see a message something like "Shutting down... Sending TERM to all processes", then a short delay while the OS waits for all the processes to finish cleanly. There is then usually another message saying, "Sending KILL to all processes". This is the OS who, having waited patiently for programs to close doesn't ask nicely again - it just kills any remaining programs.

bobsmith5002 05-20-2012 07:02 AM

Still No Luck
 
Hi. This the most comprehensive and clear discussion I have found on this issue. Thank you for sharing.

I have submitted a simple shell script with nohup and cannot kill the process. It simply spawns new processes. I am using:

$> kill -[switch] [process id]

which will terminate the process and create a new process. I have tried to send the following switches in order as suggested on another help site (http://stackoverflow.com/questions/8...-nohup-process)

SIGTERM (15)
SIGINT (2)
SIGKILL (9)

Any insights or suggestions would be greatly appreciated.

pan64 05-20-2012 09:13 AM

kill will never create new processes. probably you need to kill the parent process but I'm not really sure. nohup cannot protect any process against kill -9. processes can catch and tries to handle (ignore) some signals, like 1, 2 or 15, but not all of them.

chrism01 05-20-2012 11:56 PM

Post your script please

bobsmith5002 05-22-2012 07:41 AM

Script as Requested
 
It is a simple script...


$> nohup my-script.sh &


# Convert m4a to mp3
for i in *.m4a; do \
ffmpeg -i "$i" -acodec libmp3lame -ac 2 -ab 192k "${i%m4a}mp3"; \
done


Thank you in advance for any insights.

chrism01 05-22-2012 07:01 PM

OK; some notes

1. This
Code:

nohup my-script.sh &
implies you have the current dir '.' defined in your $PATH. This is generally considered a security risk, as someone could put a copy of an important prog eg rm into your current dir and it may take effect before the real one in eg /bin.
Normally you wouldn't have this; instead you'd use a path eg
Code:

nohup ./my-script.sh &
2. Your actual code looks odd; the '\'s are redundant. They are only used when you want to create a very long cmd line (eg 132 chars) and need to fold it eg so it can be seen in 80 chars.
Try
Code:

# Convert m4a to mp3
for i in *.m4a
do
    ffmpeg -i "$i" -acodec libmp3lame -ac 2 -ab 192k "${i%m4a}mp3"
done

Your inability to stop this implies to me that you are actually trying to kill the ffmpeg process(es), not the pid of the script itself.

Normally
Code:

kill pid
is sufficient and sends the SIGTERM signal by default; see matthewg42's explanation above.
As he said, use 'kill -l' (lowercase L) to see all signals
Do NOT use -9 = SIGKILL if you can avoid it, as it kills the process without allowing it to clean up, which can lead to corrupt files.

bobsmith5002 05-23-2012 07:24 AM

chrism01,

Thank you for taking the time to analyze and educate so clearly. Greatly appreciated.

~bobsmith5002

i92guboj 05-24-2012 04:37 AM

Are you sure you're picking the right pid? My experience with nohup is limited, and i am not sure how it works, but you should be able to capture the pod with $! so you can later kill it

Code:

nohup <...>
pid=$!
...
.
.
kill -9$pid

But this really depends on what exactly your script is doing.

chrism01 05-24-2012 08:39 PM

Actually, $! is set by backgrounding (using the '&' cmd/char), although I probably always use '&' if I'm going to use nohup ...

bobsmith5002 05-25-2012 07:14 AM

All,

It is what chrism01 suggested. I foolishly did not detect the proper process id in grep. This allowed the true parent process id to continue re-spawning new 'ffmpeg' children.

TLDR - I am in the learning curve with 'nohup' - painfully so.

Many thanks,
bobsmith5002


All times are GMT -5. The time now is 08:00 PM.