Shell script to delete znodes(Zookeeper) - sh

i am trying to create a shell script which will delete my znodes. Here is the command:
echo "ls /" | zookeeper-client
echo "rmr /collections" | zookeeper-client
There are many such nodes i want to delete. Whenever i execute the above commands, sometimes it successfully delete the collections node and sometime it throws error. The reason i found is whenever we run "zookeeper-client" through a shell script, it takes time for the zookeeper shell to come up.
Is there any way i can delete such nodes ?
Any help will be appreciated.
Thanks.

You can delete with this
zookeeper-client rmr
e.g zookeeper-client rmr /test

The amount of hackery here is horrendous -- I'm having trouble retaining any self-respect after writing the below.
Please, please use a different language rather than adopting the below collection of awful hacks.
IFS=$'\n ' read -r -d '' -a names \
< <(./cli_st 127.0.0.1:2181 < <(sleep 1; echo 'ls /'; sleep 1) 2>&1 | grep '^[[:space:]]')
./cli_st 127.0.0.1:2181 < <(sleep 1; printf 'rmr %s\n' "${names[#]}"; sleep 1; )
cli_st, here, is the command-line client included in the Zookeeper codebase's C-language bits.

Related

How do I automate killing a job in cron? [duplicate]

This question already has answers here:
Find and kill a process in one line using bash and regex
(30 answers)
Closed 1 year ago.
Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:
jeremy#jeremy-desktop:~$ ps aux | grep firefox
jeremy 7451 25.0 27.4 170536 65680 ? Sl 22:39 1:18 /usr/lib/firefox-3.0.1/firefox
jeremy 7578 0.0 0.3 3004 768 pts/0 S+ 22:44 0:00 grep firefox
jeremy#jeremy-desktop:~$ kill 7451
What I'd like is a command that would do all that for me. It would take an input string and grep for it (or whatever) in the list of processes, and would kill all the processes in the output:
jeremy#jeremy-desktop:~$ killbyname firefox
I tried doing it in PHP but exec('ps aux') seems to only show processes that have been executed with exec() in the PHP script itself (so the only process it shows is itself.)
pkill firefox
More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm
Also possible to use:
pkill -f "Process name"
For me, it worked up perfectly. It was what I have been looking for.
pkill doesn't work with name without the flag.
When -f is set, the full command line is used for pattern matching.
You can kill processes by name with killall <name>
killall sends a signal to all
processes running any of the specified
commands. If no signal name is
specified, SIGTERM is sent.
Signals can be specified either by
name (e.g. -HUP or -SIGHUP ) or by number (e.g.
-1) or by option -s.
If the command name is not regular
expression (option -r) and contains a
slash (/), processes executing that
particular file will be selected for
killing, independent of their name.
But if you don't see the process with ps aux, you probably won't have the right to kill it ...
A bit longer alternative:
kill `pidof firefox`
The easiest way to do is first check you are getting right process IDs with:
pgrep -f [part_of_a_command]
If the result is as expected. Go with:
pkill -f [part_of_a_command]
If processes get stuck and are unable to accomplish the request you can use kill.
kill -9 $(pgrep -f [part_of_a_command])
If you want to be on the safe side and only terminate processes that you initially started add -u along with your username
pkill -f [part_of_a_command] -u [username]
Kill all processes having snippet in startup path. You can kill all apps started from some directory by for putting /directory/ as a snippet. This is quite usefull when you start several components for the same application from the same app directory.
ps ax | grep <snippet> | grep -v grep | awk '{print $1}' | xargs kill
* I would prefer pgrep if available
Strange, but I haven't seen the solution like this:
kill -9 `pidof firefox`
it can also kill multiple processes (multiple pids) like:
kill -9 `pgrep firefox`
I prefer pidof since it has single line output:
> pgrep firefox
6316
6565
> pidof firefox
6565 6316
Using killall command:
killall processname
Use -9 or -KILL to forcefully kill the program (the options are similar to the kill command).
On Mac I could not find the pgrep and pkill neither was killall working so wrote a simple one liner script:-
export pid=`ps | grep process_name | awk 'NR==1{print $1}' | cut -d' ' -f1`;kill $pid
If there's an easier way of doing this then please share.
To kill with grep:
kill -9 `pgrep myprocess`
more correct would be:
export pid=`ps aux | grep process_name | awk 'NR==1{print $2}' | cut -d' ' -f1`;kill -9 $pid
I normally use the killall command.
Check this link for details of this command.
I was asking myself the same question but the problem with the current answers is that they don't safe check the processes to be killed so... it could lead to terrible mistakes :)... especially if several processes matches the pattern.
As a disclaimer, I'm not a sh pro and there is certainly room for improvement.
So I wrote a little sh script :
#!/bin/sh
killables=$(ps aux | grep $1 | grep -v mykill | grep -v grep)
if [ ! "${killables}" = "" ]
then
echo "You are going to kill some process:"
echo "${killables}"
else
echo "No process with the pattern $1 found."
return
fi
echo -n "Is it ok?(Y/N)"
read input
if [ "$input" = "Y" ]
then
for pid in $(echo "${killables}" | awk '{print $2}')
do
echo killing $pid "..."
kill $pid
echo $pid killed
done
fi
kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')
If you run GNOME, you can use the system monitor (System->Administration->System Monitor) to kill processes as you would under Windows. KDE will have something similar.
The default kill command accepts command names as an alternative to PID. See kill (1). An often occurring trouble is that bash provides its own kill which accepts job numbers, like kill %1, but not command names. This hinders the default command. If the former functionality is more useful to you than the latter, you can disable the bash version by calling
enable -n kill
For more info see kill and enable entries in bash (1).
ps aux | grep processname | cut -d' ' -f7 | xargs kill -9 $
awk oneliner, which parses the header of ps output, so you don't need to care about column numbers (but column names). Support regex. For example, to kill all processes, which executable name (without path) contains word "firefox" try
ps -fe | awk 'NR==1{for (i=1; i<=NF; i++) {if ($i=="COMMAND") Ncmd=i; else if ($i=="PID") Npid=i} if (!Ncmd || !Npid) {print "wrong or no header" > "/dev/stderr"; exit} }$Ncmd~"/"name"$"{print "killing "$Ncmd" with PID " $Npid; system("kill "$Npid)}' name=.*firefox.*

