restrict pidof to own processes - bash

In bash,
pidof unison
will list me unison processes of all users. However I am only interested in instances started under my uid.
How can I achieve this?
What I want to do:
I'm periodically syncing several accounts on several machines using unison. However, I want the sync to not start, if the current user has manually started unison.

You could use pgrep instead, like
pgrep -u <userid> unison
which will return a list of pids of unison processes that have the euid of the given user.

You can parse ps output like this:
ps -ef | grep unison | grep -v grep | grep "${USER}" | awk '{print $2}'

Related

How to use xargs to pass arguments as sudo to given program in terminal shell

I want kill a series process like follow:(rg is a grep alternative)
ps -aux | rg "some_procs" | awk '{print $2}' | xargs kill -9
but It raise an error message show I don't have permission. I also can't add sudo neither after xargs or behind xargs.(seems it will cause xargs assign argus to sudo rather kill).
How do I use sudo in this pipe link to kill those processes?
ps -aux | rg "mytmp" | awk '{print $2}' | xargs sudo kill -9
this command worked.
In addition to
the answer,
if you are using GNU/Linux box, you could have use
pkill so
sudo pkill some_procs
Also note that, otherwise, your can pass many PIDs to kill OK. You don't need all ps default fields, but only PID and name to filter. Last, you can skip the rg part because you can do it in awk directly.
sudo kill -9 $(ps -o pid,args | awk '$2 ~ /some_procs/ {$1}')

How do I make a stop after a run?

If I do a:
npm run script
Can I stop it with a stop?
npm stop script
Why I tried it and it does not work.
I know that with the combination of "Ctrl + c" I kill it, but I want to do it by command.
Try something like that:
ps -ef | grep script | awk '{print $2}' | head -n1 | xargs kill -9
This command should find first process named script on the list of all unix processes created by all users and kill it by with using its PID.

Get the PID from the child process

I am having an issue trying to get the PID from a command using Time.
The command I use is:
{ time cp ubuntu/ubuntu-16.04.2-desktop-amd64.iso
ubuntucopia/$i-ubuntu-16.04.2-desktop-amd64.iso; }
2>> "logs/time.log" &
If I use now $!, I've get te PID from TIME. How could I do to get the pid of the command cp? Currently to solve this I am using this:
father=$!
cpPid=$(pgrep -P $father)
With this, not always I get the pid, sometimes $cpPid is empty.
Thank you!
This will get you the pid of the cp command, though you should use a search string more specific than cp, as this sample has a high potential for multiple matches.
ps -eo pid,cmd | grep cp | grep -v grep | awk '{print $1}'

Kill command won't work correctly in bash script

I was running an ubuntu console, when I type the following command, all the processes would be perfectly killed.
kill -9 $(ps -ef | grep 'job1/' | grep -v grep| awk '{print $2}')
But when I was trying to use crontab to call a script routinely, things went wrong.
#!/bin/bash
pid=$(ps -ef | grep 'job1/' | grep -v grep | awk '{print $2}')
echo $pid
kill -9 $pid
# the following commands were never executed
sleep 5
/data/job1/tomcat8/bin/startup.sh
The result was just like this:
15432 15438
Killed
It seems to just killed the job, but won't execute the following commands. Any idea?
If you are going to make a script that kills things by PID then you need to be very careful that you kill the right things.
You already have grep -v grep to avoid killing the grep itself, but it seems that you have not put in anything to protect against the script killing itself. Since you know your own PID you could grep -v that, but what if you are 123 and one of the things you want to kill is 1234? Probably safer to go by script name.

Killing processes SHELL

This command ps -ef | grep php returns a list of processes
I want to kill in one command or with a shell script all those processes
Thanks
The easiest way to kill all commands with a given name is to use killall:
killall php
Note, this only sends an interrupt signal. This should be enough if the processes are behaving. If they're not dying from that, you can forcibly kill them using
killall -9 php
The normal way to do this is to use xargs as in ps -ef | grep php | xargs kill, but there are several ways to do this.
ps -ef lists all processes and then you use grep to pick a few lines that mention "php". This means that also commands that have "php" as part of their command line will match, and be killed. If you really want to match the command (and not the arguments as well), it is probably better to use pgrep php.
You can use a shell backtick to provide the output of a command as arguments to another command, as in
kill `pgrep php`
If you want to kill processes only, there is a command pkill that matches a pattern to the command. This can not be used if you want to do something else with the processes though. This means that if you want to kill all processes where the command contain "php", you can do this using pkill php.
Hope this helps.
You can find its pid (it's on the first column ps prints) and use the kill command to forcibly kill it:
kill -9 <pid you found>
Use xargs:
ps -ef | grep php | grep -v grep | awk '{print $2}' | xargs kill -9
grep -v grep is exclude the command itself and awk gives the list of PIDs which are then passed kill command.
Use pkill php. More on this topic in this similar question: How can I kill a process by name instead of PID?

Resources