Use of exec command in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use of exec command in a script
# 1  
Old 05-07-2013
Use of exec command in a script

Guru's,
I want to make a use of "exec" command in my script and want to check return code of executing script, but as you know exec command will terminate current processID and comeout and will trigger new one, i am unable to check return code of script and not able to run a scrpit after exec.
so can you please help me to fix or give alternative to trigger a script.

Basically i am trying to check add a check whether script A is running or not if NO then it will trigger another instance of same script.

Script :

Code:
#!/usr/bin/ksh
LOG_FILE=/GEM_PILOT/custom/script/cx_log
SERVER="${HOSTNAME}";
USER=`whoami`
CURRENT_PROCESS_ID=" ";

if [ -n "${SERVER})" ]
then
  while read LINE
  do
    if [ `echo ${LINE} | cut -c1` != "#" ]
    then
      DIR=`echo ${LINE} | cut -f1 -d"|"`;
      cd $DIR
      SCRIPTNAME=`echo ${LINE} | cut -f2 -d"|"`;
      ISEXECUTABLE=`echo ${LINE} | cut -f3 -d"|"`;
      DESC=`echo ${LINE} | cut -f4 -d"|"`;
    fi
    if [ -n "${SCRIPTNAME}" ]
    then
      CURRENT_PROCESS_ID=`ps -ef | grep "${SCRIPTNAME}" | grep -v grep | grep "${USER}" | grep "1" | tr -s ' ' ' ' | cut -c2- | cut -f2 -d" "`;
      if [ -z "$CURRENT_PROCESS_ID" ]
      then
        cd ${DIR}
        exec  $SCRIPTNAME;
        RET=$?;
        if [ "${RET}" -eq 0 ]
        then
           NEW_PROCSS_ID=`ps -ef | grep "${SCRIPTNAME}" |grep -v grep | grep "${USER}" | grep "1" |  tr -s ' ' ' ' | cut -c2- | cut -f2 -d ""`;
          if [ -z "$NEW_PROCSS_ID" ]
          then
            echo "$SCRIPTNAME has successfully started with ProcessID ${NEW_PROCESS_ID}" >> $LOG_FILE
        mailx -s "$USER instnce of $SCRIPTNAME restarted with ProcessID $NEW_PROCSS_ID" test@xyz.com
          fi
      fi
      fi
      echo "$SCRIPTNAME is already running with $CURRENT_PROCESS_ID SPID" >> ${LOG_FILE};
    fi
    done < /GEM_PILOT/custom/script/test.dat;
fi


Last edited by pawar.atul28; 05-07-2013 at 12:32 PM..
# 2  
Old 05-07-2013
Delete the word "exec".

Regards,
Alister
# 3  
Old 05-08-2013
Allister thank you for replying, i tried removing exec command, it's triggering script, but command followed after those are getting bypass and not getting execute.
I want to execute those as well and send alerts to team
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Programming

Script to monitor progress of find/exec command

hi all, i want to monitor the progress of a find and exec command, this is the code i use - find . -type f -exec md5sum {} \; >> /md5sums/file.txt this command works and produces a text file with all the md5sums but while running it doesnt show the progress is there anyway i can do this... (4 Replies)
Discussion started by: robertkwild
4 Replies

3. Programming

Bash script - find command with delete and exec

hi all, i have devised a script that starts in /restored/ and in there, there are a lot of sub folders called peoples names and in the sub folders are files/folders and it deletes the data in the sub folders BUT not the sub folder itself and it should then touch a file in all the sub folders... (3 Replies)
Discussion started by: robertkwild
3 Replies

4. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

5. Shell Programming and Scripting

exec perl in expect script yields "invalid command"

I'm trying to execute something like this: exec perl -i -pe 's/\015/\012/g' '${file}' in my expect script and I get: error "invalid command name \"perl\". however, if I run perl -i -pe 's/\015/\012/g' "/Users/Shared/menu-items.txt" directly in my terminal, it runs fine. I'm an... (4 Replies)
Discussion started by: dpouliot
4 Replies

6. Shell Programming and Scripting

exec a build command (adduser) in a script

Hi, With a awk script i create a "adduser line" $ cat /tmp/tmp.ldif | awk -f ldif2adduser.awk adduser --uid 1002 --gid 1000 --gecos "ROUSSIN Guy" --home /homeL/guy --shell /bin/bash --disabled-password guy If i cut and paste this line, all is fine. But in a shell script i get errors : ... (2 Replies)
Discussion started by: guyr
2 Replies

7. Shell Programming and Scripting

exec command

can any one pls explain the meaning of exec 1<&5 ?? its urgent (2 Replies)
Discussion started by: santosh1234
2 Replies

8. UNIX for Dummies Questions & Answers

Need help with -exec cp command.

I have a ksh script that contains the following: find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt -exec cp /dir1/dir2/dir3 {} \; When I run it from the cli, it runs fine. When I run it from the ksh script I get find: missing argument to `-exec' I also tried -exec cp... (40 Replies)
Discussion started by: bbbngowc
40 Replies

9. Shell Programming and Scripting

exec command

hai i want know the difference between two shell scripts those are 1) a=2004 echo $a #output------2004 exec < inputfile while read line do echo $a #output-------2004 a=2005 echo $line echo $a ... (1 Reply)
Discussion started by: g_s_r_c
1 Replies

10. Shell Programming and Scripting

Multiple exec command s in a script

Hi everyone, I've been racking my brains for ages on this and need your help/advice. I am writing a script that is reading in file to process and putting them into a temporary file. The loop starts and the script gets the first file name, does what i needs to do (copy it) and then returns to... (2 Replies)
Discussion started by: Angoss
2 Replies
Login or Register to Ask a Question