Skip to content
Advertisement

I want to output ” Killed ~” to logfile when it kill -9

I want to output this message /usr/local/ex1.sh: line xxx: <PID> Killed ex2.sh >> $LOG_FILE 2>&1 to logfile.

however

The “ex1.sh” output /usr/local/ex1.sh: line xxx: <PID> Killed ex2.sh >> $LOG_FILE 2>&1 to console when I executed ex1.sh in console.

The result that i want is that “ex1.sh” output to file, not that output to console.

This source is “ex1.sh”.

ex2.sh >> $LOG_FILE 2>&1 &
PID=`ps -ef | grep ex2.sh | grep -v grep | gawk '{print $2}'`
/bin/kill -9 $PID >> $LOG_FILE 2>&1 &

Why does “ex1.sh” output this message to console?

Advertisement

Answer

The output is in fact not written by the kill command or ex2.sh. It is written by the shell executing the background process ex2.sh.

The shell executing the script started the script ex2.sh in the background as a child process and is monitoring it. When the script is killed, the shell acts on this by printing the message.

In your special case the shell knows more about the killed process and the process executing kill. So it prints a rather verbose message.

If you start ex2.sh (without ‘&’) in terminal 1 and kill it from terminal 2, the shell in terminal 1 will just print “Killed”.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement