Unable to kill nohup process - bash

If I start the script by ./test.sh &, I am able to kill using kill -SIGINT PID.
But if I start my shell script using nohup ./test.sh & I am unable to kill the process using kill -SIGINT PID.
Kindly need your advice to kill the script using kill -SIGINT PID

The SIGINT signal means interrupt from keyboard; that's why it terminates a script run in foreground, but not in background neither using nohup.
To properly terminate your process use kill -TERM PID, which works in the 3 cases.

Related

Cannot kill stopped job - no job control in subprocess?

I'm working with ash/dash and try to kill a subprocess - which doesn't seem to respond:
sh & opens a subprocess and jobs delivers [1]+ Stopped (tty Input) sh.
But trying to kill this Job with kill %1 or kill 26672 doesn't work. jobs delivers [1]+ Stopped (tty Input) sh again.
After putting the job to foreground with fg opens the shell for input. Neither ctrl+c nor ctrl+z are working but I can kill the process with exit or kill -SIGKILL $$ respectively stop/suspend the process with kill -STOP $$ (there is no suspend-command in ash).
On the other hand - doing this with i.e. sleep 100 works fine till I fg and stop the process with ctr+z. Then I'm not able to kill this stopped job.
So what am I missing and what could be the solution to kill a stopped job? Do I have to deal with set -m and how?
Thanks in advance.
You can try
kill -9 $(jobs -p)
Or, just do exit command to logout, it will automatically killed the stopped jobs
You can rung the Stopped process by kill -SIGCONT %numberand also if you need to kill that process you can kill it by kill -SIGTERM %number.Try this I think this will help you.

send signals to process using its pid

Hell all,
I am trying to write a shell script to run a program and send a sequence of signal with delay between them. I wrote the following code.
#!/bin/sh
KNOCK="KNOCK"
export KNOCK
./knock &
knockPID=$!
kill -SIGUSR2 $knockPID
kill -SIGUSR2 $knockPID
kill -SIGUSR1 $knockPID
sleep 2s;
kill -SIGUSR1 $knockPID
kill -SIGUSR2 $knockPID
I keep getting the following error for each of the kill commands
kill: Illegal option -S
your help is appreciated.
Generally, named signal arguments for the kill command are "recognized in a case-independent fashion, without the SIG prefix". So, you want:
kill -USR1 $knockPID
and so on.
kill -s SIGUSR2 $knockPID
should probably work on all modern OSes.
I ran into this problem too, I fixed it by using a different shell:
#!/bin/bash

How to kill child processes when killing from pid file

kill -9 `cat ~/code/posbox/posbox.pid`
I'm running the command above to kill a process. The pid is in the file, it's correctly killing the process but child processes are still running
when I do
kill <PID number>
it kills the process along with all the child processes. How do I kill all parent and child processes from a pid given in a pid file?
Since kill <PID number> works, just use that:
# By default, send SIGTERM, not SIGKILL (9)
kill $(cat ~/code/posbox/posbox.pid)
It sounds like your process catches the TERM signal and passes it on to its children before exiting, allowing them all to exit cleanly. Using kill -9 ends the parent immediately, without giving it a chance to tell its children to exit at all.

In Bash, how can I run multiple infinitely-running commands and cancel them all with ^C?

I would like to write a script that runs a few different infinitely running commands, e.g.
run_development_webserver.sh
watch_sass_files_and_compile_them.sh
watch_coffeescript_files_and_compile_them.sh
I'd like to run each of them in parallel, and kill them all by hitting ^C. Is this possible, and if so how can I do this?
I'll let Admiral Ackbar answer this one.
#!/bin/bash -e
run_development_webserver.sh &
PIDS[0]=$!
watch_sass_files_and_compile_them.sh &
PIDS[1]=$!
watch_coffeescript_files_and_compile_them.sh &
PIDS[2]=$!
trap "kill ${PIDS[*]}" SIGINT
wait
This starts each of your commands in the background (&), puts their process ids ($!) into an array (PIDS[x]=$!), tells bash to kill them all (${PIDS[*]) when your script gets a SIGINT signal (Ctrl+C), and then waits for all the processes to exit.
And I'll proactively mention that "kill ${PIDS[*]}" expands PIDS when you create the trap; if you change the double quotes (") to single quotes ('), it will be expanded when the trap is executed, which means you can add more processes to PIDS after you set the trap and it will kill them too.
If you have a stubborn process that doesn't want to quit after a Ctrl+C (SIGINT), you may need to send it a stronger kill signal - SIGTERM or even SIGKILL (use this as a last resort, it unconditionally kills the process without giving it a chance to clean up). First, try changing the trap line to this:
trap "kill -TERM ${PIDS[*]}" SIGINT
If it doesn't respond to the SIGTERM, save that process's pid separately, say in STUBBORN_PID, and use this:
trap "kill ${PIDS[*]}; kill -KILL $STUBBORN_PID" SIGINT
Remember, this one won't let the stubborn process clean up, but if it needs to die and isn't, you may need to use it anyway.

Run command when bash script is stopped

How can i, in a bash script, execute a command when the user stops the script (with ctrl - c)?
Currently, i have this:
afplay file.mp3
while true:
do osascript -e "set volume 10"
end
But i would like it to execute killall afplay when the user is finished with it, regardless if it is command-c or another keypress.
trap 'killall afplay' EXIT
Use trap.
trap "kill $pid" INT TERM EXIT
Also avoid killall or pkill, since it could kill unrelated processes (for instance, from another instance of your script, or even a different script). Instead, put the player's PID in a variable and kill only that PID.
You need to put a trap statement in your bash script:
trap 'killall afplay' EXIT
Note however that this won't work if the bash process is sent a KILL signal (9) as it's not possible for processes to intercept that signal.

Resources