Home > FAQs > Why does my RightScript not finish when attempting to launch a background process?

MindTouch
Copyright (c) 2006-2014 MindTouch Inc.
http://mindtouch.com

This file and accompanying files are licensed under the MindTouch Master Subscription Agreement (MSA).

At any time, you shall not, directly or indirectly: (i) sublicense, resell, rent, lease, distribute, market, commercialize or otherwise transfer rights or usage to: (a) the Software, (b) any modified version or derivative work of the Software created by you or for you, or (c) MindTouch Open Source (which includes all non-supported versions of MindTouch-developed software), for any purpose including timesharing or service bureau purposes; (ii) remove or alter any copyright, trademark or proprietary notice in the Software; (iii) transfer, use or export the Software in violation of any applicable laws or regulations of any government or governmental agency; (iv) use or run on any of your hardware, or have deployed for use, any production version of MindTouch Open Source; (v) use any of the Support Services, Error corrections, Updates or Upgrades, for the MindTouch Open Source software or for any Server for which Support Services are not then purchased as provided hereunder; or (vi) reverse engineer, decompile or modify any encrypted or encoded portion of the Software.

A complete copy of the MSA is available at http://www.mindtouch.com/msa

Why does my RightScript not finish when attempting to launch a background process?

Background Information

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.

Answer

Daemonize wrapper

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.

RightScript: Run attachment in background

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.

Dealing with packages that launch a daemon

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).

Example: Install Jenkins CI Server w/ config change
#!/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 "===="
You must to post a comment.
Last Modified
21:27, 16 May 2013

Tags

Classifications

This page has no classifications.

Announcements

None

Glossary | 用語용어 Site Map | Site Help Community Corporate Site Get Support Dashboard Login
Doc Feedback Product Feedback Resources MultiCloud Marketplace Forums

Dashboard Status


© 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.