How to Make Sure an Application Keeps Running on Linux

How to make sure an application keeps running on Linux

Notice: Upstart is in maintenance mode and was abandoned by Ubuntu which uses systemd. One should check the systemd' manual for details how to write service definition.

Since you're using Ubuntu, you may be interested in Upstart, which has replaced the traditional sysV init. One key feature is that it can restart a service if it dies unexpectedly. Fedora has moved to upstart, and Debian is in experimental, so it may be worth looking into.

This may be overkill for this situation though, as a cron script will take 2 minutes to implement.

#!/bin/bash
if [[ ! `pidof -s yourapp` ]]; then
invoke-rc.d yourapp start
fi

how to keep Go webservice running

The short answer: use the system service manager if you want to keep things super-simple. CentOS currently uses Upstart, and it's well documented and can handle most Go applications without too many problems. There are some good examples of Upstart + Go here and here

The long answer: personal preference. Supervisord, Monit and Circus are good options as well, but bring differing levels of complexity. I personally like supervisord, since it has a fairly clear syntax and a good heap of options.

There's also a good run-down here: http://tech.cueup.com/blog/2013/03/08/running-daemons/

How do I make sure my bash script isn't already running?

# Use a lockfile containing the pid of the running process
# If script crashes and leaves lockfile around, it will have a different pid so
# will not prevent script running again.
#
lf=/tmp/pidLockFile
# create empty lock file if none exists
cat /dev/null >> $lf
read lastPID < $lf
# if lastPID is not null and a process with that pid exists , exit
[ ! -z "$lastPID" -a -d /proc/$lastPID ] && exit
echo not running
# save my pid in the lock file
echo $$ > $lf
# sleep just to make testing easier
sleep 5

There is at least one race condition in this script. Don't use it for a life support system, lol. But it should work fine for your example, because your environment doesn't start two scripts simultaneously. There are lots of ways to use more atomic locks, but they generally depend on having a particular thing optionally installed, or work differently on NFS, etc...

How to ensure only one copy of the application is running?

The easiest way is to bind to a port (could be unix domain, in a "private" directory) Only one process can bind to a port, so if the port is bound, the process is running. If the process exits, the kernel automatically closes the filedescriptor. It does cost your process a (unused?) filedescriptor. Normally a daemon process would need some listen socket anyway.

Why use nohup when the app can be run as system service?

nohup is quick, easy, doesn't require root access and doesn't make permanent changes in the system. That's why many people use it (or try to use it) instead of configuring services.

Running things in the background without any supervision is usually a bad idea, although there are many legitimate use cases which don't fit traditional service model. For example:

  1. Background process might be needed only sometimes, after certain user actions.
  2. More than one instance might be needed. For example: one per user or one per session.
  3. Process might not need to to be running all the time. It just quits after doing its job.

Some real world examples (which use something like nohup and would be hard to implement as system services):

  1. git will sometimes run git gc in background to optimize repository without blocking user work
  2. adb will start its service in background and keep it running until user asks to terminate it
  3. Some compilers have option to keep running in the background to reduce startup times of subsequent invocations

Exit from bash script but keep the process running

Run that process in background:

#!/bin/sh
(php /home/stjc/app/artisan queue:listen --timeout=60 --tries=5) &

try adding an ampersand(&) at the end with brackets on either side of original command.

Edit:

: is a shell builtin which means NOP depending on your OS it might a problem try escaping the it in the php command and see if it works for you

#!/bin/sh
(php /home/stjc/app/artisan queue\:listen --timeout=60 --tries=5) &

also puting the full path to your php interpreter is strongly advised.



Related Topics



Leave a reply



Submit