How do I put an already running CHILD process under nohup - bash

My question is very similar to that posted in: How do I put an already-running process under nohup?
Say I execute foo.sh from my command line, and it in turn executes another shell script, and so on. For example:
foo.sh
\_ bar.sh
\_ baz.sh
Now I press Ctrl+Z to suspend "foo.sh". It is listed in my "jobs -l".
How do I disown baz.sh so that it is no longer a grandchild of foo.sh? If I type "disown" then only foo.sh is disowned from its parent, which isn't exactly what i want. I'd like to kill off the foo.sh and bar.sh processes and only be left with baz.sh.
My current workaround is to "kill -18" (resume) baz.sh and go on with my work, but I would prefer to kill the aforementioned processes. Thanks.

Use ps to get the PID of bar.sh, and kill it.
imac:barmar $ ps -l -t p0 -ww
UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD
501 3041 3037 4006 0 31 0 2435548 760 - Ss 8c6da80 ttyp0 0:00.74 /bin/bash --noediting -i
501 68228 3041 4006 0 31 0 2435544 664 - S 7cbc2a0 ttyp0 0:00.00 /bin/bash ./foo.sh
501 68231 68228 4006 0 31 0 2435544 660 - S c135a80 ttyp0 0:00.00 /bin/bash ./bar.sh
501 68232 68231 4006 0 31 0 2435544 660 - S a64b7e0 ttyp0 0:00.00 /bin/bash ./baz.sh
501 68233 68232 4006 0 31 0 2426644 312 - S f9a1540 ttyp0 0:00.00 sleep 100
0 68243 3041 4106 0 31 0 2434868 480 - R+ a20ad20 ttyp0 0:00.00 ps -l -t p0 -ww
imac:barmar $ kill 68231
./foo.sh: line 3: 68231 Terminated ./bar.sh
[1]+ Exit 143 ./foo.sh
imac:barmar $ ps -l -t p0 -ww
UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD
501 3041 3037 4006 0 31 0 2435548 760 - Ss 8c6da80 ttyp0 0:00.74 /bin/bash --noediting -i
501 68232 1 4006 0 31 0 2435544 660 - S a64b7e0 ttyp0 0:00.00 /bin/bash ./baz.sh
501 68233 68232 4006 0 31 0 2426644 312 - S f9a1540 ttyp0 0:00.00 sleep 100
0 68248 3041 4106 0 31 0 2434868 480 - R+ 82782a0 ttyp0 0:00.00 ps -l -t p0 -ww

Related

.bashrc somehow looping and sourcing itself (fork bomb)

I'm using a web host with an Apache terminal, using it to host a NodeJS application. For the most part everything runs smooth, however when I open the terminal I often get bash: fork: retry: no child processes and bash: fork: retry: resource temporarily unavailable.
I've narrowed down the cause of the problem to my .bashrc file, as when using top I could see that the many excess processes being created were bash instances:
top - 13:41:13 up 71 days, 20:57, 0 users, load average: 1.82, 1.81, 1.72
Tasks: 14 total, 1 running, 2 sleeping, 11 stopped, 0 zombie
%Cpu(s): 11.7 us, 2.7 sy, 0.1 ni, 85.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 41034544 total, 2903992 free, 6525792 used, 31604760 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 28583704 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1001511 xxxxxxxx 20 0 11880 3692 1384 S 0.0 0.0 0:00.02 bash
1001578 xxxxxxxx 20 0 11880 2840 524 T 0.0 0.0 0:00.00 bash
1001598 xxxxxxxx 20 0 11880 2672 348 T 0.0 0.0 0:00.00 bash
1001599 xxxxxxxx 20 0 11880 2896 524 T 0.0 0.0 0:00.00 bash
1001600 xxxxxxxx 20 0 11880 2720 396 T 0.0 0.0 0:00.00 bash
1001607 xxxxxxxx 20 0 11880 2928 532 T 0.0 0.0 0:00.00 bash
1001613 xxxxxxxx 20 0 11880 2964 532 T 0.0 0.0 0:00.00 bash
1001618 xxxxxxxx 20 0 11880 2780 348 T 0.0 0.0 0:00.00 bash
1001619 xxxxxxxx 20 0 12012 3024 544 T 0.0 0.0 0:00.00 bash
1001620 xxxxxxxx 20 0 11880 2804 372 T 0.0 0.0 0:00.00 bash
1001651 xxxxxxxx 20 0 12012 2836 352 T 0.0 0.0 0:00.00 bash
1001653 xxxxxxxx 20 0 12016 3392 896 T 0.0 0.0 0:00.00 bash
1004463 xxxxxxxx 20 0 9904 1840 1444 S 0.0 0.0 0:00.00 bash
1005200 xxxxxxxx 20 0 56364 1928 1412 R 0.0 0.0 0:00.00 top
~/.bashrc consists of only:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
If I comment out the last 3 lines like so:
#export NVM_DIR="$HOME/.nvm"
#[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
#[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Then the terminal functions as expected and no excess processes are created. However I obviously can't use nvm/npm commands while it's disabled as nvm isn't started.
I'm relatively inexperienced with bash and can't seem to figure out why this is happening. It seems that bash is somehow calling itself every time it opens, which creates the loop/fork bomb once the terminal is opened.
How can I prevent this while still being able to use nvm/npm?

