Bash script hangs if I interrupt it - linux

For example there are
ps -ax
PID TTY Stat Time Command
1 ? Ss 0:00 /sbin/init
.
.
.
1564 ? Sl 0:00 usr/bin/xyz
1569 ? Sl 0:00 gnome-terminal
.
.
1730 ? sl 0:00 gcaltool
1759 ? sl 0:00 firefox
I have to interrupt the process 1564
1564 ? Sl 0:00 usr/bin/xyz
1569 ? Sl 0:00 gnome-terminal
The terminal gets hanged if I interrupt the process PID 1564. I think it hangs because I am interrupting the terminal process. Is there any other way as I have to strace the process
process PID 1730,
process PID 1759,
which were generated at the last?
Any way to bypass the strace and interrupt of the terminal process?

Related

How can I get STAT column in ps command?

I installed Cygwin for 64 bit versions of Windows.and I run "Cygwin64 Terminal" in order to confirm whole process' state. As far as I know, ps command must show STAT column but It is impossible to find STAT column Whenever I execute "ps -l" or "ps aux" ,"ps -ef", "ps axj"..
I really want to view STAT column in ps command because a source code which I practice recently request me to check zombie process by means of ps command.
screenshot of ps command without STAT column
Use procps
$ /usr/bin/procps.exe ax
PID TTY STAT TIME COMMAND
1580 pty1 Ss 0:00 -bash
1624 pty0 R 0:00 /usr/bin/procps ax
1522 ? Ss 0:05 /usr/bin/mintty -i /Cygwin-Terminal.ico -
1599 pty1 T 0:00 less .bashrc
1523 pty0 Ss 0:00 -bash
1579 ? Ss 0:00 /usr/bin/mintty -i /Cygwin-Terminal.ico -
you can find it in procps-ng package
$ cygcheck -f /usr/bin/procps
procps-ng-3.3.16-1

Killing subprocess from inside a Docker container kills the entire container

On my Windows machine, I started a Docker container from docker compose. My entrypoint is a Go filewatcher that runs a task of a taskmanager on every filechange. The executed task builds and runs the Go program.
But before I can build and run the program again after filechanges I have to kill the previous running version. But every time I kill the app process, the container is also gone.
The goal is to kill only the svc1 process with PID 74 in this example. I tried pkill -9 svc1 and kill $(pgrep svc1). But every time the parent processes are killed too.
The commandline output from inside the container:
root#bf073c39e6a2:/app/cmd/svc1# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 2.5 0.0 104812 2940 ? Ssl 13:38 0:00 /go/bin/watcher
root 13 0.0 0.0 294316 7576 ? Sl 13:38 0:00 /go/bin/task de
root 74 0.0 0.0 219284 4908 ? Sl 13:38 0:00 /svc1
root 82 0.2 0.0 18184 3160 pts/0 Ss 13:38 0:00 /bin/bash
root 87 0.0 0.0 36632 2824 pts/0 R+ 13:38 0:00 ps -aux
root#bf073c39e6a2:/app/cmd/svc1# ps -afx
PID TTY STAT TIME COMMAND
82 pts/0 Ss 0:00 /bin/bash
88 pts/0 R+ 0:00 \_ ps -afx
1 ? Ssl 0:01 /go/bin/watcher -cmd /go/bin/task dev -startcmd
13 ? Sl 0:00 /go/bin/task dev
74 ? Sl 0:00 \_ /svc1
root#bf073c39e6a2:/app/cmd/svc1# pkill -9 svc1
root#bf073c39e6a2:/app/cmd/svc1
Switching to the containerlog:
task: Failed to run task "dev": exit status 255
2019/08/16 14:20:21 exit status 1
"dev" is the name of the task in the taskmanger.
The Dockerfile:
FROM golang:stretch
RUN go get -u -v github.com/radovskyb/watcher/... \
&& go get -u -v github.com/go-task/task/cmd/task
WORKDIR /app
COPY ./Taskfile.yml ./Taskfile.yml
ENTRYPOINT ["/go/bin/watcher", "-cmd", "/go/bin/task dev", "-startcmd"]
I expect only the process with the target PID is killed and not the parent process that spawned it it.
You can use process manager like "supervisord" and configure it to re-execute your script or the command even if you killed it's process which will keep your container up and running.

How does 'kill -STOP and kill -CONT' work?

I'm facing an issue.
We have a clean script using to clean old files, and sometimes we need stop it for and will start it again later. Like the below processes. We use kill -STOP $pid and kill -CONT $pid in check.sh to control the clean.sh, $pid is all the pids of clean.sh (at there, they are 23939, 25804):
root 4321 0.0 0.0 74876 1184 ? Ss 2015 0:25 crond
root 23547 0.0 0.0 102084 1604 ? S 2015 0:00 \_ crond
root 23571 0.0 0.0 8728 972 ? Ss 2015 0:00 \_ /bin/bash -c bash /home/test/sbin/check.sh >>/home/test/log/check.log 2>&1
root 23577 0.0 0.0 8732 1092 ? S 2015 0:00 \_ bash /home/test/sbin/check.sh
root 23939 0.0 0.0 8860 1192 ? S 2015 0:45 \_ bash /home/test/bin/clean.sh 30
root 25804 0.0 0.0 8860 620 ? S 2015 0:00 \_ bash /home/test/bin/clean.sh 30
root 25805 0.0 0.0 14432 284 ? T 2015 0:00 \_ ls -d ./455bb4cba6142427156d2b959b8b0986/120x60/ ./455bb4cba6142427156d2b959b8b0986/80x
root 25808 0.0 0.0 3816 432 ? S 2015 0:00 \_ wc -l
Once the check.sh stopped clean.sh, hours later, check.sh started clean.sh, but there is a strange thing, after a stop and continue, there is a child process 'ls -d ....', it's still stopping.
Could you tell me if it's caused by wrong use of the signal? And how can I modify it?
ok, same like my description is not clear, my bad English...
Not sure what's the reason, but there is a way to sovle it:
kill -CONT $pid
pkill -CONT -P $pid
This will continue the child process.

