0

I am trying to run a bash script through the process class and do something when I use process.kil() But it seems that the exit signal isn't triggered so:

A. Is it possible?

B. If it isn't possible is there a way to send a signal to the process?

Script.sh

#!/bin/bash
Exit(){
echo "terminated">>log.txt
kill $child}
trap Exit EXIT
 daemon &
child=$!
echo $child > log.txt
wait $child

C# code:

Process script = new Process()
script.StartInfo.Filename = "Script.sh"
script.Start()
//Other stuff
script.Kill()

C# script.Kill doesn't seem to trigger Exit() in Script.sh

Exiting through ubuntu's system monitor does trigger it though.

Edit:

Changing the signal to SIGTERM didn't change the results.

TylerH
  • 20,799
  • 66
  • 75
  • 101
shaiel cohen
  • 41
  • 1
  • 9

1 Answers1

0

There is no such signal EXIT in UNIX/Linux. SIGTERM is the closest to what you want and the default signal used by kill.

However, bash has a pseudo-signal called EXIT. It's described in Bash Manual - Bourne Shell Builtins - trap which is where you might have gotten confused.

If a sigspec is 0 or EXIT, arg is executed when the shell exits. If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function. Refer to the description of the extdebug option to the shopt builtin (see The Shopt Builtin) for details of its effect on the DEBUG trap. If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the . or source builtins finishes executing

If you meant to use this, then you should start your script with the line:

#!/bin/bash

to ensure it is run under the bash shell.

See http://man7.org/linux/man-pages/man7/signal.7.html for a list of all the signals in Linux.

tk421
  • 5,775
  • 6
  • 23
  • 34
  • It does include the #!/bin/bash that was a copy mistake – shaiel cohen Jun 26 '18 at 05:32
  • I'm guessing this is a C# issue rather than an Ubuntu issue. I tested your script locally and both ways work. Maybe https://stackoverflow.com/questions/13952635/what-are-the-differences-between-kill-process-and-close-process might help. – tk421 Jun 26 '18 at 22:50