Have process automatically restart with upstart and initctl - upstart

I'm on CentOS 6.3.
I've written a script in Python for the purpose of an app that I'm making. I want it to run as a process that will restart itself if it fails.
I have a .conf file like so:
start on runlevel [2345]
stop on runlevel [06]
script
/usr/bin/python /home/myself/myscript.py
end script
I can start it with initctl start myscript but when it fails, it doesn't restart. If I do initctl status myscript it says stop/waiting.

I seem to have solved this.
start on runlevel [2345]
stop on runlevel [06]
respawn
script
exec /usr/bin/python /home/myself/myscript.py
end script
I added respawn and placed exec before the command. According to this: http://newcome.wordpress.com/2012/02/26/running-programs-as-linux-daemons-using-upstart/ exec is an Upstart specific stanza instead of the normal exec command.

Related

docker CMD run supervisord in background

is there any way to run supervisord in the background. means start the process and get out of shell.
I have a docker file where i try to run a script that suppose to start the postgresql and then get out. so I have a process running and i can create users.
Docker command
CMD ["/runprocess.sh"]
script runproccess.sh
#!/bin/bash
supervisord -c "/etc/supervisord.conf"
I have also tried to run it in background, but no luck
#!/bin/bash
supervisord -c "/etc/supervisord.conf" &
supervisord starts the process and just stays on screen for ever.
i want it to run the process and get out. so I can run other part of my script.
you can remove setting nodaemon or set it to false in supervisord.conf
[supervisord]
nodaemon=false ; Docker利用ではtrueにする必要あり
this will make supervisor start in background.

How to call stop scripts on BusyBox shutdown?

I'm running BusyBox with an entry in /etc/inittab
::sysinit:/etc/init.d/rcS
The rcS script calls all the start scripts in /etc/rc.d/ on startup.
How is it possible to tell the BusyBox init to shut down all services probably by calling /etc/rc.d/xxx stop on calling the BusyBox applets "poweroff", "halt" or "reboot"?
Just for the records - I finally came along with adding my own shutdown script to /etc/inittab
::shutdown:/etc/init.d/rcD
The script just loops the startup scripts backwards:
#!/bin/sh
if [ -d /etc/rc.d ]; then
for x in $(ls -r /etc/rc.d/) ; do
/etc/rc.d/$x stop
done
fi

Upstart / init script not working

I'm trying to create a service / script to automatically start and controll my nodejs server, but it doesnt seem to work at all.
First of all, I used this source as main reference http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/
After testing around, I minimzed the content of the actual file to avoid any kind of error, resulting in this (the bare minimum, but it doesnt work)
description "server"
author "blah"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/var/www"
exec nodejs /var/www/server/server.js >> /var/log/node.log 2>&1
end script
The file is saved in /etc/init/server.conf
when trying to start the script (as root, or normal user), I get:
root#iof304:/etc/init# start server
start: Job failed to start
Then, I tried to check my syntax with init-checkconf, resulting in:
$ init-checkconf /etc/init/server.conf
File /etc/init/server.conf: syntax ok
I tried different other things, like initctl reload-configuration with no result.
What can I do? How can I get this to work? It can't be that hard, right?
This is what our typical startup script looks like. As you can see we're running our node processes as user nodejs. We're also using the pre-start script to make sure all of the log file directories and .tmp directories are created with the right permissions.
#!upstart
description "grabagadget node.js server"
author "Jeffrey Van Alstine"
start on started mysql
stop on shutdown
respawn
script
export HOME="/home/nodejs"
exec start-stop-daemon --start --chuid nodejs --make-pidfile --pidfile /var/run/nodejs/grabagadget.pid --startas /usr/bin/node -- /var/nodejs/grabagadget/app.js --environment production >> /var/log/nodejs/grabagadget.log 2>&1
end script
pre-start script
mkdir -p /var/log/nodejs
chown nodejs:root /var/log/nodejs
mkdir -p /var/run/nodejs
mkdir -p /var/nodejs/grabagadget/.tmp
# Git likes to reset permissions on this file, but it really needs to be writable on server start
chown nodejs:root /var/nodejs/grabagadget/views/layout.ejs
chown -R nodejs:root /var/nodejs/grabagadget/.tmp
# Date format same as (new Date()).toISOString() for consistency
sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/nodejs/grabagadget.log
end script
pre-stop script
rm /var/run/nodejs/grabagadget.pid
sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/nodejs/grabgadget.log
end script
As of Ubuntu 15, upstart is no longer being used, see systemd.

redhat linux upstart (initctl) issue on reboot

Morning,
I have monit I am testing on redhat 6.4 system. I setup a /etc/init/monit.conf:
description "Monit service manager"
limit core unlimited unlimited
start on runlevel [2345]
stop on runlevel [!2345]
expect daemon
respawn
exec /local/mis/monit/bin/monit -c /local/mis/monit/etc/monitrc
pre-stop exec /local/mis/monit/bin/monit -c /local/mis/monit/etc/monitrc quit
At the command line as root I can run stop monit and start monit just fine and it shows pid number. However, during reboot, it does not start. It shows start/running with no pid if I run initctl list, but if you check with ps -ef monit is not running. I can run stop monit and then run start monit just fine after a reboot. I am at a lose with how to troubleshoot. My system has /var/log/messages, but no /var/log/syslog. I see options to use log-priority info, but I am not sure how to set that as the level for logging during the reboot. The /var/log/message does not mention monit and /var/log/boot.log does not either. dmesg shows nothing.
this sounds like the same problem we just fixed.
We run monit as user "monit". upstart was trying to start it as root and the monit files where owned my user "monit". And we were getting identical symptoms you were getting.
To fix it i altered /etc/init/monit to
exec su -c "/web/bin/monit -c /web/etc/monitrc" monit
now when i start monitI see::
# start monit
monit start/running, process 3421
The final solution I ended up using: description "Monit service manager"
start on (net-device-up IFACE=eth0 and started networking and runlevel [2345])
stop on runlevel [!2345]
limit core unlimited unlimited
expect daemon
respawn
Had to do a pre-start script to loop until successful with nslookup of mail server listed in the monitrc file.
pre-start script
while [ 0 ]; do
i=/usr/bin/nslookup outlookwebapp.na.sas.com | grep Name
if [ ! -z "$i" ]; then
break
fi
sleep 4
done
end script
exec /local/mis/monit/bin/monit -c /local/mis/monit/etc/monitrc
pre-stop exec /local/mis/monit/bin/monit -c /local/mis/monit/etc/monitrc quit

upstart script for logstash, writing the pid file that deals with the fork

I am using the example upstart script for logstash but am having trouble writing a pid file for monit to use at /var/run/logstash.pid
When i use "echo $$ > /var/run/logstash.pid" it writes the wrong pid value to file, I think its often the value before a fork. Is there a solution to this?
# logstash - agent instance
#
description "logstash agent instance"
start on virtual-filesystems
stop on runlevel [06]
# Respawn it if the process exits
respawn
respawn limit 5 30
limit nofile 65550 65550
expect fork
# You need to chdir somewhere writable because logstash needs to unpack a few
# temporary files on startup.
chdir /home/logstash
script
# This runs logstash agent as the 'logstash' user
echo $$ > /var/run/logstash.pid
su -s /bin/sh -c 'exec "$0" "$#"' logstash -- /usr/bin/java -jar logstash.jar agent -f /etc/logstash/agent.conf --log /var/log/logstash.log &
emit logstash-agent-running
end script