Linux Runlevel 1: start programm

I'm trying to automatically start a process when I enter runlevel 1 by init 1. It's a watchdog which has to send a life signal all the time therefore in runlevel 1 too! But when runlevel 1 is entered each process is killed and the system switches to runlevel S. I tried to tell linux to start my process by update-rc.d -f watchdog 99 1 S .. The resulting entries in /etc/rc1.d/ and /etc/rcS.d/ are:
/etc/rc1.d/:
.
.
.
S30killprocs -> ../init.d/killprocs
S90single -> ../init.d/single
S99watchdog -> ../init.d/watchdog
.
.
.
/etc/rcS.d/:
.
.
.
S01glibc.sh -> ../init.d/glibc.sh
S02hostname.sh -> ../init.d/hostname.sh
S99watchdog -> ../init.d/watchdog
.
.
.
A ps ax after runlevel 1 was entered sadfully doesn't return my watchdog process. I have to start it manually.
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 init [S]
2 ? S 0:00 [kthreadd]
3 ? S 0:00 [ksoftirqd/0]
4 ? S 0:00 [kworker/0:0]
5 ? S 0:00 [kworker/u:0]
6 ? S 0:00 [rcu_kthread]
7 ? S< 0:00 [khelper]
8 ? S 0:00 [kworker/u:1]
104 ? S 0:00 [sync_supers]
106 ? S 0:00 [bdi-default]
108 ? S< 0:00 [kblockd]
119 ? S 0:00 [khubd]
219 ? S 0:00 [kswapd0]
220 ? S 0:00 [fsnotify_mark]
221 ? S< 0:00 [aio]
314 ? S< 0:00 [scsi_tgtd]
347 ? S< 0:00 [kpsmoused]
349 ? S 0:00 [kworker/0:1]
366 ? S 0:00 [w1_bus_master1]
390 ? S 0:00 [mmcqd/0]
395 ? S 0:00 [jbd2/mmcblk0p2-]
396 ? S< 0:00 [ext4-dio-unwrit]
475 ? S 0:00 [flush-179:0]
4532 ttyS0 Ss 0:00 init [S]
4533 ttyS0 S 0:00 bash
4536 ttyS0 R+ 0:00 ps ax
The system I'm working on is Debian GNU/Linux 5.0 debarm (embedded). Any hints or solution? Thank you.
As said here:
Run Level 1 is known as 'single user' mode. A more apt description
would be 'rescue', or 'trouble-shooting' mode. In run level 1, no
daemons (services) are started.
You have several options to get around this limitation:
Don't use runlevel 1, this is not what it is meant for
Start the watchdog on login (if this is a valid option), e.g. .bashrc.
I solved it by adding following line into /etc/inittab:
~~:S:wait:/etc/init.d/watchdog start
~~:S:wait:/sbin/sulogin
Note: It must be before /sbin/sulogin.

How to see a terminal output from a previously closed terminal

I connect to a remote server using SSH
I was compiling using cmake and then make, it's not common to have a progress percentage in compilation process, but this time it has. I was watching the compilation process until my internet connection failed, so puTTY closed the session and I had to connect again to my server. I though that all the progress was lost, but i first make sure by watching the processes list by ps aux command, and I noticed that the processes related to the compilation are still running:
1160 tty1 Ss+ 0:00 /sbin/mingetty tty1
2265 ? Ss 0:00 sshd: root#pts/1
2269 pts/1 Ss 0:00 -bash
2353 pts/1 S+ 0:00 make
2356 pts/1 S+ 0:00 make -f CMakeFiles/Makefile2 all
2952 ? S 0:00 pickup -l -t fifo -u
3085 ? Ss 0:00 sshd: root#pts/0
3089 pts/0 Ss 0:00 -bash
3500 pts/1 S+ 0:01 make -f src/compiler/CMakeFiles/hphp_analysis.dir/bui
3509 pts/1 S+ 0:00 /bin/sh -c cd /root/hiphop/hiphop-php/src/compiler &&
3510 pts/1 S+ 0:00 /usr/bin/g++44 -DNO_JEMALLOC=1 -DNO_TCMALLOC=1 -D_GNU
3511 pts/1 R+ 0:03 /usr/libexec/gcc/x86_64-redhat-linux6E/4.4.4/cc1plus
3512 pts/0 R+ 0:00 ps ax
I would like to know if is possible to watch the current progress of the compilation by watching the previously closed terminal output. Something similar like 'cat /dev/vcsa1' or something
As per the comment above, you should have used screen.
As it is, you could try to peek at the file descriptors used by sshd and the shell that you started, but I don't think this will get you very far.

Resources