Why doesn't killall work from script? - embedded-linux

I have a script (runcx) that starts two programs (cxLog and cx). I wish to be able to kill the whole mess, so I created a script:
50:/root # cat stop
killall runcx
killall cx
killall cxLog
But it doesn't work:
50:/root # ./stop
: no process killed
: no process killed
: no process killed
whereas individual commands do:
50:/root # killall runcx
50:/root # killall cx
killall: Could not kill pid '256': No such process
50:/root #
(cx is using threads, and pid '256' apparently disappeared when its parent process was killed)
What's going on here? How can I get my programs killed without all the typing?
The environment is Linux kernel 2.4.26 and a rather old version of busybox.

I figured this out after typing all the above but before hitting the post button. I had created script "stop" on a PC using vim, and the default file format is dos. Thus, killall was trying to kill programs named "runcx^M", "cx^M", and "cxLog^M". I removed the ^Ms and the script worked much better!

Related

How can I kill a Festival speech command?

Let's say I run the following command:
cat /var/log/dmesg | festival --tts
This might return the message [1] 4726, indicating a process ID associated with this operation. When I run kill 4726 or killall festival or killall cat or killall aplay, the speech does not stop (or, at least, it continues on for quite some time before stopping). If I run the command above, how can I kill what it starts doing?
Kill sends a SIGTERM to the program. SIGTERM tells the program to stop, allowing it to shut down gracefully. The program is unallocating memory, closing connections, flushing to disk, removing temp files, etc. So SIGTERM may not be immediate or quick.
Kill -9, sends a SIGSTOP or SIGKILL, which is only seen by the kernel. The kernel will terminate the process. While this is faster, it does not allow for a graceful exit.
I am not familiar with festival, so if you are worried that these commands are forking off processes and you want to stop all the children, you can brute force the issue by spawning them all out of a bash shell. When you kill the parent bash shell, it will kill all of the processes owned by it.
bash -c "cat /var/log/dmesg | festival --tts" &
You will get bash the pid for the bash shell, which you can kill and clean up all sub-procs.

shell script process termination issue

/bin/sh -version
GNU sh, version 1.14.7(1)
exitfn () {
# Resore signal handling for SIGINT
echo "exiting with trap" >> /tmp/logfile
rm -f /var/run/lockfile.pid # Growl at user,
exit # then exit script.
}
trap 'exitfn; exit' SIGINT SIGQUIT SIGTERM SIGKILL SIGHUP
The above is my function in shell script.
I want to call it in some special conditions...like
when:
"kill -9" fires on pid of this script
"ctrl + z" press while it is running on -x mode
server reboots while script is executing ..
In short, with any kind of interrupt in script, should do some action
eg. rm -f /var/run/lockfile.pid
but my above function is not working properly; it works only for terminal close or "ctrl + c"
Kindly don't suggest to upgrade "bash / sh" version.
SIGKILL cannot be trapped by the trap command, or by any process. It is a guarenteed kill signal, that by it's definition cannot be trapped. Thus upgrading you sh/bash will not work anyway.
You can't trap kill -9 that's the whole point of it, to destroy processes violently that don't respond to other signals (there's a workaround for this, see below).
The server reboot should first deliver a signal to your script which should be caught with what you have.
As to the CTRL-Z, that also gives you a signal, SIGSTOP from memory, so you may want to add that. Though that wouldn't normally be a reason to shut down your process since it may be then put into the background and restarted (with bg).
As to what do do for those situations where your process dies without a catchable signal (like the -9 case), the program should check for that on startup.
By that, I mean lockfile.pid should store the actual PID of the process that created it (by using echo $$ >/var/run/myprog_lockfile.pid for example) and, if you try to start your program, it should check for the existence of that process.
If the process doesn't exist, or it exists but isn't the right one (based on name usually), your new process should delete the pidfile and carry on as if it was never there. If the old process both exists and is the right one, your new process should log a message and exit.

BASH script suspend/continue a process within script

In my bash script I am writing, I am trying to start a process (sleep) in the background and then suspend it. Finally, the process with be finished. For some reason through, when I send the kill command with the stop signal, it just keeps running as if it received nothing. I can do this from the command line, but the bash script is not working as intended.
sleep 15&
pid=$!
kill -s STOP $pid
jobs
kill -s CONT $pid
You can make it work by enabling 'monitor mode' in your script: set -m
Please see why-cant-i-use-job-control-in-a-bash-script for further information

How do I write a watchdog daemon in bash?

I want a way to write a daemon in a shell script, which runs another application in a loop, restarting it if it dies.
When run using ./myscript.sh from an SSH session, it shall launch a new instance of the daemon, except if the daemon is already running.
When the SSH session ends, the daemon shall persist.
There shall be a parameter (./myscript -stop) that kills any existing daemon.
(Notes on edit - The original question specified that nohup and similar tools may not be used. This artificial requirement was an "XY question", and the accepted answer in fact uses all the tools the OP claimed were not possible to use.)
Based on clarifications in comments, what you actually want is a daemon process that keeps a child running, relaunching it whenever it exits. You want a way to type "./myscript.sh" in an ssh session and have the daemon started.
#!/usr/bin/env bash
PIDFILE=~/.mydaemon.pid
if [ x"$1" = x-daemon ]; then
if test -f "$PIDFILE"; then exit; fi
echo $$ > "$PIDFILE"
trap "rm '$PIDFILE'" EXIT SIGTERM
while true; do
#launch your app here
/usr/bin/server-or-whatever &
wait # needed for trap to work
done
elif [ x"$1" = x-stop ]; then
kill `cat "$PIDFILE"`
else
nohup "$0" -daemon
fi
Run the script: it will launch the daemon process for you with nohup. The daemon process is a loop that watches for the child to exit, and relaunches it when it does.
To control the daemon, there's a -stop argument the script can take that will kill the daemon. Look at examples in your system's init scripts for more complete examples with better error checking.
The pid of the most recently "backgrounded" process is stored in $!
$ cat &
[1] 7057
$ echo $!
7057
I am unaware of a fork command in bash. Are you sure bash is the right tool for this job?

How to replicate CTRL+C in a shell script?

I'm running a script that performs a command that lasts too long, so I want to interrupt it. Any help? I've tried to search a lot, and I've been pointed to the kill command. Although, I can't get it to work. By the way, I'm using a Mac OS X. Thanks!
Assuming you have the process' PID, send it a SIGINT signal:
kill -SIGINT PID
If you don't have the PID you can try pkill or killall, but they're somewhat less safe; a PID is the only way to uniquely identify a process. Right after you spawn the other process the PID should be in $!, so you can save it then
process-that-takes-a-long-time &
pid=$!
# other stuff
kill -SIGINT $pid
timeout 10 your_command
To replicate Linux Ctrl+C on a terminal on Mac OS X, use ctrl+C!
Its the same keystroke combination
Worked for me (first Steps on Mac)
edit: I didnt read the script is running in background.

Resources