Daemonize php script using upstart

Standard

What is upstart? If you google the term, what you get on the first of the list is a fund crowdsourcing site, but I am not into that. What I am into is the second search result: event based init daemon

According to Wikipedia, Upstart is a replacement for traditional init daemon. In order words, the daemon in /etc/init.d are started upon server start and strictly synchronous, whereas Upstart is asynchronous.

Upstart is available in all forms of linux, which means you can daemonise php scripts on any Linux favour platforms. So depending on what distro you are using, installing upstart for Ubuntu is as simple as:

sudo apt-get install upstart

Yup it is that simple. Then you create a yourprogram.conf file                                                                                 

description “yourprogram”

author      “Moses Liao Gangzheng”

# used to be: start on startup

# until we found some mounts weren’t ready yet while booting:

start on started mountall

stop on shutdown

# Automatically Respawn:

respawn

respawn limit 99 5

script

export HOME=”/root”

exec /usr/bin/php /var/www/RistoConnect/cron/CampaignUpdater.php >> /mnt/log/Campaign.log 2>&1

end script

post-start script

# Optionally put a script here that will notifiy you node has (re)started

# /root/bin/hoptoad.sh “node.js has started!”

end script

So which script is being daemonise? No prizes for the right answer it is CampaignUpdater.php at /var/www/RistoConnect/cron. That is where you put the php script you want to daemonise using upstart. After saving the file as CampaignUpdater.conf, you can start the daemon by running:

start yourprogram

or stop the daemon by running on console:

stop yourprogram

Yeah, its that simple. Just a few steps you have demonised a php script, without going through the trouble of writing bash scripts which you may not be familiar with.

For more information about Upstart, visit their site http://upstart.ubuntu.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s