ubuntu shell scripting - process

I'm new to shell scripting. I need to write a script that executes this command to get the process ID's for the tasks...
ps aux | grep java | grep dbConvert2 | awk '{print $2}'
then do some other stuff, and then kill the process ID's that I grabbed earlier...
I know I can do kill -9, i just don't know how to dynamically grab all the PID's and store them as variables

append | xargs kill -9 to your current command
[edit]
if you want to do some operations on each id, you can use a for loop, something like:
for my_pid in `YOUR_CMD`; do
<some stuff with $my_pid>
kill -9 $my_pid
done

pkill -9 'java.*dbConvert2'
You might want to use pgrep 'pattern' to try different patterns before.
Edit: If your process isn't matched, you might need to use -f (applies to both pgrep and pkill, use after the -9 though) to search the entire command including arguments.
Example: pkill -9 -f 'java.*dbConvert2'

Related

AIX - How to kill using process name instead of PID

Is there a way to kill a process by specifying the process name instead of PID for AIX?
E.g. for the below process I want to kill it by specifying sapstartsrv instead of 10682424
hmsadm 10682424 1 0 Apr 30 - 0:54 /usr/sap/HMS/ASCS01/exe/sapstartsrv pf=/usr/sap/HMS/SYS/profile/START_ASCS01_H\
Thanks.
You can use command like this:
kill -9 $(ps -ef|grep sapstartsrv|awk '{print $2}')
of course first check if command ps -ef|grep sapstartsrv|awk '{print $2}' return only processes you want to kill
Try this. The brackets around the first letter of process you are trying to kill helps. Obviously change this to a valid server.
while true; do date; ping -c4 server; sleep 500; done &
ps -aef | grep -i [p]ing | awk '{print $2}' | xargs kill -9
If that doesn't work sometimes you have to kill the parent process.
ps -aef | grep -i [p]ing | awk '{print $2 " " $3}' | xargs kill -9

I want to stop nohup script

I've started nohup command to delete 1 month worth of data:
Nohup sqlplus username/password#sidname #/home/month.sql
After deleting some 10 days of data from that script, I want to stop the nohup script, or I want to keep the remaining 20 days of data as it is.
Please help with this
If you know the PID of the script, then kill -9 PID.
else find the PID first by using ps -ef | grep "Nohup sqlplus", then kill -9 PID.

How to gzip tee output while running in gnu parallel?

Suppose I am running tee inside a command executed by parallel.
I would like to gzip the output from tee:
... | tee --gzip the_file | and_continue
bash process substitution is useful for cases like this. Something like:
... | tee >(gzip -c the_file) | and_continue
If you're choosing different files in a parallel run and need to format the name differently each time, take a look at GNU Parallel argument placeholder in bash process substitution for how that has to change (to defer the process substitution to act per parallel job).

--immediate-submit {dependencies} string contains script paths, not job IDs?

I'm trying to use the --immediate-submit on a PBSPro cluster. I tried using an in-place modification of the dependencies string to adapt it to PBSPro, similar to what is done here.
snakemake --cluster "qsub -l wd -l mem={cluster.mem}GB -l ncpus={threads} -e {cluster.stderr} -q {cluster.queue} -l walltime={cluster.walltime} -o {cluster.stdout} -S /bin/bash -W $(echo '{dependencies}' | sed 's/^/depend=afterok:/g' | sed 's/ /:/g')"
This last part gets converted into, for example:
-W depend=afterok: /g/data1a/va1/dk0741/analysis/2018-03-25_marmo_test/.snakemake/tmp.cyrhf51c/snakejob.trimmomatic_pe.7.sh
There are two problems here:
How can I get the dependencies string to output job ID instead of the script path? The qsub command normally outputs the job ID to stdout, so I'm not sure why it's not doing so here.
How do I get rid of the space after afterok:? I've tried everything!
As an aside, it would be helpful if there were some option to debug the submission or not to delete the tmp.cyrhf51c directory in .snakemake -- is there some way to do this?
Thanks,
David
I suggest to use a profile for this, instead of trying to find an ad-hoc solution. This will also help with debugging. E.g., there is already a pbs-torque profile available (https://github.com/Snakemake-Profiles/pbs-torque), probably there is not much to change towards pbspro?

How to kill a process in cygwin?

Hi i have the following process which i cant kill:
I am running cygwin in windows xp 32 bit.
I have tried issuing the following commands:
/bin/kill -f 4760
/bin/kill -9 5000
kill -9 5000
kill 5000
When i write /bin/kill -f 4760 i get the message, 'kill: couldn't open pid 4760'.
When i write /bin/kill -9 5000 i get the message, 'kill: 5000: No such process'.
I simply don't understand why this process cant be killed.
Since it has a WINID shouldnt it be killed by /bin/kill -f 4760?
hope someone can help thx :)
The process is locked from Windows most likely. The error you are getting "couldnt open PID XXX" points to this.
To confirm try killing it with windows taskkill
taskkill /PID 4760
Strangely, the following works in Cygwin:
echo PID1 PID2 PID3 | xargs kill -f
For example:
ps -W | grep WindowsPooPoo | awk '{print $1}' | while read line; do echo $line | xargs kill -f; done;
Different Windows programs will handle the signals that kill sends differently; they've never been designed to deal with them in the same way that Linux/Cygwin programs are.
The only reliable method for killing a Windows program is to use a Windows specific tool, such as Task Manager or Process Explorer.
That said, if you've not already, you may have luck with running your Cygwin terminal in administrator mode (right click on your shortcut and select "Run as administrator").
The method presented by #Donal Tobin is correct:
kill -f <pid>
However, I don't need to log in as administrator.
Create a file called killall.sh with this line
ps -W | grep $1 | awk '{print $1}' | while read line; do echo $line | xargs kill -f; done;
Then give it execute permissions.
chmod 777 killall.sh
In your .bash_profile add this line
alias killall="~/killall.sh" (point it to the correct location)
Then you just have to type "killall [name]"
killall.sh - Kill by process name.
#/bin/bash
ps -W | grep "$1" | awk '{print $1}' | xargs kill -f;
Usage:
$ killall <process name>
For me this command does not work on Windows 10 in Cygwin:
$ kill -f 15916
bash: kill: (15916) - No such process
Instead of it, you can use next commands:
$ powershell kill -f 15916
$ netstat -ano | grep ':8080' | awk '{print $5}' | xargs powershell kill -f
$ netstat -ano | grep ':8080' | awk '{print $5}' | while read pid; do powershell kill -f $pid; done;
$ netstat -ano | grep ':8080' | awk '{sub(/\r/,"",$5) ; print $5}' | while read pid; do taskkill /F /PID $pid; done;
SUCCESS: The process with PID 15916 has been terminated.