Custom service start is not working in OLE6. Where am i going wrong? - linux

I have Created a service in /etc/init.d. It calls another script file catalina.sh within it.
It starts fine with
./Service_name start
but when i try
service Service_name start
the script is executed but the service does not start
Here is the concerned part of the script file
start(){
echo -n "Starting $VAR"
PID="$(pgrep -f $VAR)"
if [ "$PID" = "" ]
then
cd /home/com/Analytics/servers/$VAR/bin
./catalina.sh start >/dev/null
while [ $temp -lt $startime ]
do
sleep 5
echo -n " ."
temp=$(( $temp + 5 ))
done
echo -e "\e[0;32m [ OK ] \e[0m"
else
echo -e "\e[0;31m [ FAILED ] \e[0m"
echo -e "\e[0;33m $VAR is already running. \e[0m"
fi
}
Also i would like to mention that
service Service_name stop
and
service Service_name status
works fine.

Actually the problem was requirement of bash of JAVA_HOME or JRE_HOME when catalina.sh is called as a service.. Which was fixed by exporting JAVA_HOME.
export JAVA_HOME=path_to_jdk

Related

Linux Script to check if process is running and restart if not

I am having this script which looks for the process filebeat and restarts it if is not running. Cron runs this script every 5 minutes. Most of the time this works fine except sometime it creates multiple filebeat process. Can someone please point out what is the issue in my script.
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
service=filebeat
servicex=/usr/share/filebeat/bin/filebeat
pid=`pgrep -x "filebeat"`
if [ $pid > /dev/null ]
then
echo "$(date) $service is running!!!"
else
echo "$(date) starting $service"
cd /home/hpov/beats/filebeat
./filebeat -c filebeat.yml &
fi
#!/bin/bash
pidof script.x86 >/dev/null
if [[ $? -ne 0 ]] ; then
echo "Restarting script: $(date)" >> /var/log/script.txt
/etc/script/script.x86 &
fi
Super easy :D

can shell script make itself run in background after running some steps?

I have BBB based custom Embedded Linux based board with busybox shell(ash)
I have a situation where my script must run in background with following condition
There must only one instance of the script.
wrapper script need to know if script started successfully in background or not.
There is another wrapper script which starts and stops my script, wrapper script is as mentioned below.
#!/bin/sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
readonly TEST_SCRIPT_PATH="/home/testscript.sh"
readonly TEST_SCRIPT_LOCK_PATH="/var/run/${TEST_SCRIPT_PATH##*/}.lock"
start_test_script()
{
local pid_of_testscript=0
local status=0
#Run test script in background
"${TEST_SCRIPT_PATH}" &
#---------Now When this point is hit, lock file must be created.-----
if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
if [ -n "${pid_of_testscript}" ];then
kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
if [ ${status} -ne 0 ];then
echo "Error starting testscript"
else
echo "testscript start successfully"
fi
else
echo "Error starting testscript.sh"
fi
fi
}
stop_test_script()
{
local pid_of_testscript=0
local status=0
if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
if [ -n "${pid_of_testscript}" ];then
kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
if [ ${status} -ne 0 ];then
echo "testscript not running"
rm "${TEST_SCRIPT_LOCK_PATH}"
else
#send SIGTERM signal
kill -SIGTERM "${pid_of_testscript}"
fi
fi
fi
}
#Script starts from here.
case ${1} in
'start')
start_test_script
;;
'stop')
stop_test_script
;;
*)
echo "Usage: ${0} [start|stop]"
exit 1
;;
esac
Now actual script "testscript.sh" looks something like this,
#!/bin/sh
#Filename : testscript.sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
set -eu
LOCK_FILE="/var/run/${0##*/}.lock"
FLOCK_CMD="/bin/flock"
FLOCK_ID=200
eval "exec ${FLOCK_ID}>>${LOCK_FILE}"
"${FLOCK_CMD}" -n "${FLOCK_ID}" || exit 0
echo "${$}" > "${LOCK_FILE}"
# >>>>>>>>>>-----Now run the code in background---<<<<<<
handle_sigterm()
{
# cleanup
"${FLOCK_CMD}" -u "${FLOCK_ID}"
if [ -f "${LOCK_FILE}" ];then
rm "${LOCK_FILE}"
fi
}
trap handle_sigterm SIGTERM
while true
do
echo "do something"
sleep 10
done
Now in above script you can see "---Now run the code in background--" at that point I am sure that either lock file is successfully created or instance of this script is already running. So Then I can safely run other code in background and wrapper script can check for lockfile and find out if the process mentioned in the lock file is running or not.
can shellscript itself make it to run in background ?
if not is there a better way to meet all the conditions ?
I think you can look into job control built-in, specifically bg.
Job Control Commands
When processes say they background themselves, what they actually do is fork and exit the parent. You can do the same by running whichever commands, functions or statements you want with & and then exiting.
#!/bin/sh
echo "This runs in the foreground"
sleep 3
while true
do
sleep 10
echo "doing background things"
done &

Bash script to create virtual clusters isn't working

