How to kill process and make it return specific error code - linux

How can I kill a Linux process and make any waitpid() calls waiting for it get a specific error code?
An example use case is when I want to pretend a long-running test has successfully finished without having to wait for it.

This was asked and answered by Brendan Dolan-Gavitt on Twitter. His solution for x86_64 is:
gdb -p <pid> -batch -ex 'set {short}$rip = 0x050f' -ex 'set $rax=231' -ex 'set $rdi=0' -ex 'cont'
(I'm posting it on Stack Overflow so I will find it a year from now when I need it.)

Related

Howto debug running bash script

I have a bash script running on Ubuntu.
Is it possible to see the line/command executed now without script restart.
The issue is that script sometimes never exits. This is really hard to reproduce (now I caught it), so I can't just stop the script and start the debugging.
Any help would be really appreciated
P.S. Script logic is hard to understand, so I can't to figure out why it's frozen by power of thoughts.
Try to find the process id (pid) of the shell, you may use ps -ef | grep <script_name>
Let's set this pid in the shell variable $PID.
Find all the child processes of this $PID by:
ps --ppid $PID
You might find one or more (if for example it's stuck in a pipelined series of commands). Repeat this command couple of times. If it doesn't change this means the script is stuck in certain command. In this case, you may attach trace command to the running child process:
sudo strace -p $PID
This will show you what is being executed, either indefinite loop (like reading from a pipe) or waiting on some event that never happens.
In case you find ps --ppid $PID changes, this indicates that your script is advancing but it's stuck somewhere, e.g. local loop in the script. From the changing commands, it can give you a hint where in the script it's looping.

How to terminate gdbserver?

I am trying to debug with gdbserver. after I terminat the gdb client on the host I see that the gdbserver is still listening :
Remote side has terminated connection. GDBserver will reopen the connection.
Listening on port 5004
I tried to exit gdbserver with everything I have found anywhere no luck: quit,exit,q, monitor exit,Esc,Cnt+c... nothing kills it. Moreover, when I opened another terminal and looked for the process running gdbserver (with the commands ps,top) I couldn't find it there...
my question is - How to terminate gdbserver ?
Give command
monitor exit
from your host gdb before terminating the client. If you have already terminated it, just attach with another one.
monitor exit step-by-step
https://stackoverflow.com/a/23647002/895245 mentions it, but this is the full setup you need.
Remote:
# pwd contains cross-compiled ./myexec
gdbserver --multi :1234
Local:
# pwd also contains the same cross-compiled ./myexec
gdb -ex 'target extended-remote 192.168.0.1:1234' \
-ex 'set remote exec-file ./myexec' \
--args ./myexec arg1
(gdb) r
[Inferior 1 (process 1234) exited normally]
(gdb) monitor exit
Tested in Ubuntu 14.04.
gdbserver runs on the target, not the host.
Terminating it is target dependent. For example, if your target is UNIX-ish, you could remote login and use ps and kill from a target shell.
For any type of target, rebooting should kill gdbserver.
(If this isn't enough to answer your question, include more information about the target in the question.)
on linux write:
ps -ef |grep gdbserver
Now find the pid of the gdbserver process and then
kill -9 <pid>
Here is a script which I'm using to start gdb server via ssh and kill it when necessary with ctrl+c
#!/usr/bin/env bash
trap stop_gdb_server INT
function stop_gdb_server {
ssh remote-srv-name "pkill gdbserver"
echo "GDB server killed"
}
ssh remote-srv-name "cd /path/to/project/dir/ && gdbserver localhost:6789 my-executable"
quit [expression]
q
To exit GDB, use the quit command (abbreviated q), or type an end-of-file character (usually C-d). If you do not supply expression, GDB will terminate normally; otherwise it will terminate using the result of expression as the error code.
gdbserver should exit when your target exits. The question is how your target is exiting: does it
do nothing: just fall through
return 0 in main
exit(0) in main
From the debug sessions I've been running, in the first case, gdbserver will not exit. It will just hang around forever and you have to kill it. In the latter two cases, gdbserver will exit.

How to kill background task from another session? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I've run a multithreading program in background:
./my_task &
Then I logged out, then logged again. Now jobs command does not show this program, but top does show the threads of this program. So it is still running. How to stop it? I guess I can kill each thread, but there are many of them and I don't know how it will affect my_task program.
I am using Debian Squeeze.
In common case, you can use
ps aux | grep my_task
-or, if you know, that process name starts with "my_task" exactly:
ps aux | grep [m]y_task
(this will exclude grep process itself from result table)
to get desired process id (let it be $pid) and then kill it with kill $pid
edit (thanks to comments below): jobs is part of bash itself, and so information about it is listed in man bash page:
Job control refers to the ability to selectively stop (suspend) the
execution of processes and continue
(resume) their execution at a later point. A user typically employs this facility via an interactive
interface supplied jointly by the operating system kernel's terminal driver and bash.
The shell associates a job with each pipeline. It keeps a table of currently executing jobs, which may
be listed with the jobs command. When bash starts a job asynchronously (in the background), it prints a
line that looks like:
[1] 25647
indicating that this job is job number 1 and that the process ID of the last process in the pipeline
associated with this job is 25647. All of the processes in a single pipeline are members of the same
job. Bash uses the job abstraction as the basis for job control.
but this will not help a case since it will list jobs only for current instance (which, of cause, will change when you're changing your session)
run your proces with log. I have used gnome-calculator for example:
gnome-calculator & echo $! > tmp/11/mylog
and add below to .bashrc or other autostart for kill it:
kill `cat tmp/11/mylog`
You can use pgrep to find the command:
$ pgrep my_task
4384
Then you can use that output to make sure it's the command you want:
$ ps -fp 4384 | cat
I pipe the output to cat because the ps command will chop off the output at the rowsize of the terminal unless it's piped to another command.
You could combine them too:
$ ps -fp $(pgrep my_task) | cat
You can also use pkill if you're brave:
$ pkill my_task
This will kill any processes that match the regular expression my_task that is owned by the user.

Write a bash script to restart a daemon [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I thought I could just use this related question: How Do I write a bash script to restart a process if it dies. #lhunath had a great answer and told me everything I might do about it was wrong, but I'm restarting a daemon process and if I'm hoping there's something I can do in a single script that works.
my process starts with a kick off script that shows the startup log, but then quits and leaves the process running off the shell:
>sudo ./start
R CMD Rserve --RS-conf /var/FastRWeb/code/rserve.conf --vanilla --no-save
...
Loading required package: FastRWeb
FastRWeb: TRUE
Loading data...
Rserv started in daemon mode.
>
The process is up and running,
ps -ale | grep Rserve
1 S 33 16534 1 0 80 0 - 60022 poll_s ? 00:00:00 Rserve
Is there a simple way to wrap or call the 'start' script from bash and restart when the process dies or is this a case where PID files are actually called for?
Dang - question got closed even after pointing to a very similar question that was not closed on stackoverflow. you guys suck
A very simple way to monitor the program is to use cron: check every minute (or so) if the program still is alive, ./start it otherwise.
As root, invoke crontab -e.
Append a line like this:
* * * * * if ! pidof Rserve 2>&1 >/dev/null; then /full/path/to/start; fi
This method will stay persistent, i.e., it will be executed after a reboot etc. If this is not what you want, move it to a shell script:
#! /bin/bash
# monitor.sh
while true; do
if ! pidof Rserve 2>&1 >/dev/null; then /full/path/to/start; fi
sleep 10
done
This script has to be started manually from the command line, and can be easily stopped with Ctrl-C.
The easiest solution, if you can run the process is NON-daemon mode, is to wrap it in a script.
#!/bin/bash
while (true)
do
xmessage "This is your process. Click OK to kill and respawn"
done
Edit
Many deamons leave a lock file, usually in /var/lock, that contains their PID. This keeps multiple copies of the deamon from running.
Under Linux, it is fairly simple to look throgh /proc and see if that process is still around.
Under other platforms you may need to play games with ps to check for the processes existence.

Linux Core Dump Without Killing Process

Is it possible to generate a core dump without killing the process? If so, what is the command/signal to do so?
Thanks,
Jim
See the 'gcore' command, part of GDB.
I had the best success with attaching gdb in batch mode to the running program, get a backtrace and then detach.
gdb --batch --quiet -ex "set pagination off" -ex "thread apply all bt"
-ex "detach" -ex "quit" pid pid_of_process
A method to generate a coredump directly from program without gdb is described here:
https://unix.stackexchange.com/questions/11185/dump-process-core-without-killing-the-process
It make sense only if you are developing. Principle is to fork program and to raise SIGABRT from child.

Resources