EzDevInfo.com

init.d interview questions

Top init.d frequently asked interview questions

Restart Node.js App with init.d

I'd like to have an init.d daemon restart my node.js app if it crashes. This script starts/stops my node app. I've had no luck getting it to restart the app if it crashes.

I'm running under CentOS. What am I missing?

#!/bin/sh
. /etc/rc.d/init.d/functions

USER="rmlxadmin"
DAEMON="/usr/bin/nodejs"
ROOT_DIR="/home/rmlxadmin"

SERVER="$ROOT_DIR/my_node_app.js"
LOG_FILE="$ROOT_DIR/app.js.log"

LOCK_FILE="/var/lock/subsys/node-server"

do_start()
{
        if [ ! -f "$LOCK_FILE" ] ; then
                echo -n $"Starting $SERVER: "
                runuser -l "$USER" -c "$DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure
                RETVAL=$?
                echo
                [ $RETVAL -eq 0 ] && touch $LOCK_FILE
        else
                echo "$SERVER is locked."
                RETVAL=1
        fi
}
do_stop()
{
        echo -n $"Stopping $SERVER: "
        pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
        kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}

case "$1" in
        start)
                do_start
                ;;
        stop)
                do_stop
                ;;
        restart)
                do_stop
                do_start
                ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
                RETVAL=1
esac
exit $RETVAL

Source: (StackOverflow)

Run Qt application on startup as Linux daemon

I've developed a Qt application which contains a TCP server and such. I'm now trying to make Ubuntu packages and let the application automatically start on startup.

The application needs to be running even if nobody is logged in, which means a daemon started via a script in /etc/init.d/

I tried simply running the application on start and sending a kill-signal on stop in the init.d script but that means the application runs in the foreground and blocks the init-script.

Forking like in an other question almost seems to work, I get 'unknown error' after trying to start a TCP server. Nevertheless, there should be an easy to way to write a init-script that runs my application in the background on startup on the various Linux distributions.

Could anyone point me in the right direction?

Using Ubuntu 9.10 with Qt 4.5


Source: (StackOverflow)

Advertisements

update-rc.d and init.d dependencies

So I'm trying to write some init.d scripts satisfying LSB so they run appropriately at startup and shutdown. Unfortunately, I'm having issues with LSB/update-rc.d to satisfy dependencies.

# Required-Start: $network $local_fs hadoop-namenode hadoop-datanode zookeeper-server
# Required-Stop:  $network $local_fs hadoop-namenode hadoop-datanode zookeeper-server
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6

however, when I run update-rc.d defaults, it merely generates the defaults involved, with the exact same start time as the already dependencies, which breaks things horribly. Am I missing something with update-rc.d or LSB to get this configured?

In case it's relevant, this is Ubuntu 12.04


Source: (StackOverflow)

Mongo mongod init.d script not working on CentOS

I am trying to figure out why the provided init.d script is not working on CentOS. I tried starting it manually:

/etc/init.d/mongod start

But I get the following error:

Starting mongod: /usr/bin/dirname: extra operand `2>&1.pid'
Try `/usr/bin/dirname --help' for more information.

I looked in the script where it tries to start:

  daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"

So I looked where mongod var is defined:

mongod=${MONGOD-/usr/bin/mongod}

Also tried:

service mongod start

Same error.

Not sure what I have setup wrong, but I have verified that I have the latest script but I cannot get mongod process to start.

Any ideas???


Source: (StackOverflow)

Add nginx as a ubuntu service stop and reload doesn't work

My nginx was compile from the source, only give the flag that --conf-path=/etc/nginx/nginx.conf

Everything works and I was trying to use this service init.d script to make nginx as a system service,

Here's the script that I made only 2 minor changes:

1. DAEMON=/usr/local/nginx/sbin/nginx

2.NGINX_CONF_FILE="/etc/nginx/nginx.conf"

then sudo service nginx start

it works

yozloy@SNDA-172-17-10-158:/usr/local$ sudo service nginx start
 * Starting Nginx Server...
   ...done

.

but service nginx stop and service nginx reload

give me a error

yozloy@SNDA-172-17-10-158:/usr/local$ sudo service nginx stop
 * Stopping Nginx Server...
   ...fail!

and the error doesn't appear on the log/error.log file


Source: (StackOverflow)