Jump to the top parent shell from any arbitrary depth of subshell

I created multiple subshells
$ ps -f
UID PID PPID C STIME TTY TIME CMD
501 2659 2657 0 8:22AM ttys000 0:00.15 -bash
501 2776 2659 0 8:23AM ttys000 0:00.02 bash
501 2778 2776 0 8:23AM ttys000 0:00.09 bash
501 3314 2778 0 9:13AM ttys000 0:00.26 bash
501 8884 3314 0 4:41PM ttys000 0:00.03 /bin/bash
501 8891 8884 0 4:41PM ttys000 0:00.01 /bin/bash
501 8899 8891 0 4:41PM ttys000 0:00.02 /bin/bash
501 423 408 0 7:16AM ttys001 0:00.22 -bash
501 8095 423 0 3:52PM ttys001 0:00.15 ssh root#www.****.com
501 8307 8303 0 4:05PM ttys002 0:00.17 -bash
I'd like to jump back the most top one, but have to try exit one by one
$ ps -f
UID PID PPID C STIME TTY TIME CMD
501 2659 2657 0 8:22AM ttys000 0:00.17 -bash
501 423 408 0 7:16AM ttys001 0:00.22 -bash
501 8095 423 0 3:52PM ttys001 0:00.15 ssh root#***.com
501 8307 8303 0 4:05PM ttys002 0:00.17 -bash
I checked there are 3 bashes left, so I continue,
$ exit
logout
Saving session...completed.
[Process completed]
Sad, it's the most cases I encounter, How could I jump to the top from arbitrary depth of subshells?

Fail of bash script with flock

I have a task: Calling a script by curl every ten seconds.
For that I do:
Create bash-script: admin_cron.sh
#!/bin/bash
while :; do
sleep 10
flock -n /var/www/admin/data/cron_lock -c \
'curl -m 3 "http://url.com/?mod=kasdim_robot_cron&server=true"' &
done
Add rule in crontab:
*/1 * * * * flock -n /var/www/admin/data/admin_lock -c /var/www/admin/data/admin_cron.sh
So, cron will start admin_cron.sh every minute if the file admin_lock is free. Then, if admin_cron.sh is terminated or dies, admin_cron.sh starts again. It's ok.
admin_cron.sh tries to call every 10 sec my URL through curl. Max time for curl is three seconds.
Problem: when I start, everything works fine for about two to three days; after, I see in the htop util that admin_cron.sh is hover process:
at start time:
CPU[ 0.0%] Tasks: 61, 53 thr; 1 running
Mem[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||461M/996M] Load average: 0.44 0.83 0.94
Swp[ 0K/0K] Uptime: 86 days, 18:43:58
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
707 root 20 0 29600 2828 2512 S 0.0 0.3 1:27.18 ├─ /usr/sbin/cron -f
22150 root 20 0 48868 2864 2416 S 0.0 0.3 0:00.00 │ └─ /usr/sbin/CRON -f
22151 admin 20 0 4288 720 648 S 0.0 0.1 0:00.00 │ └─ /bin/sh -c flock -n /var/www/admin/data/admin_lock -c /var/www/admin/data/admin_cron.sh
22156 admin 20 0 10044 816 728 S 0.0 0.1 0:00.00 │ └─ flock -n /var/www/admin/data/admin_lock -c /var/www/admin/data/admin_cron.sh
22161 admin 20 0 4288 764 692 S 0.0 0.1 0:00.00 │ └─ /bin/sh -c /var/www/admin/data/admin_cron.sh
22167 admin 20 0 11132 2896 2648 S 0.0 0.3 0:00.00 │ └+ /bin/bash /var/www/admin/data/admin_cron.sh
After some time:
CPU[|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%] Tasks: 64, 53 thr; 2 running
Mem[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||466M/996M] Load average: 1.00 1.00 1.00
Swp[ 0K/0K] Uptime: 86 days, 18:42:30
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
707 root 20 0 29600 2828 2512 S 0.0 0.3 1:27.18 ├─ /usr/sbin/cron -f
25818 root 20 0 48868 2864 2416 S 0.0 0.3 0:00.00 │ └─ /usr/sbin/CRON -f
25819 admin 20 0 4288 752 676 S 0.0 0.1 0:00.00 │ └─ /bin/sh -c flock -n /var/www/admin/data/admin_lock -c /var/www/admin/data/admin_cron.sh
25821 admin 20 0 10044 800 712 S 0.0 0.1 0:00.00 │ └─ flock -n /var/www/admin/data/admin_lock -c /var/www/admin/data/admin_cron.sh
25824 admin 20 0 4288 788 716 S 0.0 0.1 0:00.00 │ └─ /bin/sh -c /var/www/admin/data/admin_cron.sh
25825 admin 20 0 12636 4260 2552 R 98.7 0.4 60h56:25 │ └─ /bin/bash /var/www/admin/data/admin_cron.sh
Where is my problem?

