execution of a command (unexpected behaviour) - unix

ps -ef | grep "someprocess" output lines contain :
.........................someprocess
.........................ps -ef | grep "someprocess"
Can somebody explain how this gets executed??
If I am checking for a process that is running or not it will allways come as running since
ps -ef | grep "someprocess" will always be there
Of course I know that I can do ps -ef | grep "someprocess" | grep -v 'grep'
But I want to know the process of execution of the above to be clear. why do I get the line that I just executed (ps -ef | grep "someprocess") searching for a process?

From this blog post:
Why does grep show itself? Because the pipe is created by the shell an instant before ps is executed so when the latter searches for all the loaded processes even grep is found.

Or use
pgrep -fl somepattern
that will not show the pgrep process by default
Similarly, pkill will not kill itself :)

Related

Not able to start grakn, Storage is not able to start

I have installed grakn on unix and earlier it was working fine but now giving issues as it is not able to start.
I tried to run it using below command:
./grakn server start
Getting below error.
Starting Storage-FAILED!
Unable to start Storage
Please run 'grakn server status' or check the logs located under 'logs' directory.
There may be lot of things happening under the hood. Without looking into logs this is hard to tell what exactly happening. You can try killing all the processes and then remove associate pids from /tmp/ directory. Then retry starting grakn server.
$ for KILLPID in `ps ax | grep 'grakn' | awk '{print $1}'`; do kill -9 $KILLPID; done
$ ps -ef | grep defunct | grep -v grep | cut -b8-20 | xargs kill -9
$ rm -rf /tmp/grakn-*
Let me know if it helps.

Using grep and wc to search and count matching processes

I am using ps to list all of the processes running on the machine I am connected to, searching them for my own processes, and then printing the number of processes I am running, like so:
ps -Af | grep '^mkuhlman' | wc -l
Problem is, checking against the actual list of processes, I'm only running 8, but wc is listing 9 processes. What am I doing wrong?
To clarify, I am not looking for matches to processes, but matches to my own username.
Your pipeline has a few processes, and you're counting them.
Running ps is fine, but you might be happier with pgrep. It has a man page. (And ps -A seems at odds with grepping for your own username.)
Though grep -v grep would work in most cases, it can result in wrong output as it excludes all grep processes, not just the ones related to the ps command line. So, you could do this instead:
ps -Af | grep -E '^mkuhlman|__unique__' | grep -v __unique__
where __unique__ is a unique string that is unlikely to be used in the command line of other user processes.
See also:
Finding process count in Linux via command line

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 &

Increase the performace of the code by reducing the number of ssh

This function take hugh amount of time to calculate the status of a process, beacuse every time it has to ssh into the machine and find the status of a process.
I only have four machines and around 50+ process to monitor and the details are mentioned into configDaemonDetails.txt
like:
abc#sn123|Daemon_1|processname_1
abc#sn123|Daemon_2|processname_2
efg#sn321|Daemon_3|processname_3
How to reduce the time with doing ssh once into a machine and finding all its process informations as defined in the txt file. ?
CheckProcessStatus ()
{
echo " ***** Checking Process Status ***** "
echo "========================================================="
IFS='|'
cat configDaemonDetails.txt | grep -v "^#" | while read MachineDetail Daemon ProcessName
do
Status=`ssh -f -T ${MachineDetail} ps -ef | egrep -v "grep|less|vi|more" | grep "$ProcessName"`
RunTime=`echo "$Status" | sed -e 1'p' -e '1,$d' | awk '{print $5" "$6}'`
if [ -z "$Status" ]
then
echo "The Process is DOWN $Daemon | $ProcessName "
else
echo "The Process $Daemon | $ProcessName is up since $RunTime"
fi
done
echo "-----------------------------------------------------"
}
Thanks :)
Can't you just fetch the entire ps -ef output at once, and then parse it appropriately? I suspect that is what you are asking, and maybe all you want is an example of how to do that? If that is the case, say so and I'll flesh out an example.
SSH is a bit over kill for getting status of a process, I'd suggest using SNMP instead.
e.g, you can get a process list like this:
snmpwalk -v2c -cPASSWORD HOST 1.3.6.1.2.1.25.4.2.1
Take a look at this Nagios plugin that does process checks, and look in the code for the actual SNMP OIDs.

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