puppet exec command issue - linux

Below is my puppet code ,It is killing the process in agent but showing the error code
exec { "restart_process":
command => "ps -ef | grep nrpe | grep -v pts/1 | /bin/awk '{print $2}' | xargs kill -9",
path => ['/usr/bin', '/sbin', '/bin', '/usr/sbin'],
subscribe => File["/root/Backup/deploy"],
refreshonly => true,
}
Error code:
Info: Computing checksum on file /root/Backup/deploy/nrpe.sh
Info: /Stage[main]/Myfile/File[/root/Backup/deploy/nrpe.sh]: Filebucketed /root/Backup/deploy/nrpe.sh to puppet with sum 136c6013774f5dfb84943718a70b36e0
Notice: /Stage[main]/Myfile/File[/root/Backup/deploy/nrpe.sh]/content: content
changed '{md5}136c6013774f5dfb84943718a70b36e0' to '{md5}c4fc9099405a108923ad18f7e2db13c8'
Info: /root/Backup/deploy: Scheduling refresh of Exec[restart_process]
Error: /Stage[main]/Myfile/Exec[restart_process]: Failed to call refresh: ps -ef | grep nrpe | grep -v pts/1 | /bin/awk '{print }' | xargs kill -9 returned instead of one of [0]
Error: /Stage[main]/Myfile/Exec[restart_process]: ps -ef | grep nrpe | grep -v pts/1 | /bin/awk '{print }' | xargs kill -9 returned instead of one of [0]
Notice: Finished catalog run in 1.56 seconds
Can any body help me

#maxd is right that you need to escape the $ in the command when you specify it to Puppet. This is because Puppet performs its own variable interpolation on quotation-mark-quoted strings, and this will replace the $2 with the relevant replacement string -- probably an empty string -- before your command even reaches the catalog, and long before any attempt is made to run it.
I don't think that's your fundamental problem, however. Your command is a shell pipeline, but the default Exec provider for your system is almost certainly posix, and that provider will not do what you want with the command string you provided. You probably want to add
provider => 'shell'
to your resource, to have Puppet use the shell to execute it instead of trying to execute it directly as a (single) command.

Related

Kill command does the job but gives error

I'm using shell script to stop a python service and to do that I'm executing the below command.
ps -ef | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9
After executing I'm getting below error:
kill: sending signal to 487225 failed: Operation not permitted
But, when I check the terminal where the process is running, it terminates.
/home/saakhan/Documents/scripts/cluster-helpers.sh: line 75: 526839 Killed python3 src/service.py
Now, in my shell script I'm checking for the error if any, while stopping the service. So, it calls that error function which displays "Failed to stop the Service" even though the service is stopped successfully.
Do I need to change the command for this ?
Note: I have to use 'kill' only and not 'pkill
As #Bodo pointed out, there was more than one PIDs and only one of them got killed while the other showed error as it wasn't started by the same user.
I changed the grep command to filter the process started by the same user and then killed it, so it worked.
Update:
I used this command to successfully run my code and terminate the app as needed.
ps -u | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9

Grep the PIDs of simulator and kill the same using kubectl

I need help with the command where I am trying to grep the PIDs of ecm simulator and kill the same using kubectl :
kubectl exec eric-service-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "ps -ef | grep ecm | grep node | awk '{print $2}' "
Output of the above command:
root 9857 0 0 07:11 ? 00:00:00 bash -c /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js> /tmp/simulatorEcmResponse.txt
root 9863 9857 0 07:11 ? 00:00:00 /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js
Expected output is:
9857
9863
Then further I need to kill the PIDs:
kubectl exec eric-service-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "ps -ef | grep ecm | grep node | awk '{print $2}' | xargs kill -9"
When I am executing the same within the service pod it's working but it's giving issues when I am doing via kubectl from outside.
Could anyone please let me know what I am doing wrong here?
NOTE: There are 2 PIDs which needs to be killed from the below output:
eric-service-0:/ # ps -ef | grep ecm | grep node
root 9857 0 0 07:11 ? 00:00:00 bash -c /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js> /tmp/simulatorEcmResponse.txt
root 9863 9857 0 07:11 ? 00:00:00 /tmp/simulator/node-v8.11.3-linux-x64/bin/node /tmp/simulator/ecm_mod.js
EDIT:
Output of the command as asked by #Cyrus below:
Posting this this as Community Wiki answer for better visibility. Solution has been provided in comments by #Cyrus.
In Short, OP wanted to Kill/interrupt some process using their PID's. OP wanted to do it from cluster level on specific pod/container which included ecm simulator.
To do it, commands below were used:
exec - execute a command in a container
-- bash - run bash inside container
ps -ef - list all process on the system
grep - serch specific pattern
awk - pattern scanning and processing language.
xargs - build and execute command lines from standard input
kill - send a signal to a process
In MANUAL you can find some information about ps flags:
To see every process on the system using standard syntax:
ps -e
ps -ef
ps -eF
ps -ely
however each flag will still give another output, like below:
-e
PID TTY TIME CMD
-ef
UID PID PPID C STIME TTY TIME CMD
Cyrus advised to use following command:
kubectl exec eric-service-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "pgrep -f 'node.*ecm'"
bash -c - If the -c option is present, then commands are read from the first non-option argument command_string.
Also explain in comment:
pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. From man pgrep. node.*ecm is a regex.