Optimizing vncscreenshot scripts

Good Day,
I'm using vncsnapshot http://vncsnapshot.sourceforge.net/ in debian 7 environment to capture screenshots of workstations to monitor staffs desktop activity. This captures screenshot via nmap and saves it to my desired location accessed via internal web-page.
I have scripts like this . The x.x.x.x is the ip-range of the network to capture all open workstations.
#!/bin/bash
nmap -v -p5900 --script=vnc-screenshot-it --script-args vnc-screenshot.quality=30 x.x.x.x
And set-up in crontab to run every 5 mins.
The server has too many running processes because of it. This is the sample of ps command
root 32696 0.0 0.0 4368 0 ? S Feb23 0:00 /bin/bash /var/www/vncsnapshot/.scripts/.account.sh
root 32708 0.0 0.0 14580 4 ? S Feb23 0:00 nmap -v -p5900,5901,5902 --script=vnc-screenshot-mb
root 32717 0.0 0.0 1952 60 ? S Apr10 0:00 sh -c vncsnapshot -cursor -quality 30 x.x.x.x
root 32719 0.0 0.1 11480 4892 ? S Apr10 0:00 vncsnapshot -cursor -quality 30 30 x.x.x.x /var/w
root 32720 0.0 0.0 1952 60 ? S Apr25 0:00 sh -c vncsnapshot -cursor -quality 30 30 x.x.x.x
root 32722 0.0 0.0 1952 4 ? Ss Feb09 0:00 /bin/sh -c /var/www/vncsnapshot/.scripts/.account.sh
root 32723 0.0 0.0 3796 140 ? S Apr25 0:00 vncsnapshot -cursor -quality 30 30 x.x.x.x /var/w
root 32730 0.0 0.0 1952 4 ? Ss Feb08 0:00 /bin/sh -c /var/www/vncsnapshot/.scripts/.account
root 32734 0.0 0.0 4364 0 ? S Feb08 0:00 /bin/bash /var/www/vncsnapshot/.scripts/.account.
root 32741 0.0 0.0 13700 4 ? S Feb08 0:00 nmap -v -p5900 --script=vnc-screenshot-account --
root 32755 0.0 0.0 1952 4 ? Ss Feb08 0:00 /bin/sh -c /var/www/vncsnapshot/.scripts/.account.sh
root 32757 0.0 0.0 1952 4 ? S Feb07 0:00 sh -c vncsnapshot -cursor -quality 30 30 x.x.x.x
root 32760 0.0 0.0 3796 0 ? S Feb07 0:00 vncsnapshot -cursor -quality 30 30 x.x.x.x /var/w
root 32762 0.0 0.0 4368 0 ? S Feb09 0:00 /bin/bash /var/www/vncsnapshot/.scripts/.account.sh
root 32764 0.0 0.0 4368 0 ? S Feb08 0:00 /bin/bash /var/www/vncsnapshot/.scripts/.account.sh
How can I optimize this set-up to close un-nessesary processes that are still running.
Thanks
I split the processes in two part: nmap that regularly scan the network and the vncsnapshot that grab screenshot of a list of previously scanned host.
In my opinion, in this way the things are cleaner.
i haven't test this code
#!/bin/bash
## capture the list of host with vnc port open
list=/dev/shm/list
port=5900
network=192.168.1.*
nmap -n -p${port} --open ${network} -oG - | grep 'open\/tcp' | awk '{print $2}' > ${list}
the other script, check if a process is alive with lock file and in case launch the grab command
#!/bin/bash
list=/dev/shm/list
run=/run/vncscreenshot/
mkdir -p ${run} &>/dev/null
cat ${list} |\
while read host
do
lock="${run}/${host}.lock"
test -e ${lock} && ps -p $(<${lock}) &>/dev/null && continue
vnc-screenshot-it vnc-screenshot.quality=30 ${host} &
echo $! > ${lock}
done