Nginx daemon stop is failing

I've got Ubuntu 11.04 i386 server with nginx 1.0.11 installed. Also, I'm using this init.d script, the only one I've found in several different places. It starts the server nicely, however, on stop/reset it says

* Stopping Nginx Server...      [fail]

Of course, the daemon is not stopped, and upon restart the configuration is not reloaded.

How can I repair this?


Source: (StackOverflow)

Add additional parameters to Spring Boot app

I am wondering if it's possible to add spring's additional parameters such as -Dspring.profiles.active=prod to spring boot app in case of running it as a service.

I checked the script that was generated automatically by spring-boot-maven-plugin:

command="$javaexe -jar -Dsun.misc.URLClassPath.disableJarChecking=true $jarfile $@"

so maybe it can be done via maven plugin's options, but couldn't find any except of JVM arguments which is not so useful...


Source: (StackOverflow)

Special considerations for using Python in init.d script?

Are there any special considerations for using Python in an 'init.d' script being run through init? (i.e. booting Ubuntu)

From what I understand through googling/testing on Ubuntu, the environment variables provided to an 'init.d' script are scarce and so using "#!/usr/bin/env python" might not work.

Anything else?


Source: (StackOverflow)

God won't register a running custom service

I've got a rails website and a small minecraft server running on a linode vps. I'm running minecraft as a custom service off of a ram server based on an init.d file. Since I'm using God to monitor my rails website I thought I'd use it for minecraft as well, but it doesn't seem to be able to recognize the service in any way. The conditions don't detect its presence. :process_running always returns false, whether it's running or not, and fail to start it when it isn't. To add to the confusion :memory_usage and :cpu_usage are always zero.

My /etc/init.d/minecraft file is here: http://pastie.org/2760483

It works perfectly well, and 'service minecraft start' and whatnot gives me pretty much everything I need. My hope was to be able to put it to sleep it automatically through god whenever the cpu usage got high to prioritize the website. However none of the god conditions are figuring out what's going on with the process.

My /opt/god/minecraft.god file is here: http://pastie.org/2760498

Obviously the low cpu in that is an attempt to get a rise out of god. Asking for a smiting, if you will.

Trying to run god off the config: sudo god -c minecraft.god -D

yields:

I [2011-10-26 01:55:55]  INFO: Loading minecraft.god
I [2011-10-26 01:55:55]  INFO: Syslog enabled.
I [2011-10-26 01:55:55]  INFO: Using pid file directory: /var/run/god
I [2011-10-26 01:55:55]  INFO: Socket already in use
I [2011-10-26 01:55:55]  INFO: Socket is stale, reopening
I [2011-10-26 01:55:55]  INFO: Started on drbunix:///tmp/god.17165.sock
I [2011-10-26 01:55:55]  INFO: minecraft move 'unmonitored' to 'up'
I [2011-10-26 01:55:55]  INFO: minecraft moved 'unmonitored' to 'up'
I [2011-10-26 01:55:55]  INFO: minecraft [ok] memory within bounds [0kb] (MemoryUsage)
I [2011-10-26 01:55:55]  INFO: minecraft [ok] cpu within bounds [0%%] (CpuUsage)

Source: (StackOverflow)

Call to daemon in a /etc/init.d script is blocking, not running in background

I have a Perl script that I want to daemonize. Basically this perl script will read a directory every 30 seconds, read the files that it finds and then process the data. To keep it simple here consider the following Perl script (called synpipe_server, there is a symbolic link of this script in /usr/sbin/) :

#!/usr/bin/perl
use strict;
use warnings;

my $continue = 1;
$SIG{'TERM'}  = sub { $continue = 0; print "Caught TERM signal\n"; };
$SIG{'INT'} = sub { $continue = 0; print "Caught INT signal\n"; };

my $i = 0;
while ($continue) {
     #do stuff
     print "Hello, I am running " . ++$i . "\n";
     sleep 3;
}

So this script basically prints something every 3 seconds.

Then, as I want to daemonize this script, I've also put this bash script (also called synpipe_server) in /etc/init.d/ :

#!/bin/bash
# synpipe_server : This starts and stops synpipe_server
#
# chkconfig: 12345 12 88
# description: Monitors all production pipelines
# processname: synpipe_server
# pidfile: /var/run/synpipe_server.pid
# Source function library.
. /etc/rc.d/init.d/functions

