Shell script when executed appears twice is the process list - unix

I am running the below shell script to run in background ./something.sh &
#!/bin/bash
tail -n0 -f -F service.log | while read LOGLINE
do
done
when i check ps -ef| grep something, i see two processes
20273 1 0 16:13 ? 00:00:00 /bin/bash /something.sh
20280 20273 0 16:13 ? 00:00:00 /bin/bash /something.sh

This is because your script is piping the output of a program to a shell command. When you run this there will be three processes:
The something.sh that you explicitly started
The tail that your script starts
A copy of something.sh that is executing the while loop.

Related

I ran a script using nohup which calls 3 subscripts inside

nohup ksh mail_failures.ksh >temp.out &
As this script calls another 3 scripts and it runs forever..I couldn't kill this job..I tried below
ps -ef | grep nohup
This command giving only one output with pid..after I killed that and
It showing with the different PID
How can I resolve this?
How can we get the pid of parent?
Everytime I do ps -ef |grep nohup I'm getting only one output..and if I try to kill it again it's saying it coming with a different PID..?

Calling Rscript from linux shell script

Can anyone suggest how I might get this working....
I have an R script that takes several minutes to run and writes a few hundred lines of output. I want to write a shell script wrapper around this R script which will launch the R script in the background, pipe its output to a file and start following the bottom of that file. If the user then enters CTRL-C I want that to kill the shell script and tail command but not the R script. Sounds simple right?
I've produced a simplified example below, but I don't understand why this doesn't work. Whenever I kill the shell script the R script is also killed despite apparently running in the background. I've tried nohup, disown etc with no success.
example.R
for(i in 1:1000){
Sys.sleep(1)
print(i)
}
wrapper.sh
#!/bin/bash
Rscript example.R > logfile &
tail -f logfile
Thanks in advance!
The following seems to work on my Ubuntu machine:
#!/bin/bash
setsid Rscript example.R > logfile.txt &
tail -f logfile.txt
Here are some of the relevant processes before sending SIGINT to wrapper.sh:
5361 pts/10 00:00:00 bash
6994 ? 00:00:02 update-notifier
8519 pts/4 00:00:00 wrapper.sh
8520 ? 00:00:00 R
8521 pts/4 00:00:00 tail
and after Ctrl+C, you can see that R is still running, but wrapper.sh and tail have been killed:
5361 pts/10 00:00:00 bash
6994 ? 00:00:02 update-notifier
8520 ? 00:00:00 R
Although appending your Rscript [...] command with & will send it to the background, it is still part of the same process group, and therefore receives SIGINT as well.
I'm not sure if it was your intention, but since you are calling tail -f, if not interrupted with Ctrl+c, your shell that is running wrapper.sh will continue to hang even after the R process completes. If you want to avoid this, the following should work,
#!/bin/bash
setsid Rscript example.R > logfile.txt &
tail --pid="$!" -f logfile.txt
where "$!" is the process id of the last background process executed (the Rscript [...] call).

Kill nohup process unix

I am running jar using nohup command
nohup java -jar Test.jar
I can't find PID of the jar I am running. I try with:
ps ax | grep java
but it is not listing, then I tried with
ps -ef | grep nohup
and I got this output
root 8503 7529 0 21:52 pts/0 00:00:00 grep nohup
which of the PIDs is PID of nohup process? The first one 8503 is always diffrent, while the second one is the same.
Maybe the program test.jar is finished. See output in nohup.out. To avoid to display grep as a process, use the following pattern:
ps -aux | grep 'j[a]va'
or
ps -aux | grep java | grep -v grep
The following command in your terminal may help you out to run the script using nohup and redirect the output in your desired file.
General Syntax
nohup some_command &> nohup_log_file.out &
Example
nohup python script.py &> nohup_log_script.out &

How to kill process inside container? Docker top command

I have simple example from official guide at docker website.
I run the following:
sudo docker run -d ubuntu:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"
a66asdasdhqie123...
Then take some output from created container:
sudo docker logs a66
hello
hello
hello
...
Then I lookup the running processes of a container:
sudo docker top a66
UID PID PPID C STIME TTY TIME CMD
root 25055 15152 0 20:07 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done
root 25295 25055 0 20:10 ? 00:00:00 sleep 1
Next I try to kill the first process of container:
sudo docker exec a66 kill -9 25055
However after I make it nothing changes. Process still works and output "hello" every second. What do I wrong?
When I reproduce your situation I see different PIDs between docker top <container> and docker exec -it <container> ps -aux. When you do docker exec the command is executed inside the container => should use container's pid. Otherwise you could do the kill without docker straight from the host, in your case: sudo kill -9 25055.
check this:
ps | grep -i a66 | tr -s ' '|cut -f2 -d' '|
{
while read line;
do kill -9 $line;
done
}
to understand this start from executing commands from left till end of each pipe (|)
Simpler option:
kill $(pidof a66)
Took me a while to find the right answer, but you will have to manage this process from within the container. When you run docker top a66 from the host, the PIDs are from your host, although that's not quite the case if using Cygwin. Regardless, you will need to bash or what-have-you back into your container and use commands like ps aux and kill while in the container to find and manage the different PIDs for the same processes there.
i was looking for something like this, but i couldn't find and then i did this:
[root#notebook ~]# docker exec -it tadeu_debian ps aux | grep ping |
awk '{ print $2 }' | xargs -I{} docker exec -i tadeu_debian kill -9
It was two "execs" from Docker e one xargs.
Well, i hope this helps someone!
when you build Docker, use this command :
RUN apt-get install lsof
then in py file you can use:
os.system("lsof /dev/nvidia* | awk '{print $2}' | xargs -I {} kill {}")
REMEMBER: this command kill all process on GPU

Kill and restart multiple processes that fit a certain pattern

I am trying write a shell script that will kill all processes that are running that match a certain pattern, then restart them. I can display the processes with:
ps -ef|grep ws_sched_600.sh|grep -v grep|sort -k 10
Which gives a list of the relevent processes:
user 2220258 1 0 16:53:12 - 0:01 /bin/ksh /../../../../../ws_sched_600.sh EDW02_env
user 5562418 1 0 16:54:55 - 0:01 /bin/ksh /../../../../../ws_sched_600.sh EDW03_env
user 2916598 1 0 16:55:00 - 0:01 /bin/ksh /../../../../../ws_sched_600.sh EDW04_env
But I am not too sure about how to pass the process ids to kill?
The sort doesn't seem necessary. You can use awk to print the second column and xargs to convert the output into command-line arguments to kill.
ps -ef | grep ws_sched_600.sh | awk '{print $2}' | xargs kill
Alternatively you could use pkill or killall which kill based on process name:
pkill -f ws_sched_600.sh
pkill ws_sched_600.sh
If you are concerned about running your command on multiple platforms where pkill might not be available
ps -ef | awk '/ws_sched_600/{cmd="kill -9 "$2;system(cmd)}
I think this is what you are looking for
for proc in $(ps -ef|grep ws_sched_600.sh|sort -k 10)
do
kill -9 proc
done
edit:
Of course... use xargs, it's better.

Resources