Script to start process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to start process
# 1  
Old 08-14-2017
Script to start process

Hi

I want to create a script to be able to check if a process is running and act on it. Essentially I want to:
  • Check acc process status /opt/ca/APMCommandCenterController./apmccctrl.sh status
  • If process found not to be running
    • Execute start command /opt/ca/APMCommandCenterController./apmccctrl.sh start
    • Sleep 60
    • Check acc process again
    • If still not in a running state
      • Return ‘Not running' return code to enterprise manager or 0
    • Else
      • Return running return code to enterprise manager or 1
I think it the start is pretty straight forward
Code:
acc_command=/opt/ca/APMCommandCenterController/apmccctrl.sh
  
 if acc_command="CA APM Command Center Agent Controller is running: PID:20583, Wrapper:STARTED, Java:STARTED"
  
 then
 acc_status="Running"
 else
 ./apmccctrl.sh start
 sleep 60

After the sleep part I am not 100% sure on how to get it too check again and if it is wrong return not running for example.

Thanks In Advance

Last edited by rbatte1; 08-14-2017 at 01:15 PM.. Reason: Removed gratuitous FONT formatting and added formatted lists and ICODE tags for the logic description to make it clear
# 2  
Old 08-14-2017
Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

I'm afraid I don't really understand what you want to implement. Your verbal specification deviates from your pseudo code. In the latter, you define the acc_command variable twice, but you don't run the apmccctrl.sh at all, neither with the start nor the status parameter. On top, your if statement has a syntax and a logical error; use test or [ to check for the exit status, and consider that a variable assignment always returns 0 (success).

EDIT: I have to correct myself: of course you can check a command's exit status without test or [ : if command; then ... ; still the remark applies that above wouldn't work ...

Last edited by RudiC; 08-16-2017 at 01:23 PM..
# 3  
Old 08-14-2017
I have formatted your logic to hopefully make it easier to read and removed the excessive formatting. Please just write in plain text and use the formatting tools on this page directly to keep it all cleaner / clearer.

Can you confirm what language you are wanting to use? It could be ksh, bash, perl, or a variety of others which might have similar syntax but enough subtleties to make it difficult to know how to respond, for instance shell scripts usually use a return code zero for success whereas Perl uses a return of zero for a failure.

Please can you clarify what you need and what tools you are using.




Thanks, in advance,
Robin
# 4  
Old 08-15-2017
Hi

I am currently using a bash script.

I want to create a script when I am checking if apmccctrl.sh is running & if it isn't running I want to be able to start it up and check again.

If the shell script is running I want to be able to return a status of "running" or number 1 for example.
If found it isn't running I want it to try and start it up and wait 60 seconds to then again check if it has started up successfully or not.
If failed again I want it to return "not running or 0 for example so we are able to alert on it.
Hope this makes it easier to explain.

I am using Putty version 0.63

This is what I have created so far but even if I shutdown the apmccctrl.sh I still get a return code of 1.

Code:
acc_status_check=$(${ACC_HOME}/apmccctrl.sh status)

if acc_status_check="CA APM Command Center Agent Controller is running"

then
acc_status="1"

else
/opt/ca/APMCommandCenterController/apmccctrl.sh start
sleep 60
/opt/ca/APMCommandCenterController/apmccctrl.sh
if acc_status_check="CA APM Command Center Agent Controller is not running."
then
acc_status="2"

fi
fi

echo $acc_status

Code:
[apm@ccbapprts1 enterprisemanager]$ ${ACC_HOME}/apmccctrl.sh status
CA APM Command Center Agent Controller is running: PID:23414, Wrapper:STARTED, Java:STARTED
[apm@ccbapprts1 enterprisemanager]$ ./epagent_enterprise_manager_metrics.sh
1
[apm@ccbapprts1 enterprisemanager]$ ${ACC_HOME}/apmccctrl.sh stop
Stopping CA APM Command Center Agent Controller...
Stopped CA APM Command Center Agent Controller.
[apm@ccbapprts1 enterprisemanager]$ ./epagent_enterprise_manager_metrics.sh
1
[apm@ccbapprts1 enterprisemanager]$


Last edited by RudiC; 08-15-2017 at 07:17 AM..
# 5  
Old 08-15-2017
Does the apmccctrl.sh script start up the processes in the background? Can you share it (wrapped in CODE tags, of course)

Some indenting of your code would help the clarity of the process, however I think that the first if statement is a syntax error in bash. Would it be better to use this:-
Code:
acc_status_check=$(${ACC_HOME}/apmccctrl.sh status)

if [ "$acc_status_check" = "CA APM Command Center Agent Controller is running" ]
then
   acc_status="1"
else
   /opt/ca/APMCommandCenterController/apmccctrl.sh start
   sleep 60
   acc_status_check=$(${ACC_HOME}/apmccctrl.sh)
   if [ "$acc_status_check" = "CA APM Command Center Agent Controller is not running." ]
   then
      acc_status="2"
   fi
fi

echo $acc_status

The spaces between [, the variables, the operator and the ] in an if statement is important. You were basically say "Can I assign a value to acc_status_check ?" rather that "Is the value of acc_status_check matching the given string?" which I assume is what you really want.

