Soft kill a process to redirect the last kbytes output to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Soft kill a process to redirect the last kbytes output to a file
# 1  
Old 08-19-2015
Question Soft kill a process to redirect the last kbytes output to a file

Hey guys,

I have a python script that I call with this line:
Code:
python mypythonscript.py >> results.csv &

The problem is that the redirection from the stdout to the file results.csv only writes 4096 kbyte blocks.

So if i kill this process with
Code:
kill [pidOfTheScript]

the last kbytes that the script produce will be missing.

Is it possible to soft kill a process so that the redirection write the last kbytes that the script produce?

Thanks Smilie
# 2  
Old 08-19-2015
You have to trap the signal and flush the output file descriptor.

I do not know python signal handling, but you should trap SIGTERM. This is the signal kill sends by default. A generic trap like you want calls a function to clean up and complete I/O and close files (which should flush anyway). Then proceeds to exit the process.

Found some info on signal function in python.

https://docs.python.org/2/library/signal.html
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 08-19-2015
thank you jim mcnamar,

but the problem is not the python script. it is ok to hard kill the python script. The Problem is the redirection to the file. It looks like as it collects some output of the python script. Only if it collects 4096 kbytes, it will redirect exact this 4096 kbytes.

So the last kbytes are always lost.
# 4  
Old 08-19-2015
Quote:
Originally Posted by Mastaer
thank you jim mcnamar,

but the problem is not the python script. it is ok to hard kill the python script.
It doesn't hurt the script or your system to hard-kill it, no -- but it certainly doesn't do what you want.

Python does single writes every 4096 bytes because this more efficient than doing 50 tinier writes. Python streams do this on the assumption the stream will end naturally, not get hard-killed while waiting for the 4096th byte. This is not a shell thing, a redirection thing, or any kind of system buffer -- the buffer is in Python, part of Python's code, controlled by Python, and must be configured in Python to disable Python's write buffer.

This is not the same as system disk cache, that's transparent, you'd see a correct result if the system knew it was supposed to be there.

This is not the same as the buffers used for pipes. Those don't apply when not using pipes.

You cannot force Python or any other program to not buffer from the outside, unless there's some mysterious NEVER_BUFFER environment variable Python responds to or something.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-19-2015
Power

ohhh... okay i get it. thank you very much Smilie

I found a other solution:
Code:
stdbuf -oL

But you are both right! I should change it in python. Thank you very much
# 6  
Old 08-19-2015
There is a mysterious variable Python responds to, actually!
Code:
export PYTHONUNBUFFERED=1

./myscript >> file &

I don't think stdbuf is applicable unless you're writing to a pipe or TTY. Block or line buffering is not a system feature for file streams, Python must be doing that by itself.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-19-2015
this works well. Thanks Corona688 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to kill the process when the file is locked?

I was trying to read the file to create a table in SAS and I got error as follows while I read. Resource is write-locked by another user. File =/usr/sas/sas_config/Lev1/SASApp/StoredProcessServ​er/Logs/SASApp_STPServer_2015-09-29_tmp_18208.log. System Error Code = 0. ERROR: File is in... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

2. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

3. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

4. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. Shell Programming and Scripting

kill process from a file or directly with top

i have edited a script to kill an exact mysql process is causing the high load on the server, my problem is, kill dont kill it! script: #!/bin/sh top -n 1 -u mysql | grep mysqld | awk '{print $1}' > pid proc='cat pid' kill -9 $proc or i try with kill -9 `top -n 1 -u mysql | grep mysqld... (8 Replies)
Discussion started by: chandro
8 Replies

6. Programming

Redirect Standard Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); ... (5 Replies)
Discussion started by: djodjo
5 Replies

7. Shell Programming and Scripting

Redirect bg process output to within the script

Hi, I have a process running in the background, which throws up some output to the terminal when I run my script. How can I read this output from my script? Thank you. (5 Replies)
Discussion started by: Theju
5 Replies

8. Programming

Redirect Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); printf("Hello \n");... (3 Replies)
Discussion started by: djodjo
3 Replies

9. Shell Programming and Scripting

how to kill process from file

i have a script that read a file which contains process_id and time that he's in and it lookes like this 0:30 54545 0:44 66788 0:90 23233 i need to read every line in the file and get the time and if the process is greater then 0:30 to kill the process id the script looks like... (17 Replies)
Discussion started by: naamas03
17 Replies

10. Post Here to Contact Site Administrators and Moderators

how to kill process from file

hello i want to write a script in unix which read file that contains : hour and process_id like this file bbb: 0:30 6678 1:40 8789 and for every line he check if the hour greater then 30 minutes he kill the process_id the script looks like this cat bbb | while read line do ... (1 Reply)
Discussion started by: naamas03
1 Replies
Login or Register to Ask a Question