Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make the chef-client (v0.10+) daemon process wake and run, and print what its doing to stdout
#!/bin/bash
#
# Author:: Ash Berlin (ash_github@firemirror.om)
# Copyright:: Copyright (c) 2011 DigiResults Ltd.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e
# Typically /var/run/chef/client.pid (init.d) or /etc/sv/chef-client/supervise/pid (runit)
pid_file="/var/run/chef/client.pid"
log_file="/var/log/chef/client.log"
declare tail_pid
on_exit() {
rm -f $pipe
[ -n "$tail_pid" ] && kill $tail_pid
}
trap "on_exit" EXIT ERR
pipe=/tmp/pipe-$$
mkfifo $pipe
tail -fn0 "$log_file" > $pipe &
tail_pid=$!
sudo kill -USR1 $(cat "$pid_file")
sed -r '/(ERROR: Sleeping for [[:digit:]]+ seconds before trying again|INFO: Report handlers complete)$/{q}' $pipe
@ashb

This comment has been minimized.

Copy link
Owner Author

@ashb ashb commented Oct 30, 2011

Since chef v0.10 you can send USR1 to the chef-client process and it will wake up and do a run.

But the usual case when I want to do a run is cos I'm either testing a cookbook change or I want to deploy now. I could just run sudo chef-client but then that will only log to std out.

Just run this script, it will send chef-client a USR1 signal and then tail the log file (but nicely so that you'll get your terminal back when the run completes).

@mrflip

This comment has been minimized.

Copy link

@mrflip mrflip commented Oct 30, 2011

patched to suggest where runit users should look for the pid: https://gist.github.com/1326206

also added to cluster chef -- https://github.com/infochimps/cluster_chef/blob/version_3/lib/chef/knife/cluster_kick.rb

Thanks @ashb!

@ashb

This comment has been minimized.

Copy link
Owner Author

@ashb ashb commented Nov 9, 2011

Thanks - merged those two fixes in.

@fooforge

This comment has been minimized.

Copy link

@fooforge fooforge commented Jun 29, 2012

Patched the script to first test whether a pid file acutally exists before calling USR1 on the chef-client: https://gist.github.com/3016799.
Thanks from my side as well, @ashb!

@mrflip

This comment has been minimized.

Copy link

@mrflip mrflip commented Jul 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment