when i start service script with ./script start able to get the credentials through proxy (Environment variables found), if i start the service script like service script start not able to get the credentials through proxy(Environment variables not found), Proxy was not found. How can i work this script through service script start?
script :
#!/bin/bash -l
# chkconfig: 2345 20 80
if [ "$system" = "redhat" ]; then
## source platform specific external scripts
. /etc/init.d/functions
## set or override platform specific variables
lockfile=${LOCKFILE-/var/lock/subsys/$SERVICE_NAME}
## set or override platform specific functions
start_daemon() {
daemon --user $1 --pidfile $2 "$3 $4"
}
log_daemon_msg() {
echo -n $"$1"
}
echo_ok() {
echo_success; echo
}
echo_fail() {
echo_failure; echo
}
log_success_msg() {
success $"$#"
}
log_failure_msg() {
failure $"$#"
echo $"$#"
}
log_action_msg() {
echo $#
}
fi
if [ -n "$http_proxy" ] ; then
echo "proxy found"
echo $http_proxy | grep "#"
if [ $? -eq 0 ]; then
PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/.*#\(.*\):.*/\1/')
PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*#.*:\(.*\)/\1/' | tr -d "/")
USERNAME=$(echo $http_proxy | sed 's/http:\/\/\(.*\)#.*/\1/'|awk -F: '{print $1}')
PASSWORD=$(echo $http_proxy | sed 's/http:\/\/\(.*\)#.*/\1/'|awk -F: '{print $2}')
else
PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/\(.*\):.*/\1/')
PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*:\(.*\)/\1/' | tr -d "/")
fi
fi
start() {
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
CMD="java "
if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ] ; then
CMD="java -Dhttps.proxyHost=$PROXY_HOST -Dhttps.proxyPort=$PROXY_PORT"
if [ -n "$USERNAME" -a -n "$PASSWORD" ]; then
CMD="$CMD -Dhttps.proxyUser=$USERNAME -Dhttps.proxyPassword=$PASSWORD"
fi
fi
#nohup $CMD -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
#echo "$PATH_TO_JAR"
echo "$CMD"
nohup $CMD -jar $PATH_TO_JAR 2>> /tmp/glog >> /tmp/glog &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
}
case "$1" in
start)
start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
When i am running this script like ./script start, script able to find proxy settings and passing the proxy settings. But while running with service script start not able to find proxy settings. Did i miss anything to load environment variables while running script with service?
From man service:
service runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /.
So when you try to run a service script, only several env variables like TERM, LANG remains, your http_proxy and https_proxy is discarded. To use environment variables you need, you could define them in file /etc/default/yourservice with:
http_proxy=http://yourproxy.com
https_proxy=http://yourproxy.com
and source it in the beginning of your script:
[ -f /etc/default/yourservice ] && . /etc/default/yourservice
Related
#!/bin/bash
host=$1
startport=$2
stopport=$3
function pingcheck
{
ping = `ping -c 1 $host | grep bytes | wc -l`
if [ $ping > 1 ]; then
echo "$host is up";
else
echo "$host is down quitting";
exit
fi
}
function portcheck
{
for ((counter=$startport; counter<=$stopport; counter++))
do
(echo > /dev/tcp/$host/$counter) > /dev/null 2>&1 && echo "$counter open"
done
}
pingcheck
portcheck
I tried testing the script by passing 127.0.0.1 1 5 to it from the terminal but all i keep getting is ping: unknown host =
127.0.0.1 is down quitting. Tried with other IP Addresses as well, I got the same output. I was following instruction from a book as I am new to shell scripting. It will be helpful if someone can tell me what I am doing wrong.
I made some comments inline:
#!/bin/bash
host=$1
startport=$2
stopport=$3
function pingcheck
{
ping=`ping -c 1 $host | grep bytes | wc -l` #Don't use spaces before and after the "="
if [ $ping -gt 1 ]; then #Don't use >, use -gt
# if [[ $ping > 1 ]]; then #Or use [[ and ]], but this won't work in all shells
echo "$host is up";
else
echo "$host is down quitting";
exit
fi
}
function portcheck
{
for ((counter=$startport; counter<=$stopport; counter++))
do
(echo > /dev/tcp/$host/$counter) > /dev/null 2>&1 && echo "$counter open"
done
}
pingcheck
portcheck
Variables in bash are always in the format:
VARNAME=VALUE
You should not put spaces in between there. VALUE could be an expression using `` or using $(). $() is usually the preferred way, because you can do $(something $(something)) and you can't do `something `something``.
The syntax of if is:
if EXPRESSION
then
something
fi
An expression is in sh always a call to an application. [ is an application usually used in ifs. You can get a really good manual of [ by doing man [. Bash has native support for [[, which isn't an application, but can do more than [.
I have 4 important services running on my machine which I want to see them all the time. I have this simple bash script running as bash profile.
echo
PROC="nginx mysql php-fpm pptpd"
for p in $PROC
do
ps cax | grep $p > /dev/null
if [ $? -eq 0 ]; then
echo -e "\e[92m$p running\e[0m"
else
echo -e "\e[101m$p IS NOT RUNNING \e[0m"
fi
done
echo
The out put of this script is:
nginx running
mysql running
php-fpm running
pptpd running
How can I make it like this?
nginx running - mysql running - php-fpm running - pptpd running
Build the status lines first into an array, and then print the array:
status=()
for p in $PROC
do
if ps cax | grep -q $p; then
status+=( " \e[92m$p running\e[0m " )
else
status+=( " \e[101m$p IS NOT RUNNING \e[0m " )
fi
done
(IFS=-; echo -e "${status[*]}")
${status[*]} expands to every element in the array joined by the first character of IFS, which I set to - earlier. Note that I used a subshell (IFS=-; echo ...), so that changing IFS doesn't affect the rest of the script.
Other notes:
ps cax | grep $p > /dev/null
if [ $? -eq 0 ]; then
Can be combined to:
if ps cax | grep -q $p; then
Which is much more concise and readable. You could also consider using pgrep instead.
Use printf or add the -n flag to echo.
POSIX Compliant Refactor without ProcTools
#!/bin/sh
showstatus() {
echo
while [ "$1" ]; do
if ps cax | grep -qF "$1"; then
msg='\e[92m%s running\e[0m'
else
msg='\e[101m%s IS NOT RUNNING \e[0m'
fi
printf "$msg" "$1"
shift
[ "$1" ] && printf ' - '
done
echo
}
showstatus nginx mysql php-fpm pptpd
POSIX Compliant Refactor with ProcTools
#!/bin/sh
showstatus() {
echo
while [ "$1" ]; do
if pkill -0 "$1"; then
msg='\e[92m%s running\e[0m'
else
msg='\e[101m%s IS NOT RUNNING \e[0m'
fi
printf "$msg" "$1"
shift
[ "$1" ] && printf ' - '
done
echo
}
showstatus nginx mysql php-fpm pptpd
I have written the following Linux shell script through snippits gleaned from the web (I'm very new to shell scripts), its purpose is to ensure only a single instance of a programme is run with the added option of specifying which workspace a programme will open to.
I'm sure much of the code could be better constructed, however it works with one bug, when some, like Thunderbird, are opened they ignore the workspace switch unless the workaround I've added is used, but why? and is there a better way?
The script uses wmctrl: sudo apt-get install wmctrl
Usage: single-switch programme_name [-ws(int)] where (int) is number of workspace (must exist), the -ws param must be the last listed
#!/bin/bash
if ! [ $1 ] ; then exit ; fi
if [ "?" = "$1" ] ; then
FILE=$(echo "$0" | rev | cut -d "/" -f1 | rev) # extract filename from path
echo "usage $FILE <program name> [-ws(int)]"
exit 1;
fi
TITLE=$1
NAME=""
for var in "$#"; do [ "$(echo ${var} | head -c3)" != '-ws' ] && NAME="$NAME $var" ; done # remove our param from command
ntharg() { shift $1 ; echo $1 ; }
PARAM=`ntharg $# "$#"` # get the last param
if [ "-ws" != "$(echo ${PARAM} | head -c3)" ]; then PARAM=-1 ; # check its ours
else
PARAM=$( echo "$PARAM" | egrep -o '[0-9]+' ) # get the number
PARAM=$((PARAM-1)) # decrement
fi
if [ $PARAM -ge 0 ] ; then
wmctrl -x -a "$TITLE" || ( wmctrl -s $PARAM && zenity --title="Launch $TITLE" --warning --text="$TITLE is starting" --timeout="1" ; $NAME )
# dummy message otherwise some (ie thunderbird) ignore switch
else
wmctrl -x -a "$TITLE" || $NAME & # no switch, just raise or run programme
fi
# Done.
#
i wrote alert log script and some how this is not working and not throwing any error when i execute the script
i am suspecting sed part is not working properly. could you please advice where i am doing wrong?
here is the piece of code
#!/bin/sh
## Heading #########################################################################################
#---------------------------------------------------------------------------------------#
# script usage #
#---------------------------------------------------------------------------------------#
_usage() {
echo "Usage: $0 ORACLE_SID "
} # _usage
ORACLE_SID="$1"
setenv ()
{
eval "$1=$2"
export "$1"
} # setenv
unsetenv ()
{
while [ $# -gt 0 ]
do
unset "$1"
shift
done
} # unsetenv
if [ $# -ne 1 ]; then
_usage
exit 1
fi
Env=/u01/app/oracle/config
HN=`uname -n`
ERROR_FILE=/tmp/${ORACLE_SID}_error.log
HN=`hostname`
DBA_MAIL="oracle.mail#company"
DBA_PAGE=""
#+--------------------------------------------------------------------------------------+
#| get oracle environment variables from our common env dir |
#+--------------------------------------------------------------------------------------+
if [ -r $Env/${ORACLE_SID}.env ]
then
. $Env/${ORACLE_SID}.env
else
ORACLE_SID=""
fi
#+--------------------------------------------------------------------------------------+
#| just checking for Oracle Env variables for connecting database |
#+--------------------------------------------------------------------------------------+
if [ "$ORACLE_SID" = "" ]
then
echo "ORACLE_SID is invalid"
exit 1
fi
if [ "$ORACLE_HOME" = "" ]
then
echo "The environment variable ORACLE_HOME must be set"
exit 1
fi
if [ "$ORACLE_BASE" = "" ]
then
echo "The environment variable ORACLE_BASE must be set"
exit 1
fi
_AlertLogLoc ()
{
ALERTLOG=`$ORACLE_HOME/bin/sqlplus -s "/as sysdba" << EOF
set head off pause off term on feed off timing off
select value from v\\$parameter where name like 'background_dump_dest';
exit;
EOF`
}
_AlertLogLoc
echo $ALERTLOG
export ALERTLOG
if [ -f $ALERTLOG/alert_${ORACLE_SID}.log ]; then
echo "Found Database Alert log"
else
echo "Alert log not found .. exit from script"
fi
if [ -f $ALERTLOG/alert_${ORACLE_SID}.skip ]; then
echo " ORACLE_SID skip error file found"
SKIP_ERR=`cat $ALERTLOG/alert_$ORACLE_SID.skip|xargs|sed -e 's/ /|/g'`
echo $SKIP_ERR
else
echo "No errors will be excluded"
fi
REC_CUR_ALSIZE=/oraworkspace/OSE/logs/alert_${ORACLE_SID}.size # file to record current alert log lines
#---------------------------------------------------------------------------------------------------------#
# let Capture ORA- error from the alert log #
#---------------------------------------------------------------------------------------------------------#
if [ -f $REC_CUR_ALSIZE ]; then
ALSIZE=`cat $REC_CUR_ALSIZE|sed -e 's/^[ \t]*//'`
ALSIZE=`expr $ALSIZE + 1`
else
ALSIZE=0
fi
if [ $ALSIZE -eq 0 ]; then
echo "PROBABLY RUNNUNG THE SCRIPT FIRST TIME"
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |egrep -v "$SKIP_ERR"|grep -i 'ORA-' > /tmp/${ORACLE_SID}_error.log
#`wc -l $ALERTLOG/alert_${ORACLE_SID}.log > $REC_CUR_ALSIZE
cat $ALERTLOG/alert_${ORACLE_SID}.log|wc -l > /oraworkspace/OSE/logs/alert_${ORACLE_SID}.size
#ALSIZE=`cat $ALERTLOG/alert_${ORACLE_SID}.log |wc -l`
else
sed -n ${ALSIZE}',$p' $ALERTLOG/alert_${ORACLE_SID}.log |egrep -v "$SKIP_ERR"|grep -i 'ORA-' > /tmp/${ORACLE_SID}_error.log
#wc -l $ALERTLOG/alert_${ORACLE_SID}.log >> $REC_CUR_ALSIZE
cat $ALERTLOG/alert_${ORACLE_SID}.log |wc -l > /oraworkspace/OSE/logs/alert_${ORACLE_SID}.size
fi
#---------------------------------------------------------------------------------------------------------#
# Notify if any errors are found #
#---------------------------------------------------------------------------------------------------------#
ERR_CNT=`cat /tmp/${ORACLE_SID}_error.log |wc -l`
if [ $ERR_CNT -ne 0 ]; then
echo "Errors found in the alert log. send email notification"
mailx -s "${HN}:${ORACLE_SID} ORA error Found in the alert log" ${DBA_MAIL} < $ERROR_FILE
#mailx -s "${HN}:${ORACLE_SID} ORA error Found in the alert log" ${DBA_MAIL} < $ERROR_FILE
else
echo " No errors found in the alert log"
fi
If $ALERTLOG/alert_$ORACLE_SID.skip doesn't exist or is empty (around line 94), the egrep -v "$SKIP_ERR" will exclude all lines from the sed output, so it will not have a chance to see any remaining ORA- errors.
ALSIZE=1
SKIP_ERR=""
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |\
egrep -v "$SKIP_ERR"|wc -l
0
SKIP_ERR="dummy"
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |\
egrep -v "$SKIP_ERR"|wc -l
15165
So you need to set your SKIP_ERR to something when it's not set or empty (if your skip file is empty). You haven't said if that is the case or shown the script output, but it seems to work apart from that.
Also not that if ALSIZE is zero sed isn't happy, at least in RHEL 5:
ALSIZE=0
SKIP_ERR="dummy"
sed -n $ALSIZE',$p' $ALERTLOG/alert_${ORACLE_SID}.log |\
egrep -v "$SKIP_ERR"|wc -l
sed: -e expression #1, char 4: invalid usage of line address 0
0
And when you test that the file exists at line 91, you show a message stating that and suggesting you'll stop there, but there is no exit after line 94; doesn't seem to be relevant here but seems like an oversight?
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
You need to use additional tools like node-supervisor for this case.
Install node-supervisor with npm:
sudo npm install -g supervisor
Change DAEMON variable in your init.d script to node-supervisor executable: /usr/bin/supervisor. You can check this path using command 'whereis supervisor' in your system (after installation, of course).
Now supervisor will restart your application if it's crash.