I am trying to create a script to create virtual clusters on my virtual machine which is a CentOS 7 minimal.
I got a script named cluster
#!/bin/bash
function vc
{
echo
echo -n "Enter project name: "
read platform_name
echo
echo -n "web extension: "
read web_extension
echo
echo -e "The following website will be created"
echo -e "\e[32m Platform:\e[0m\t${platform_name}"
echo -e "\e[32m Extension:\e[0m\t${web_extension}"
echo -e "\e[32m Full URL:\e[0m\t http://www.${platform_name}.${web_extension}"
echo
echo -e "Do you wish to proceed? [Y/n]"
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo -e "\e[32m Creating platform \e[0m\t"
else
echo
echo -e "\e[32m Not creating platform \e[0m\t"
fi
}
if [ -n "$(type -t $FUNCTION_NAME)" ] && [ "$(type -t $FUNCTION_NAME)" =
function ];
then $FUNCTION_NAME $2; else help; fi
Then as far as I understood I just have to make it executable
chmod +x cluster
And after this I should make a syslink for it ln -s cluster /bin/cluster
And now I should normally be able to just typ cluster vc in the terminal and it should execute the script but it keeps giving me "command cluster not found"
Am I doing something obviously wrong? Or do I need to use another chmod on it so I can run this?
Symbolic link targets are resolved relative the the symlink location. In your case that means, if you run /bin/cluster it looks for a file named cluster (the target) in the /bin/directory. Either provide a relative path which points to your file or link to an absolute path: ln -s /path/to/cluster /bin/cluster.
Also make sure that the target location is readable and executable by whomever executes the symlink.

Bash script not returning any output

I'm a newbie in the linux environment, and I'm starting to create an automated smoke test for several commands we frequently use at our company. Basically, running some kind of shell script that runs through multiple commands and also validates the command's output.
The first test cases I started writing out was to check our service can be successfully stopped and started. After researching around about bash scripts I came up with this:
#!/bin/bash
sudo service companyservice stop | grep 'Stopping companyservice ... [ OK ]' &> /dev/null \
if [ $? == 0 ] then echo "Stopping Company Service: SUCCESS" \
else echo "Stopping Company Service: FAIL. GO HARASS A DEVELOPER" \
fi
sudo service companyservice start | grep 'Starting companyservice ... [ OK ]' &> /dev/null \
if [ $? == 0 ] then echo "Starting Company Service: SUCCESS" \
else echo "Starting Company Service: FAIL. GO HARASS A DEVELOPER" \
fi
I saved this as SmokeTest.sh, but when running sh SmokeTest.sh on command line, I see nothing on the output. No error, no failure, no success. Nothing.
Any help or hints with this is much appreciated. I am using Red Hat 6.6 OS.
Also should this be right way to automate on Linux if I want to validate command's outputs?
Your line continuation characters \ at the end of the sudo lines are making the if part of the command line you're running. Get rid of those, and you should start to see syntax errors because you don't have ; after the conditions for your if statements before then
Also, on the lines with the continuation characters you're redirecting stderr to /dev/null which is why you wouldn't see it complaining about the situation
As you noted, it's possible to not put the ; with an if, but if you do so the then must be on the next line:
if [ -z "$var" ] then
Is wrong but
if [ -z "$var" ]; then
or
if [ -z "$var" ]
then
are both acceptable.
Also, the single line continuation characters might have been a little lost. If a line of bash ends with \ it means that the following line should actually be treated as part of the current line. So in your example:
sudo service companyservice stop | grep 'Stopping companyservice ... [ OK ]' &> /dev/null \
if [ $? == 0 ] then echo "Stopping Company Service: SUCCESS" \
else echo "Stopping Company Service: FAIL. GO HARASS A DEVELOPER" \
fi
is actually treated as a single line like
sudo service companyservice stop | grep 'Stopping companyservice ... [ OK ]' &> /dev/null if [ $? == 0 ] then echo "Stopping Company Service: SUCCESS" else echo "Stopping Company Service: FAIL. GO HARASS A DEVELOPER" fi
which is not right. If you remove the \ from each line and settle on a way to do the if...then you should be in much better shape
&> /dev/null will direct any output/errors to /dev/null and you will see no output or error. remove these parts or redirect them to a file for exmple:
&> log.txt

How to tell the status of a Linux Daemon

We have a Linux Daemon in c and a bash script to start it. The daemon sometimes fail to start because of some configuration file errors but the script reports the daemon was started successfully. A snippet of the script is shown as below, could someone tell me what's wrong with the script?
...
case "$1" in
start)
echo -n "Starting Demo Daemon: "
sudo -u demouser env DEMO_HOME=$DEMO_HOME /usr/local/demouser/bin/democtl startup > /dev/null 2> /dev/null
if [ "$?" = "0" ]; then
echo_success
else
echo_failure
fi
echo
;;
...
Thanks!
I feel there is nothing wrong with the script,it is the reponsibility of daemon to return non zero exit status if failed to start properly and based on those the script will display the message.(which i think it does)
You can add following line in your script to get running status of your Linux Daemon
status=`ps -aef |grep "\/usr\/local\/demouser\/bin\/democtl" |grep -v grep|wc -l`
if [ "$status" = "1" ]; then
echo_success
else
echo_failure
fi

Resources