init.d script won't rerun python script on error - shell

I am new to shell scripting and am having a bit of an issue when it comes to rerunning my script. My script is located here /etc/init.d/receiver and when I use the following command sudo systemctl start receiver, I get the following error,
Job for receiver.service failed because the control process exited with error code.
See "systemctl status receiver.service" and "journalctl -xe" for details.
To test out the "retry" on crash, I purposely made a line in my python script that is being called to crash the program. That line is someError = 'error. Otherwise, the shell script which calls the python script runs with no issue but the whole point of this init.d shell script is to rerun the py script in case of an error and to start at startup. Here is my code,
#!bin/sh
### BEGIN INIT INFO
# Provides: Test_receiver.py
# Reqired-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 3 4 5 6
# Default-Stop: 0
# Short-Description: Rabbit consumer script Test_receiver.py
### END INIT INFO
set -e
# /etc/init.d/startup: start and stop the Test_receiver.py script
# no file /etc/default/startup for added config at this time
. /lib/lsb/init-functions
cd /home/cschoom/Security/src
run_receiver(){
log_action_begin_msg "Starting Test_receiver.py"
log_file=/home/cschuma1/otherfile.txt
python Test_receiver.py 2>> $log_file
retval=$?
if [ $retval == "0" ]; then
echo "success"
log_action_cont_msg "No issues"
else
echo "failure" >> $log_file
log_failure_msg "ERROR WITH TEST_RECEIVER `date`"
sleep 5
run_receiver
fi
}
case "$1" in
start)
run_receiver
;;
stop)
echo "killing Test_receiver script"
log_action_begin_msg "Killing Test_receiver.py"
kill $(pgrep -f 'python Test_receiver.py')
;;
restart|force-reload)
log_action_begin_msg "Reloading Peach_receiver.py"
run_receiver
;;
*)
echo "Usage /etc/init.d/receiver (start|stop|restart|force-reload)"
exit 1
;;
esac
exit 0
`
All I am trying to do is run Test_receiver.py, grab the return value, and based off that return value, either rerun the run_receiver function which calls my python script again or exit with 0 return code. This is a rabbitmq consumer script which will just run endlessly so I would like to rerun the python script as the fault will likely not be with the script but instead with what is being tested. (my python script runs tests, like nmap, on other machines). Any help would be appreciated. Thanks

Related

run solr like daemon

I try to make solr to run as a startup script in /etc/init.d/solr.
This is script that I copypasted from How to start Solr automatically?
#!/bin/sh
# Prerequisites:
# 1. Solr needs to be installed at /usr/local/solr/example
# 2. daemon needs to be installed
# 3. Script needs to be executed by root
# This script will launch Solr in a mode that will automatically respawn if it
# crashes. Output will be sent to /var/log/solr/solr.log. A PID file will be
# created in the standard location.
# Comments to support chkconfig on Red Hat Linux
# chkconfig: 2345 64 36
# Description: A very fast and reliable search engine.
# processname solr
# Source function library.
. /etc/init.d/functions
start () {
echo -n "Starting solr..."
# start daemon
daemon --chdir='/usr/local/solr/example' --command "java -jar start.jar" --respawn --output=/var/log/solr/solr.log --name=solr --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
stop () {
# stop daemon
echo -n "Stopping solr..."
daemon --stop --name=solr --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
restart () {
daemon --restart --name=solr --verbose
}
status () {
# report on the status of the daemon
daemon --running --verbose --name=solr
return $?
}
case "$1" in
start)
start
;;
status)
status
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: solr {start|status|stop|restart}"
exit 3
;;
esac
exit $RETVAL
I did everything as described in above link. But get an error
service solr start
Starting solr.../etc/init.d/solr: Usage: daemon [+/-nicelevel] {program}
failed. See error code for more information.
reading https://blog.hazrulnizam.com/create-init-script-centos-6/ I don't understand why daemon was written incorrect
This isn't working because the daemon function (from /etc/init.d/functions) has changed since 2010 (when the script was posted) and no longer accepts the same arguments. You will need to rewrite the daemon line to accept the currently supported arguments.
I had a look at the daemon function on a CentOS 6 box, and it looks like you might be able to replace this line:
daemon --chdir='/usr/local/solr/example' --command "java -jar start.jar" --respawn --output=/var/log/solr/solr.log --name=solr --verbose
with just this:
daemon "java -jar /usr/local/solr/example/start.jar"
(assuming that solr is installed in /usr/local/solr/example).

shell script for startup/shutdown of jboss7 fails with "Unexpected end of file" Syntax error

As per a website, I have created a simple shell script at--> /etc/init.d/jboss
This shell script is to startup/shutdown JBoss AS 7.1.1 in Ubuntu 12.04 64-bit
But when I try and run the script I get an error "Syntax error: unexpected end of file"
and it mentions the last line number of the file.
I used vim to create the above file and the contents of the file are given below-- what have I done wrong here?
#Required-Start: $local_fs $remote_fs $network $syslog
#Required-Stop: $local_fs $remote_fs $network $syslog
#Default-Start: 2 3 4 5
#Default-Stop: 0 1 6
#Short-Description: JBOSS 7.1.1 Startup/Shutdown Script
### END INIT INFORMATION
case "$1" in
start)
echo "Starting JBOSS AS 7.1.1"
sudo -u root sh /usr/share/jboss-as-7.1.1/bin/standalone.sh
;;
stop)
echo "Stopping JBOSS AS 7.1.1"
sudo -u root sh /usr/share/jboss-as-7.1.1/bin/jboss-admin.sh --connect command=:shutdown
;;
*)
echo "Usage: /etc/init.d/jboss {start|stop}"
exit 1
;;
You're missing esac to balance case.

rc.d start does not terminate?

So I wrote the Arch Linux rc.d script for mongod daemon (following an example), but when I do:
sudo rc.d start mongod
it just gets stuck on:
:: Starting /usr/bin/mongod [BUSY]
and never transitions to "DONE" phase. Any tips?
Here is my script:
#!/bin/bash
# import predefined functions
. /etc/rc.conf
. /etc/rc.d/functions
# Point to the binary
DAEMON=/usr/bin/mongod
# Get the ARGS from the conf
. /etc/conf.d/crond
# Function to get the process id
PID=$(get_pid $DAEMON)
case "$1" in
start)
stat_busy "Starting $DAEMON"
# Check the PID exists - and if it does (returns 0) - do no run
[ -z "$PID" ] && $DAEMON $ARGS &> /dev/null
if [ $? = 0 ]; then
add_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
stop)
stat_busy "Stopping $DAEMON"
kill -HUP $PID &>/dev/null
rm_daemon $DAEMON
stat_done
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
I've looked at how apache does it, but I can't figure out what they are doing that's different. Here's a piece of their httpd script:
case "$1" in
start)
stat_busy "Starting Apache Web Server"
[ ! -d /var/run/httpd ] && install -d /var/run/httpd
if $APACHECTL start >/dev/null ; then
add_daemon $daemon_name
stat_done
else
stat_fail
exit 1
fi
;;
For one thing, you are passing an $ARGS variable that is never actually defined. You will probably want to either pass some configuration options, or the location of a mongodb.conf file using the -f or --config option, to inform the daemon of the location of your database, log file, IP bindings, etc.
The mongod defaults assume that you database location is /data/db/. If this does not exist, or the daemon does not have permissions to that location, then the init script will fail.
You should probably also run the daemon with a user account other than yourself or root (the default pacman package creates a user named mongodb), and give this user read/write access to the data path and log file.
[ -z "$PID" ] && /bin/su mongodb -c "/usr/bin/mongod --config /etc/mongodb.conf --fork" > /dev/null
I would suggest referring to the mongodb init script provided in the Arch Community package, and comparing that to what you have here. Or, install MongoDB using pacman, which sets all of this up for you.
If all else fails, add some 'echo' commands inside of your if and else blocks to track down exactly where the init script is hanging, check mongodb's logs, and report back to us.

Why my shell script is in standby in the background till I bring it back on the foreground?

I have a shell script which is executing a php script (worker for beanstalkd).
Here is the script:
#!/bin/bash
if [ $# -eq 0 ]
then
echo "You need to specify an argument"
exit 0;
fi
CMD="/var/webserver/user/bin/console $#";
echo "$CMD";
nice $CMD;
ERR=$?
## Possibilities
# 97 - planned pause/restart
# 98 - planned restart
# 99 - planned stop, exit.
# 0 - unplanned restart (as returned by "exit;")
# - Anything else is also unplanned paused/restart
if [ $ERR -eq 97 ]
then
# a planned pause, then restart
echo "97: PLANNED_PAUSE - wait 1";
sleep 1;
exec $0 $#;
fi
if [ $ERR -eq 98 ]
then
# a planned restart - instantly
echo "98: PLANNED_RESTART";
exec $0 $#;
fi
if [ $ERR -eq 99 ]
then
# planned complete exit
echo "99: PLANNED_SHUTDOWN";
exit 0;
fi
If I execute the script manually, like this:
[user#host]$ ./workers.sh
It's working perfectly, I can see the output of my PHP script.
But if I detach the process from the console, like this:
[user#host]$ ./workers.sh &
It's not working anymore. However I can see the process in the background.
[user#host]$ jobs
[1]+ Stopped ./workers.sh email
The Queue jobs server is filling with jobs and none of them are processed until I bring the detached script in the foreground, like this:
[user#host]$ fg
At this moment I see all the job being process by my PHP script. I have no idea why this is happening. Could you help, please?
Thanks, Maxime
EDIT:
I've create a shell script to run x workers, I'm sharing it here. Not sure it's the best way to do it but it's working well at the moment:
#!/bin/bash
WORKER_PATH="/var/webserver/user/workers.sh"
declare -A Queue
Queue[email]=2
Queue[process-images]=5
for key in "${!Queue[#]}"
do
echo "Launching ${Queue[$key]} instance(s) of $key Worker..."
CMD="$WORKER_PATH $key"
for (( l=1; l<=${Queue[$key]}; l++ ))
do
INSTANCE="$CMD $l"
echo "lnch instance $INSTANCE"
nice $INSTANCE > /dev/null 2> /dev/null &
done
done
Background processes are not allowed to write to the terminal, which your script tries to do with the echo statements. You just need to redirect standard output to a file when you put it to the background.
[user#host]$ ./workers.sh > workers.output 2> workers.error &
(I've redirected standard error as well, just to be safe.)

Problem with pidof in Bash script

I've written a script for me to start and stop my Perforce server. To shutdown the server I use the kill -SIGTERM command with the PID of the server daemon. It works as it should but there are some discrepancies in my script concerning the output behavior.
The script looks as follows:
#!/bin/sh -e
export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/var/local/perforce_depot
export P4PORT=1666
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
. /lib/lsb/init-functions
p4start="p4d -d"
p4stop="p4 admin stop"
p4user=perforce
case "$1" in
start)
log_action_begin_msg "Starting Perforce Server"
daemon -u $p4user -- $p4start;
echo "\n"
;;
stop)
echo "BLABLA"
echo "$(pidof /usr/local/bin/p4d)"
#daemon -u $p4user -- $p4stop;
p4dPid="$(pidof /usr/local/bin/p4d)"
echo $p4dPid
if [ -z "$(pidof /usr/local/bin/p4d)" ]; then
echo "ERROR: No Perforce Server running!"
else
echo "SUCCESS: Found Perforce Server running!\n\t"
echo "Shutting down Perforce Server..."
kill -15 $p4dPid;
fi
echo "\n"
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/perforce (start|stop|restart)"
exit 1
;;
esac
exit 0
When p4d is running the stop block works as intended, but when there is no p4d running the script with stop only outputs BLABLA and an empty new line because of the echo "$(pidof /usr/local/bin/p4d)". The error message stating that no server is running is never printed. What am I doing wrong here?
PS: The part if [ -z "$(pidof /usr/local/bin/p4d)" ]; then has been changed from if [ -z "$p4dPid" ]; then for debug reasons.
EDIT: I narrowed down the problem. If I don't use the p4dPid variable and comment out the lines p4dPid="$(pidof /usr/local/bin/p4d)" and echo $p4dPid the if block is processed and the error messages is printed. Still I don't unterstand what is causing this behavior.
EDIT 2: Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit the script after any statement returning a non-zero return value.
When your service is not running, the command
echo "$(pidof /usr/local/bin/p4d)"
is processed as
echo ""
because pidof did not return any string. So the command outputs an empty line.
If you do not want this empty line, then just remove this statement, after all you print an error message when the process is not running.
Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit after any statement returning a non-zero return value.

Resources