How can I detach a gdb session from outside? - c++

I start a gdb session in the background with a command like this:
gdb --batch --command=/tmp/my_automated_breakpoints.gdb -p pid_of_proces> &> /tmp/gdb-results.log &
The & at the end lets it run in the background (and the shell is immediately closed afterwards as this command is issued by a single ssh command).
I can find out the pid of the gdb session with ps -aux | grep gdb.
However: How can I gracefully detach this gdb session from the running process just like I would if I had the terminal session in front of me with the (gdb) detach command?
When I kill the gdb session (and not the running process itself) with kill -9 gdb_pid, I get unwanted SIGABRTs afterwards in the running program.
A restart of the service is too time consuming for my purpose.
In case of a successful debugging session with this automated script I could use a detach command inside the batch script. This is however not my case: I want to detach/quit the running gdb session when there are some errors during the session, so I would like to gracefully detach gdb by hand from within another terminal session.

If you run the gdb command from terminal #1 in the background, you can always bring gdb back into foreground by running the command fg. Then, you can simply CTRL+C and detach as always to stop the debugging session gracefully.
Assuming that terminal #1 is now occupied by something else and you cannot use it, You can send a SIGHUP signal to the gdb process to detach it:
sudo kill -s SIGHUP $(pidof gdb)
(Replace the $(pidof gdb) with the actual PID if you have more than one gdb instance)

Related

How to make GCP start script to start multiple processes?

So, I'm using Google Cloud Platform and set below startup script
#! /bin/bash
cd /home/user
sudo ./process1
sudo ./process2
I worried about this script because process1 blocks shell and prevent to run sudo ./process2. And it really was. process1 was started successfully but process2 was not started.
I checked that script has no problem with starting process1 and process2. Execute ./process2 via SSH worked but after I close the SSH shell and process2 was stopped too.
How can I start both process in booting time(or even after)?
I tried testing your startup script in my environment,it seems the script works well.
1.You can please try checking process1 and process2 scripts.
2.If you want your process to run in the background even after the SSH session is closed, you can use “&” { your_command & }at the end of your command.
To run a command in the background, add the ampersand symbol (&) at the end of the command:
your_command &
then the script execution continues and isn't blocked. Or use linux internal means to auto run processes on boot.

Debug using gdb the initial startup of a linux daemon

I want to debug the very initial startup of a daemon started as a service under linux (centos 7).
My service is started as: "service mydaemon start"
I know about attaching gdb to a running process, but, unfortunately that technique is too slow, the initial execution of mydaemon is important.
mydaemon is written in C++ and full debug info is available.
unfortunately that technique is too slow
There are two general solutions to this problem.
The first one is described here: you make your target executable wait for GDB to attach (this requires building a special version of the daemon).
The second is to "wrap" your daemon in gdbserver (as root):
mv mydaemon mydaemon.exe
echo > mydaemon <<EOF
#!/bin/sh
exec gdbserver :1234 /path/to/mydaemon.exe "$#"
EOF
chmod +x mydaemon
Now execute service mydaemon start, and your process will be stopped by gdbserver and will wait for connection from GDB.
gdb /path/to/mydaemon.exe
(gdb) target remote :1234
# You should now be looking at the mydaemon process stopped in `_start`.
At that point you can set beakpoints, and use continue or next or step as appropriate.

How to execute command in background in unix shell?

Execute the command and return immediately, not blocking until the command finishes.
Concepts: Background execution, signals, signal handlers, processes, asynchronous execution
System calls: sigset()
How?
You can direct the outputs to a separate buffer, a file if you don't want to spam your current terminal.
yourapp >> ~/tempOutput.txt &
If you want output it to "nowhere" you can redirect to null
yourapp >> /dev/null &
To run the command :
sudo nohup {your command} &
To check the running command process id using nohup
ps -ef | grep nohup
and to kill the command if needed
kill {process id}
You can execute a command (or shell script) as a background job by appending an ampersand to the command as shown below.
$ ./my-script.sh &
You can execute a command (or shell script) in the background using &, But the problem with this, if you logout from the session, the command will get killed. To avoid that, you should use nohup as shown below.
example
$nohup ./my-shell-script.sh &
or
$nohup ps -aux &
After you execute a command in the background using nohup, the command will get executed even after you logout. But, you cannot connect to the same session again to see exactly what is happening on the screen. To do that, you should use screen command.
Apart from this, I recommend to use tmux, you can create a session and reattach a session any time.
$tmux new -s mysessionname
More info about tmux

kill subprocess if proc

I'm using C++11 and linux. I am attempting to start up multiple ssh commands using fork() and popen() and monitor when the ssh command stops running. When I kill the ssh command on the other computer, it doesn't appear to kill the fork() child process that started it. The child process continues the run until I exit the program. What do I need to do to kill the child process once the ssh command which was called with popen() quits running? Is there something better I could use for this than popen() to call the ssh command?
You need to call wait or waitpid in order for the O/S to remove the child process. A completed child process which has not had its status retrieved by its parent with wait becomes a "zombie" process.
If you're not interested in the child process status but only want to have them cleaned up, you can install a signal handler for SIGCHLD, which will fire whenever one of your child processes finishes, and call wait within that handler to "reap" the child.
child process should query the status of ssh process id periodically and based on response you get back, decide to kill it or not. you can use getppid() to get parent id.
After getting process id. you can check if it is active or not by using
below script
if ps -p $PID > /dev/null
then
# pid is active do something
fi

How do I keep a Perl script running on Unix after I log off?

I have a script that takes a lot of time to complete.
Instead of waiting for it to finish, I'd rather just log out and retrieve its output later on.
I've tried;
at -m -t 03030205 -f /path/to/./thescript.pl
nohup /path/to/./thescript.pl &
And I have also verified that the processes actually exist with ps and at -l depending on which scheduling syntax i used.
Both these processes die when I exit out of the shell. Is there a way to keep a script from terminating when I close the connection?
We have crons here and they are set up and are working properly, but I would like to use at or nohup for single-use scripts.
Is there something wrong with my syntax? Are there any other methods to producing the desired outcome?
EDIT:
I cannot use screen or disown - they aren't installed in my HP Unix setup and i am not in the position to install them either
Use screen. It creates a terminal which keeps going when you log out. When you log back in you can switch back to it.
If you want to keep a process running after you log out:
disown -h <pid>
is a useful bash built-in. Unlike nohup, you can run disown on an already-running process.
First, stop your job with control-Z, get the pid from ps (or use echo $!), use bg to send it to the background, then use disown with the -h flag.
Don't forget to background your job or it will be killed when you logout.
This is just a guess, but something I've seen with some versions of ssh and nohup: if you've logged in with ssh then you may need to need to redirect stdout, stderr and stdin to avoid having the session hang when you exit. (One of those may still be attached to the terminal.) I would try:
nohup /path/to/./thescript.pl > whatever.stdout 2> whatever.stderr < /dev/null &
(This is no longer the case with my current versions of ssh and nohup - the latter redirects them if it detects that any is attached to a terminal - but you may be using different versions.)
Syntax for nohup looks ok, but your account may not allow for processes to run after logout. Also, try redirecting the stdout/stderr to a log file or /dev/null.
Run your command in background.
/path/to/./thescript.pl &
To get lits of your background jobs
jobs
Now you can selectively disown any of the above jobs, with its jobid.
disown <jobid>
All the disowned process should be keep on running even after you logged out.