Supervisord- Execute a command before starting the application / program

Using supervisord, how do I execute a command before running the program?
For example in the code below, I want a file to be created before starting the program. In the code below I am using tail -f /dev/null to simulate the background process but this could be any running program like '/path/to/application'.
I tried '&&' and this doesn't seem to work. The requirement is that the file has to be created first in order for the application to work.
[supervisord]
nodaemon=true
logfile=~/supervisord.log
[program:app]
command:touch ~a.c && tail -f /dev/null
The problem is that supervisor isn't running a shell to interpret command sections, so "&&" is just one of 5 space separated arguments it is passing to the touch command; if this ran successfully, then there should be some unusual filenames in its working directory now.
You can use a shell as your command and pass it the shell logic you would like:
command=/bin/sh -c "touch ~a.c && tail -f /dev/null"
Usually, this type of shell wrapper should be the interface provided and managed by the app and is what supervisord and others just know how to call with paths and options, i.e.:
command=myappswrapper.sh ~a.c
(where myappswrapper.sh is:)
#!/bin/sh
touch $1 && tail -f /dev/null
Here is a trick.
You use a shell script to do that and beyond that
[program:app]
command:sh /path/to/your/script.sh
It's can your script.sh
touch ~a.c
exec tail -f /dev/null
notice exec

Use a Chef recipe to modify a single line in a config file

