Upon running a RightScript that attempts to start a background task, you may notice that the script/recipe never finishes and execution hangs, stalling the boot process. This is due to the way that our Chef/Rightscript provider handles detaching from a subprocess and applies to v5 RightImages.
Note: This is for v5 Rightimages ONLY! If you are still using a v4 RightImage (now deprecated) this technique is not needed.
Instead of running your script as such:
#!/bin/bash /path/to/your/process &
Try using this 'daemonize' script that will properly launch a task in the background, as such:
cat <<"EOF" >/usr/bin/daemonize #!/bin/sh cd / ( exec "$@" <&- >&- 2>&- )& EOF chmod +x /usr/local/bin/daemonize daemonize <your_process>
Be sure to replace the last <your_process> with your process/task name that you'd like to start in the background. This will properly launch the service in the background in a method that is friendly to our RightScript provider.
This is useful when a RightScript attachment is intended to run backgrounded (detached from RightLink) and its output logged to a local file.
Import Run attachment in background from the MultiCloud Marketplace for use as a RightScript in your ServerTemplate or as an Any Script/Operational Script.
In RightScripts where a package is installed and the package installation (e.g. via postinst or %post) launches a daemon, you can use the following example RightScript. The script also caters for the situation where a configuration is changed and the service needs to be restarted (Chef handles this with notifications, which is a better practice).
#!/bin/bash -e # for verification on the instance, use: # tail -f /var/log/messages /var/log/jenkins-install.log log_file="/var/log/jenkins-install.log" # log function, accepts stdin # redirects direct to the log can pipe to this function, but PIPESTATUS needs to be checked for exit code log () { echo "$*" printf "%s %s\n" "$(date)" "$*" >> "$log_file" logger -t RightScale-debug "$*" } touch "$log_file" :> "$log_file" # Set up the apt repo wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | apt-key add - echo "deb http://pkg.jenkins-ci.org/debian binary/" > /etc/apt/sources.list.d/jenkins.list if dpkg -l jenkins >> "$log_file" 2>&1; then if /etc/init.d/jenkins status | grep 'is running'; then log 'Stopping the jenkins service.' exec $(/etc/init.d/jenkins stop <&- >&- 2>&- )& fi fi # Install the packages apt-get -y update # detach and background sub-process exec $($(apt-get -y install --reinstall jenkins >> "$log_file" 2>&1) <&- >&- 2>&- )& # race condition here due to backgrounding # this will loop forever if the package install failed and the service didn't start log 'Waiting for jenkins service...' while [ 1 ]; do if file /etc/init.d/jenkins >> "$log_file" 2>&1 && /etc/init.d/jenkins status | grep 'is running'; then break; else echo '.' fi sleep 1 done log 'Setting up /etc/default/jenkins.' # Config jenkins to listen on the ip echo 'JAVA_ARGS="-Djava.net.preferIPv4Stack=true" # make jenkins listen on IPv4 address' >> /etc/default/jenkins # restart the daemon log 'Restarting the jenkins service.' exec $(/etc/init.d/jenkins restart <&- >&- 2>&- )& # optional service check after a sleep (to allow the detached restart to complete) sleep 5 /etc/init.d/jenkins status log "== Install log ($log_file) ==" cat "$log_file" log "===="
© 2006-2014 RightScale, Inc. All rights reserved.
RightScale is a registered trademark of RightScale, Inc. All other products and services may be trademarks or servicemarks of their respective owners.