Now, the fun should begin because the message you get back from ${ACC_HOME} contains more than just the string you are looking for. This needs an exact match so your test will always fail. We can probably adjust for it though.

I would suggest adding the line below after each time you run ${ACC_HOME}:-
Code:
acc_status_check="${acc_status_check%%:*}"

This should trim off everything after the first colon.

Do all these adjustments help? If not, can you:-
  • share the code for apmccctrl.sh
  • paste the output from running this script with the -x flag set, e.g. bash -x epagent_enterprise_manager_metrics.sh


Thanks, in advance,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 08-16-2017
Great stuff Robin this solved and does what I needed Smilie

Thank you so much!
# 7  
Old 08-16-2017
Quote:
Originally Posted by rbatte1
... ... ...

Some indenting of your code would help the clarity of the process, however I think that the first if statement is a syntax error in bash. Would it be better to use this:-
Code:
acc_status_check=$(${ACC_HOME}/apmccctrl.sh status)

if [ "$acc_status_check" = "CA APM Command Center Agent Controller is running" ]
then
   acc_status="1"
else
   /opt/ca/APMCommandCenterController/apmccctrl.sh start
   sleep 60
   acc_status_check=$(${ACC_HOME}/apmccctrl.sh)
   if [ "$acc_status_check" = "CA APM Command Center Agent Controller is not running." ]
   then
      acc_status="2"
   fi
fi

echo $acc_status

The spaces between [, the variables, the operator and the ] in an if statement is important. You were basically say "Can I assign a value to acc_status_check ?" rather that "Is the value of acc_status_check matching the given string?" which I assume is what you really want
... ... ...

Thanks, in advance,
Robin
Hi Robin,
You are absolutely correct in noting that the if statement:
Code:
 if acc_command="CA APM Command Center Agent Controller is running: PID:20583, Wrapper:STARTED, Java:STARTED"

does not test whether or not the expansion of the variable acc_command yields the string between the double-quotes, but it is not a bash syntax error. That if statement will execute the commands in the then clause if the assignment statement:
Code:
acc_command="CA APM Command Center Agent Controller is running: PID:20583, Wrapper:STARTED, Java:STARTED"

completes successfully (which it will better than 99.44% of the time) or will execute the commands in the else clause otherwise. In other words it tests whether or not the assignment statement completed successfully instead of comparing two strings and testing whether those two strings are identical.

It is unfortunate that this isn't a syntax error because it can give newbies the impression that it was doing what they wanted with printing any diagnostics. But, the shell command language grammar is very simple and allows the exit status of any command to be used as the condition in an if statement. The fact that a valid assignment statement sort of looks like an improperly written test command just points out how important spacing, punctuation, and the choice of operators are when writing shell scripts. Smilie

Cheers,
Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Process Scheduling where to start

Hello, i'm absolutely new to the whole Operating Systems thing. I am pretty much level 0. My assignment is to "simulate the execution of a stream of processes by a computer system, one CPU, many terminals 12 disk drives, 30 public mailboxes. The professor runs a series of inputs which is a... (1 Reply)
Discussion started by: JaneSkylar
1 Replies

2. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

3. Shell Programming and Scripting

Solaris+Perl script to get process start date

Hi all, after reading the post: * https://www.unix.com/solaris/101653-how-get-process-start-date-time-solaris.html I wrote my perl script and it worked like a charm. This script is called every 5 minutes by the monitoring server crontab and is executed on the remote network elements via ssh (the... (6 Replies)
Discussion started by: Evan
6 Replies

4. UNIX for Advanced & Expert Users

how to start a process killable by all

Hi, Is there a way to start a process that any other user would have the privs to kill? Thanks. (1 Reply)
Discussion started by: rebelbuttmunch
1 Replies

5. UNIX for Dummies Questions & Answers

cant start httpd process

httpd status is stopped.cant start it again by : /etc/init.d/httpd restart or /etc/init.d/httpd/start help needed (2 Replies)
Discussion started by: raksha.s
2 Replies

6. Shell Programming and Scripting

Ksh Script to get the start minute of n number of process

How can i write a script.? which lists all X process and gets the start minute of each of them. thanks (1 Reply)
Discussion started by: Anteus
1 Replies

7. Shell Programming and Scripting

Script - How to automatically start another process when the previous process ends?

Hi all, I'm doing automation task for my team and I just started to learn unix scripting so please shed some light on how to do this: 1) I have 2 sets of datafiles - datafile A and B. These datafiles must be loaded subsequently and cannot be loaded concurrently. 2) So I loaded datafile A... (10 Replies)
Discussion started by: luna_soleil
10 Replies

8. Shell Programming and Scripting

How to start a process.. from a different host ...

Hi ! I want to start and stop a process... on different machines(HOSTS) example : I have machine1..machine2..machine3 And I have a NFS file system. (Wlsuite/myfile/) I'm writing a script that will start processes in machine1.... machine2.... Preferably.. I dont want to log... (1 Reply)
Discussion started by: dashok.83
1 Replies

9. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies

10. Programming

get process start time

Hi all, I like to know how can I get currenlty running process start time and date , I know only porcess id in solaris and hp-ux and what is command to get same using ps with switch. Thanks Naeem (1 Reply)
Discussion started by: naeem ahmad
1 Replies
Login or Register to Ask a Question