I'm trying to automate disabling the Transparent Huge Pages (THP) Settings for MongoDB using a Chef Recipe.
The THP setting is explained here: MongoDocs THP Settings
I'm trying to follow the first option "In Boot-Time Configuration (Preferred)" by editing the grub configuration file at "/etc/grub.conf"
All I need to do is append "transparent_hugepage=never" to the end of the existing line that starts with "kernel "
I know I can replace a line with Chef::Util::FileEdit, using something like this:
ruby_block "replace_line" do
block do
file = Chef::Util::FileEdit.new("/etc/grub.conf")
file.search_file_replace_line("/kernel/", "kernel <kernel path> <kernel options> transparent_hugepage=never")
file.write_file
end
end
but I need to keep the existing kernel path and kernel options.
I've tried playing around with Chef::Util::Editor, but haven't been successful initializing the constructor. Chef::Util::FileEdit is initialized with a file path (per above), but the ruby docs say that Chef::Util::Editor is initialized with "lines". I've tried
lines = Chef::Util::Editor.new(<lines>)
where <lines> = file path, = Chef::Util::FileEdit.new(), and = 'test string', but nothing seems to work.
Does anyone have any experience with the Chef::Util::Editor? Or a better solution?
Thanks
I never figured out how to modify a single line in a config file using Chef, but here's the recipe I ended up using to disable THP settings for MongoDB.
Recipe: Install MongoDB
# Install MongoDB on Amazon Linux
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-amazon/
# 1: configure the package management system (yum)
# 2: install mongodb
# 3: configure mongodb settings
# 3.A: give mongod permission to files
# data & log directories (everything in /srv/mongodb)
# http://stackoverflow.com/questions/7948789/mongodb-mongod-complains-that-there-is-no-data-db-folder
execute "mongod_permission" do
command "sudo chown -R mongod:mongod /srv/mongodb"
#command "sudo chown mongod:mongod /var/run/mongodb/mongod.pid"
#command "sudo chown -R $USER /srv/mongodb"
end
# 3.B: edit Transparent Huge Pages (THP) Settings
# get rid of mongod startup warning
# http://docs.mongodb.org/manual/reference/transparent-huge-pages/#transparent-huge-pages-thp-settings
# 3.B.1: disable
execute "disable_thp_khugepaged_defrag" do
command "echo 0 | sudo tee /sys/kernel/mm/transparent_hugepage/khugepaged/defrag" # different b/c file doesn't have options list
end
execute "disable_thp_hugepage_defrag" do
command "echo 'never > /sys/kernel/mm/transparent_hugepage/defrag' | sudo tee --append /sys/kernel/mm/transparent_hugepage/defrag"
end
execute "disable_thp_hugepage_enables" do
command "echo 'never > /sys/kernel/mm/transparent_hugepage/enabled' | sudo tee --append /sys/kernel/mm/transparent_hugepage/enabled"
end
# 3.B.2: verify disabled on reboot
template "/etc/rc.local" do
source "init-rc.local.erb"
owner 'root'
group 'root'
mode '0775'
end
# 4: use upstart & monit to keep mongod alive
Template: init-rc.local.erb
touch /var/lock/subsys/local
if test -f /sys/kernel/mm/transparent_hugepage/khugepaged/defrag; then
echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
The problem with your own solution is the template can be overwritten by another recipe with it's own rc.local template.
To change that, I add the lines to the existing rc.local
execute "disable_thp_hugepage_defrag" do
command "sudo sed -i -e '$i \\echo never > /sys/kernel/mm/transparent_hugepage/defrag\\n' /etc/rc.local"
not_if 'grep -c "transparent_hugepage/defrag" /etc/rc.local'
end
execute "disable_thp_hugepage_enables" do
command "sudo sed -i -e '$i \\echo never > /sys/kernel/mm/transparent_hugepage/enabled\\n' /etc/rc.local"
not_if 'grep -c "transparent_hugepage/enabled" /etc/rc.local'
end
The grep makes sure that the line is not already in it.
Maybe chef has something better to manage that?
We can efficietly replace contents of file by grouping the elements
e.g.
appending "transparent_hugepage=never" to the end of the existing line that starts with "kernel "
ruby_block "replace_line" do
block do
file = Chef::Util::FileEdit.new("/etc/grub.conf")
file.search_file_replace_line(/kernel.*/, '\0 tansparent_hugepage=never')
file.write_file
end
end
\0 adds whole mached string
note: ' '(single quote)
I disabled hugepages by replicating the following in chef (looks the same as above but with the addition of a not_if statement):
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
E.G
execute "disable_hugepage_defrag" do
not_if "grep -F '[never]' /sys/kernel/mm/transparent_hugepage/defrag"
command "echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
end
I have also had success inserting lines with file.insert_line_if_no_match the ruby line replace feature will probably work for you.
search_file_replace_line(regex, newline) ⇒ Object
ruby_block 'replace_line' do
block do
file = Chef::Util::FileEdit.new('/path/to/file')
file.search_file_replace_line('/Line to find/', 'Line to replace with')
file.write_file
end
end