pname="synpipe_server"
exe="/usr/sbin/synpipe_server"
pidfile="/var/run/${pname}.pid"
lockfile="/var/lock/subsys/${pname}"

[ -x $exe ] || exit 0

RETVAL=0

start() {
    echo -n "Starting $pname : "
    daemon ${exe}
    RETVAL=$?
    PID=$!
    echo
    [ $RETVAL -eq 0 ] && touch ${lockfile}
    echo $PID > ${pidfile}
}

stop() {
    echo -n "Shutting down $pname : "
    killproc ${exe}
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f ${lockfile}
        rm -f ${pidfile}
    fi
}

restart() {
    echo -n "Restarting $pname : "
    stop
    sleep 2
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status ${pname}
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
    ;; esac

exit 0

So, (if I have well understood the doc for daemon) the Perl script should run in the background and the output should be redirected to /dev/null if I execute :

service synpipe_server start

But here is what I get instead :

[root@master init.d]# service synpipe_server start
Starting synpipe_server : Hello, I am running 1
Hello, I am running 2
Hello, I am running 3
Hello, I am running 4
Caught INT signal
                                                           [  OK  ]
[root@master init.d]# 

So it starts the Perl script but runs it without detaching it from the current terminal session, and I can see the output printed in my console ... which is not really what I was expecting. Moreover, the PID file is empty (or with a line feed only, no pid returned by daemon).

Does anyone have any idea of what I am doing wrong ?

EDIT : maybe I should say that I am on a Red Hat machine.

Scientific Linux SL release 5.4 (Boron)

Thanks, Tony


Source: (StackOverflow)

Using ec2-init scripts with Ubuntu on EC2 - Automatically set hostname and register with Route53

I'd really like to be able to use the ec2-init scripts to do some housekeeping when I spin up an instance. Ideally I'd like to be able to pass user data to set the hostname and run a couple of initialization scripts (to configure puppet etc.).

I see a script called ec2-set-hostname but I'm not sure if you can use it to set an arbitrary hostname from user-data or what the format of the user-data would need to be.

Anyone used these scripts and know how if can set the hostname and run some scripts at the same time?

Thanks in advance.


Source: (StackOverflow)

What is start-stop-daemon in linux scripting?

What is start-stop-daemon and how should it be used?

I am trying to automate a particular program to run. Whenever the system starts, the program should run. For that I am writing script in "/etc/init.d/" location.


Source: (StackOverflow)

Spring Boot application as a Service

How to configure nicely Spring Boot application packaged as executable jar as a Service in linux system? Is this recomended approach, or should I convert this app to war and install into Tomcat?

Currently I can run Spring boot application from screen session, what is nice, but requires manual start after server reboot.

What I'm looking for is general advice/direction or sample init.d script, if my approach with executable jar is proper.


Source: (StackOverflow)

How can I log the stdout of a process started by start-stop-daemon?

I am using an init script to run a simple process, which is started with:

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
    --make-pidfile --pidfile $PIDFILE --background       \
    --exec $DAEMON $DAEMON_ARGS

The process called $DAEMON usually prints log information to its standard output. As far as I can tell this data is not being stored anywhere.

I would like to write or append the stdout of $DAEMON to a file somewhere.

The only solution I know is to tell start-stop-daemon to call a shellscript instead of $DAEMON directly; the script then calls $DAEMON and writes to the logfile. But that requires an extra script which, like modifying the daemon itself, seems the wrong way to solve such a common task.


Source: (StackOverflow)

Best practice to run Linux service as a different user

Services default to starting as root at boot time on my RHEL box. If I recall correctly, the same is true for other Linux distros which use the init scripts in /etc/init.d.

What do you think is the best way to instead have the processes run as a (static) user of my choosing?

The only method I'd arrived at was to use something like:

 su my_user -c 'daemon my_cmd &>/dev/null &'

But this seems a bit untidy...

Is there some bit of magic tucked away that provides an easy mechanism to automatically start services as other, non-root users?

EDIT: I should have said that the processes I'm starting in this instance are either Python scripts or Java programs. I'd rather not write a native wrapper around them, so unfortunately I'm unable to call setuid() as Black suggests.


Source: (StackOverflow)