Append Output results

I'm running a validation software and I want all of the output sent to a text file and have the results of multiple files placed/appended to the same file. I thought my code was working, but I just discovered I'm only getting the results from 1 file output to the text file.
java -jar /Applications/epubcheck-3.0.1/epubcheck-3.0.1.jar ~/Desktop/Validator/*.epub 2>&1 | tee -a ~/Desktop/Validator/EPUBCHECK3_results.txt
open ~/Desktop/Validator/EPUBCHECK3_results.txt
EDIT
When I run the same .jar file using Windows command line it will process a batch of files and appeand the results appropriately. I would just do this, but it would mean having to switch work stations and transferring files to validate them. I would like to get this running through the Unix shell on my Mac system so that I don't have to do unnecessary work. Command line that IS working below:
FOR /f %%1 in ('dir /b "C:\Users\scrawfo\Desktop\epubcheck\drop epubs here\*.epub"') do (
echo %%1 >> epubcheck.txt
java -jar "C:\Users\scrawfo\Desktop\epubcheck\epubcheck-3.0.jar" "C:\Users\scrawfo\Desktop\epubcheck\drop epubs here\%%1" 2>> epubcheck.txt
echo. >> epubcheck.txt)
notepad epubcheck.txt
del epubcheck.txt
syntax provided by you is correct there might be some problem with java output or something Try Executing it without redirection
cat test
Output:-
This is Test File ...............
Next Executed Command with same syntax
ps l 2>&1 | tee -a test
Output:-
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME
COMMAND 4 0 3287 1 20 0 4060 572 n_tty_ Ss+ tty2
0:00 /sbin/mingetty /dev/tty2 4 0 3289 1 20 0 4060 572
n_tty_ Ss+ tty3 0:00 /sbin/mingetty /dev/tty3 4 0 3291
1 20 0 4060 576 n_tty_ Ss+ tty4 0:00 /sbin/mingetty
/dev/tty4 4 0 3295 1 20 0 4060 576 n_tty_ Ss+ tty5
0:00 /sbin/mingetty /dev/tty5 4 0 3297 1 20 0 4060 572
n_tty_ Ss+ tty6 0:00 /sbin/mingetty /dev/tty6 4 0 19086
1 20 0 4060 572 n_tty_ Ss+ tty1 0:00 /sbin/mingetty
/dev/tty1 4 0 20837 20833 20 0 108432 2148 wait Ss pts/0
0:00 -bash 4 0 21471 20837 20 0 108124 1036 - R+ pts/0
0:00 ps l 0 0 21472 20837 20 0 100908 664 pipe_w S+ pts/0
0:00 tee -a test
Checked File
cat test
Output:-(Appended properly)
This is Test File ...............
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND 4 0
3287 1 20 0 4060 572 n_tty_ Ss+ tty2 0:00
/sbin/mingetty /dev/tty2 4 0 3289 1 20 0 4060 572
n_tty_ Ss+ tty3 0:00 /sbin/mingetty /dev/tty3 4 0 3291
1 20 0 4060 576 n_tty_ Ss+ tty4 0:00 /sbin/mingetty
/dev/tty4 4 0 3295 1 20 0 4060 576 n_tty_ Ss+ tty5
0:00 /sbin/mingetty /dev/tty5 4 0 3297 1 20 0 4060 572
n_tty_ Ss+ tty6 0:00 /sbin/mingetty /dev/tty6 4 0 19086
1 20 0 4060 572 n_tty_ Ss+ tty1 0:00 /sbin/mingetty
/dev/tty1 4 0 20837 20833 20 0 108432 2148 wait Ss pts/0
0:00 -bash 4 0 21471 20837 20 0 108124 1036 - R+ pts/0
0:00 ps l 0 0 21472 20837 20 0 100908 664 pipe_w S+ pts/0
0:00 tee -a test

Resources