Perl - What is the return value of perl system function [duplicate]

I've written a shell script to soft-restart HAProxy (reverse proxy). Executing the script from the shell works. But I want a daemon to execute the script. That doesn't work. system() returns 256. I have no clue what that might mean.
#!/bin/sh
# save previous state
mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.old
mv /var/run/haproxy.pid /var/run/haproxy.pid.old
cp /tmp/haproxy.cfg.new /home/haproxy/haproxy.cfg
kill -TTOU $(cat /var/run/haproxy.pid.old)
if haproxy -p /var/run/haproxy.pid -f /home/haproxy/haproxy.cfg; then
kill -USR1 $(cat /var/run/haproxy.pid.old)
rm -f /var/run/haproxy.pid.old
exit 1
else
kill -TTIN $(cat /var/run/haproxy.pid.old)
rm -f /var/run/haproxy.pid
mv /var/run/haproxy.pid.old /var/run/haproxy.pid
mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.err
mv /home/haproxy/haproxy.cfg.old /home/haproxy/haproxy.cfg
exit 0
fi
HAProxy is executed with user haproxy. My daemon has it's own user too. Both run with sudo.
Any hints?
According to this and that, Perl's system() returns exit values multiplied by 256. So it's actually exiting with 1. It seems this happens in C too.
Unless system returns -1 its return value is of the same format as the status value from the wait family of system calls (man 2 wait). There are macros to help you interpret this status:
man 3 wait
Lists these macros and what they tell you.
A code of 256 probably means that the system command cannot locate the binary to run it. Remember that it may not be calling bash and that it may not have paths setup. Try again with full paths to the binaries!
I have the same problem when call script that contains `kill' command in a daemon.
The daemon must have closed the stdout, stderr...
Use something like system("scrips.sh > /dev/null") should work.

How to find the command line of a process only if the process is from current user

I have the following situation:
A perl script read a file where a application wrote it's pid, and tries to kill it.
But the problem is that I don't want to kill another process so I check if current process with the recorded PID has the same command line. If so, the application could be killed.
The following blues script find out the cmdline:
$PIDCMDLINE = `ps -p $PID -o cmd`;
The problem is that if another instance for another user is up, maybe on the same sid, it would be killed because it will return a valid command line, and I don't want that behaviour.
How can I restrict ps -p to search only current users processes (no, simple ps doesn't count, because -p nullify the default effect of ps)
Thank you!
You can use the following to check both command and user for the certain PID:
ps -p <PID> -o user,cmd --columns 1000 | grep `whoami`
Adding a 'grep' according to the comment.
May be a little awkward, but what about this:
$PIDCMDLINE = ps -p $PID -o user,command | grep `whoami` | awk '{ print $2 }'