In Script stopchkf,
I am just using simple grep to extract PID from 'ps -ef' output to kill that process.
Though the kill works to kill the process but along with giving message :
O/P :
./stopchkf
./stopchkf: line 5: kill: SIGQUIT: arguments must be process or job IDs
./stopchkf: line 5: kill: 11372
12301: arguments must be process or job IDs
@dextergenious
revisit the notes on how to use markdown code tags at this site. It should make it easier for you and the others to better understand what you're after.
I've edited your post for now.
you're calling ./stopchkf with no arguments while stopchkf is expecting ONE/first argument in grep "[c]hkf $1".
Why is that? And do you expect the $1 contain in grep?
These are two newline-separated PIDs that have been assigned to the stop variable.
The quoted "$stop" refers to it with the embedded newline.
An unquoted $stop allows the shell to do word splitting (per $IFS that is "space tab newline", and the kill command actually wants two arguments(words).
Your OS supports pgrep then use
stop=$(pgrep -f "chkf $1")
Still the output is newline-separated. Use
[ -n "$stop" ] && kill SIGQUIT $stop
The [ -n argument ] command needs exactly one argument, the kill command needs each PID in a separate argument.
The result with no - is that Bash expects a pid, and rejects the signal name by itself.
The other issue is that, without a proper signame option, the default is SIGTERM.
It looks like it kills pid 11372, although it normally does not confirm success.
You might add debug to find out what pid 12301 was. It might just be coincidence, or a child process that also got ended. Usually it is the grep, but you seem to know how to avoid that matching.
Sorry -- @MadeInGermany nailed that issue while I was posting.
One more time:
It's not supposed to be kill SIGQUIT $stop
as it has to contain the - if you use this syntax, i.e. kill -SIGQUIT $stop
(did you read Paul_Pedant's last reply?)
But -SIGQUIT and -SIGINT will apparently not work on a background process anyway, see here:
Yes, because since SIGQUIT is ignored (due to incorrect syntax used) the default signal sent is SIGTERM
Jobs in background are protected from actions in the foreground window that launched them. You don't want to start a background job, then start something trivial in foreground, Ctrl-C that, and see your long-running job die as well.
It depends how your job got started, too. I would not like to vouch for what happens if you start a background process from inside a background shell, or use nohup or disown. crontab and at have other possible scenarios.
My usual method is to start with SIGTERM, which will generally take down a job. It can however be trapped by a process, which should then clean up anything it needs to do before it exits. The job can also completely ignore SIGTERM, so I usually follow it up with a SIGKILL after 10 seconds. SIGKILL used to be documented as "Terminate with extreme prejudice", a phrase borrowed from the FBI.
Sorry, I didn't see the missing - before the SIGQUIT
gh is very short, and grep gh or pgrep gh might match other process names. For example a process name light would match.
If your long process name (ps -ef) can be /bin/bash ./gh or gh
then it can be more precisely matched:
# pgrep and grep -E take extended RE:
# exact gh or prefixed by a /
for signal in TERM SYS KILL
do
stop=$( pgrep -f '(^|/)gh$ )
[ -z "$stop" ] && break
kill -$signal $stop
sleep 10
done
From the inputs from you all, which I really appreciate, the only error in my originally posted script was , as pointed by Matt-Kita, that I was not using '-' with the signal name/no. & not knowing that SIGINT & SIGQUIT don't work on background processes.
Thanks all for your contributions to add to my understanding.