How to interrupt running application?

Hello

i have problem passing ctrl+C into shell script

this script will run application and return value but never ends and stay running.

i tried trap and not working at all

the command should look like this :

sarm pcsp  -s CHG_M_P1

i want something to stop this command after output !

can you help

You mean to say you want to kill it after the output within the script. or from the console.

You can try to find the

PID

of this process and use

kill

command after you get your output.

in shell scripting another command/line will not run until end of the first command
so when put this line

sarm pcsp  -s CHG_M_P1

it will never ends,
kill, trap , signulhup nothing will works because this command line not end

do this exactly from your command line:

ps -ef | grep 'sarm pcsp'

This will give you something that looks like this

  mogabr 25675  9253   0   Dec 14 ?           0:00 sarm pcsp  -s CHG_M_P1
  mogabr   1234  7765  0   Dec 14 ?            0:00 grep sarm pcsp

See the red number? That is the pid of the process. The actual number you see will be different. So get that number:

kill  [number you found goes here]

That will stop the running application. If does not (because you have a trap stement in the code) then try:

kill -9 [number you found goes here]

kill -9 is NOT a great idea, it may leave things like files in an undefined state.

You can also use

kill -9 pid

well

i think i cant explain in details

my question is
how to pass CTRL+C to run automatic in shell scripting as batch mode

---------- Post updated at 07:46 AM ---------- Previous update was at 07:38 AM ----------

yes this works good if manual on unix

how to do that in shell scriptting as following

1-run command XXX which will not end
2-kill command XXX

u know that shell scripting is a procedural language means every line should be executed and ended so next line can run
in this case i can never go to kill command because the previous line XXX command never ends

Yes I do understand scripting....

What you are asking does not make sense.

Assume you are a shell script, executing. How can you know if you are stuck?
You can only guess. Code for how long you are willing to wait then force an exit.
3600 = number of seconds in one hour

pid=$$
# kill the parent after 3600 seconds
sleep 3600 && kill $pid 2>/dev/null &
child_pid=$!
# [command that runs too long goes here]
sarm pcsp -s 
# if  sarm completed in less than 3600 seconds you get here
kill $child_pid   # stop the child from killing you.

Ctrl+C is equivalent to kill -SIGINT. man kill may tell you what what signal number sigint is on your system. On Debian linux for example its number 2.
In a shellscript you could start the problem process in the background and then kill it after you have your output.

# start the process in the background
sarm pcsp  -s CHG_M_P1 &
# store the PID of this process
SARMPID=$!
# here you have to implement something to find out if the process is ready to be killed
# then kill the process
kill -2 $SARMPID

i have tried this
and it works great
except after the process terminated ,, its come alive again and run :slight_smile:
there is something missing

---------- Post updated at 08:36 AM ---------- Previous update was at 08:12 AM ----------

yes
it works great now
only with kill -9

thanksssssssssss a lot

That is a useless use of kill -9