Bash script works locally but not on a server

This Bash script is called from a SlackBot using a Python script that uses the paramiko library. This works perfectly when I run the script locally from the bot. It runs the application and then kills the process after the allotted time. But when I run this same script on a server from the SlackBot, it doesn't kill the process. Any suggestions?? The script name is "runslack.sh" which is what is called with the grep command below.
#!/bin/bash
slack-export-viewer -z "file.zip"
sleep 10
ps aux | grep runslack.sh | awk '{print $2}' | xargs kill
Please try this and let me know if this helps you
ps -ef | grep runslack.sh | grep -v grep | awk '{print $2}' |xargs -i kill -9 {}
and i hope your user has sufficient permission to execute/kill this action on your server environment.

.bashrc saves previous process id and does not update in alias commands

I have made an alias in .bashrc to kill my python service.py & process
alias servicestop="kill $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}')"
Whenever I run first time servicestop command it will kill the process.
but again whenever I start process python service.py &, and execute command servicestop it gives an error.
After research, I found following things.
when I run first time python service.py & process. its process id was 512.
and, command servicestop kill that process(512).
Now when I run Second time process python service.py &. its process id was 546.(definitely it will be different).
When I run command servicestop. it will give following error:
-bash: kill: (512) - No such process
That means $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}') will return the previous pid, which is already killed.
Now please suggest the solution if any possible.
so whenever I want to run servicestop command, I have to run source .bashrc command first, then run servicestop command to make it work.
Please remove the servicestop alias from your .bashrc and add :
servicestop(){
kill $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}');
}
In a way, functions in .bashrc are "aliases 2.0" : simply better
Better : same function; but with the name of script to kill as parameter :
servicestop(){
kill $(ps -ef | grep -w $1 | grep -v servicestop | awk '{print $2}');
}
Use it like that :
servicestop service.py
servicestop otherSuperService.py

How do I get "awk" to work correctly within a "su -c" command?

I'm running a script at the end of a Jenkins build to restart Tomcat. Tomcat's shutdown.sh script is widely known not to work all in many instances and so my script is supposed to capture the PID of the Tomcat process and then attempt to manually shut it down. Here is the command I'm using to capture the PID:
ps -ef | grep Bootstrap | grep -v grep | awk '{print $2}' > tomcat.pid
The output when manually runs retrieves the PID perfectly. During the Jenkins build I have to switch users to run the command. I'm using "su user -c 'commands'" like this:
su user -c "ps -ef | grep Bootstrap | grep -v grep | awk '{print $2}' > tomcat.pid"
Whenever I do this however, the "awk" portion doesn't seem to be working. Instead of just retrieving the PID, it's capturing the entire process information. Why is this? How can I fix the command?
The issue is that $2 is being processed by the original shell before being sent to the new user. Since the value of $2 in the shell is blank, the awk command at the target shell essentially becomes awk {print }. To fix it, you just escape the $2:
su user -c "pushd $TOMCAT_HOME;ps -ef | grep Bootstrap | grep -v grep | awk '{print \$2}' > $TOMCAT_HOME/bin/tomcat.pid"
Note that you want the $TOMCAT_HOME to be processed by the original shell so that it's value is set properly.
You don't need the pushd command as you can replace the awk command with:
cut -d\ -f2
Note: two 2 spaces between -d\ and -f2

Resources