LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2016, 02:34 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Rep: Reputation: 2
running init.d script: via shell vs shutdown_process.


Code:
##(below is a bad way to find where line ends)
ALL=($(ps -ef | grep httpd | grep -vE "root|grep" | sed 's/.*[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}\ //'  |  sed ':a;N;$!ba;s/\n/\ XXXXXX\ /g')) #  sed 's/[0-9]\{2\}[0-9]\{2\}[0-9]\{2\}//')) 

DIR=""
VER=""

RUNR=${ALL[0]}

TAB="YES"
APPDIR=()
FLAG=0
ARGV="$@"
CMD="/usr/sbin/httpd2"
PID=/tmp/OUT-APACHE
TRACK=""


LOGDIR=/test/logs/#
PREFIX=$(echo $SCRIPTNAME | sed 's/\.ksh$//g' | sed 's/\.sh$//g')
TS=$(date +%Y%m%d%H%M%S)


LOGFILE=${LOGDIR}/${PREFIX}.${TS}.log

if [[ ! -d "$LOGDIR" ]]; then
      echo "WARN - $LOGDIR does not exist. Attempting to create it."
      mkdir $LOGDIR
      if (( $? != 0 )); then
         echo  "ERROR - unable to create $LOGDIR directory...aborting."
      else
         echo "WARN - $LOGDIR created successfully." | tee -a $LOGFILE
      fi
fi



save()
{
if [[ -z $(ps -ef | awk '!/awk/ && /httpd/ && !/log/') ]]; then
        echo ERR:  HTTPD was not running ont the server.. aborting... ;
        exit
fi

if [[ -e $PID ]];then
        echo INFO: $PID already exist. Backing it up now. | tee -a $LOGFILE
        mv $PID $PID-$(date +%d%b%Y-%s)
fi



for (( i=0; i< ${#ALL[@]} ;i++))
do
        if [[  -z $DIR ]];then
                if [[ ${ALL[i]} = "-d" ]];then
                        DIR=${ALL[i+1]}
echo INFO: line 92 | tee -a $LOGFILE

                fi
        fi
done

for (( i=0; i< ${#ALL[@]} ;i++))
do
echo INFO: line 100 | tee -a $LOGFILE
echo INFO: ${ALL[i]} | tee -a $LOGFILE


FLAG=0
        if [[ ${ALL[i]} = "XXXXXX" ]];then
                TAB="YES"   ## line break subsitute.
               CMD=${ALL[i+1]}
        fi

So this is part of script. When I execute it ./<script> stop/star
it works fine.
While when its executed from init.d(server reboot) itself.

it does not carry all the information in variables. Specially in red-parts.

I record out put in a log file when it gets executed from init.d(server reboot process)
 
Old 08-29-2016, 03:08 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Do you have questions regarding this script?
 
Old 08-29-2016, 08:36 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Original Poster
Rep: Reputation: 2
yes.

since the script works differently while executing upon server reboot, it can not perform the task it supposed to do.

My question would be, does any one here know, what makes script act differently between
a-when executed manually.
b-when executed via server reboot.

Are their different set of rules we need to follow while making script work via server reboot.
Things I know.

1-scrip get executed via server reboot both on shutdown and start up.
2-upon start up it behave more normally.
3-on shutdown.. seems like variable do not get populated.
Going back to point a above.
4-script runs fine when executed normally.

Going back to question:
Any ideas ?
 
Old 08-29-2016, 11:35 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try to log/debug, eg.:
Code:
#!/bin/sh

set -vx
exec >>/tmp/myscript.$$ 2>&1
Edit: you seem to use bash-specific features, so change the she-bang accordingly.

Last edited by NevemTeve; 08-29-2016 at 11:39 PM.
 
Old 09-02-2016, 03:08 PM   #5
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Original Poster
Rep: Reputation: 2
seems like when you do "ps -ef" thingie, as form shell it shows you ever thing.
when you do it from init.d/shutdown process, it only spit out some thing like 97 char.

So that's whey my script is getting messed up.

Issue some what resolved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] can i write a shell script in /etc/init.d that will run a python script? lapishater276 Debian 1 06-27-2014 03:23 PM
If..else..fi shell script statements in Init Potatos Linux - Newbie 3 05-21-2010 10:35 AM
init script not running naz37 Programming 2 06-12-2006 01:50 PM
/etc/init.d/cyrus Shell Script 'Permission denied' ServerStorm Linux - Software 3 01-19-2005 11:04 PM
inetd script in /etc/init.d is running amuck int0x80 Linux - Newbie 1 03-07-2003 09:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration