Visit Our UNIX and Linux User Community


Is appending to file with >> interruptable?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Is appending to file with >> interruptable?
# 1  
Old 10-11-2001
Is appending to file with >> interruptable?

I have a script1.ksh that calls another script2.ksh and redirects and appends script2.ksh's output to a file:

#script1.ksh
...
script2.ksh >> outfile
...

#script2.ksh
...
echo value1 value2 value3 ...valuen
....

Suppose I have many script1.ksh processes running at the same
time. Is it possible that file "outfile" can be corrupted if multiple
scripts/processes are writing to it at the same time?

If not corrupted, is it possible that the values written by echo
are interrupted such that the output values are interleaved with
values from the other processes?

Perhaps a simpler way of asking this question is whether the
redirection operators > and >> are atomic (ie non-interruptable)?

Thanks again,
ragtopgeek
PS - Sorry about the verbosity of my question, but you can thank
the public schools for that.
# 2  
Old 10-11-2001
1) dont blame schools for your explanation skills
2) yes its interuptable
3) i have not tested it but if you have 2 parrallel processes dumping to a file they should each dump as soon as they get a chance. so yes the data may be interweaved.
# 3  
Old 10-11-2001
Thanks for the info Optimus_P, but I want to make sure I understand completely. Are you saying that the output from
the same echo command can be interleaved with the output
from another process' same echo command? For example:
#script1.ksh
...
script2.ksh >> outfile
...

# script2.ksh
...
# echo my pid five times
echo $$ $$ $$ $$ $$
...

If one process has pid 1111 and another has 9999, then is it
possible that instead of output file having:
1111 1111 1111 1111
9999 9999 9999 9999
that it may have
1111 1111 9999 1111 9999 9999 9999 1111 9999 1111
(not sure how the new lines would work).

So a specific echo command can be interrupted?

Thanks again,
ragtopgeek
# 4  
Old 10-11-2001
i have not tested it but i do beleave so.
# 5  
Old 10-11-2001
Remember that programs use system calls to get stuff done. So you gotta look at this from the system call level.

If two processes both have the same file opened in append mode are both are writing to it, each write system call will be atomic. The data from two writes will never be inter-mingled.

But
echo $$ $$ $$ $$
will probably be 4 writes, one for each arg. If so, they could get inter-mingled as you show. On the other hand:
echo "$$ $$ $$ $$"
which sends one argument to echo, has a better shot of getting a single write. But it all really depends on how echo was written.

It is a common technique for many processes to open an error file in append mode, then write a line to it with a single write system call.
# 6  
Old 10-11-2001
Thanks! I was reading Steven's "Unix System Programming..."
book last night to get some insight. You are right in that it
depends on how the individual output commands are
implemented with regard to using system i/o functions.

I am playing around with trying to create/get interleaved
results. If I have any luck, I'll post.
# 7  
Old 10-12-2001
I acheived perfect interleaving with a small script and a wrapper script.
Wrapper
Code:
#!/bin/bash
/tmp/prog one &
/tmp/prog two &

Script /tmp/prog
Code:
#!/bin/bash
A=1
until [ $A -eq 1000 ]
do
echo $1 >> /tmp/testfile
A=`expr $A + 1`
done

I am sure there is a better way to incorporate this into one script.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #337
Difficulty: Easy
:set number in vi will add line numbers to the file and automatically save to disk.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending content of a file to another file before a specific character

Hi there, i've got a file with this content $ cat file1 Matt Mar The other file has the same number of lines with this content: $ cat file2 20404=767294 23450=32427 is there a way with either using sed, awk or paste to insert the content of file1 before the "=" character? So... (3 Replies)
Discussion started by: nms
3 Replies

2. Red Hat

Process with S state(Interruptable) in RHEL and gives Advertise error after restarting/Killing the p

Hello, In our Production system one process is in S state(interruptible)and after killing and restarting the process gives 'advertise error'. This error goes after rebooting the Server. I have RHEL 5.9 (tikanga) OS in our server. We tried debugging the issue with the help of 'strace' command... (9 Replies)
Discussion started by: Rohits
9 Replies

3. Shell Programming and Scripting

Appending to file with new ID

I have a file that looks like this: 1|A 2|B 3|C ... ... 100|A I would like to take the last line in the file and add +1 to the number so the output looks like this 1|A (4 Replies)
Discussion started by: BeefStu
4 Replies

4. Shell Programming and Scripting

Removing part of a file name and appending into a single file

I have two files like ABC_DEF_yyyyymmdd_hhmiss_XXX.txt and ABC_DEF_yyyyymmdd_hhmiss_YYY.txt. The date part is going to be changing everytime. How do i remove this date part of the file and create a single file like ABC_DEF_XXX.txt. (8 Replies)
Discussion started by: varlax
8 Replies

5. Shell Programming and Scripting

Appending number of lines in the file to begining of the file

I am having 6 files named file1,file2....file6 and i need to append number of lines in each file to begining of the file. For example, If file 1 contains a b c d then after adding new line file1 should contain 4 a b c d Thanks in advance. (2 Replies)
Discussion started by: akhay_ms
2 Replies

6. Shell Programming and Scripting

appending the count of line in each file at head of each file

hello everybody, I have some files in directory.each file contain some data. my requirement is add the count of each line of file in head of each file. any advice !!!!!!!! (4 Replies)
Discussion started by: abhigrkist
4 Replies

7. Shell Programming and Scripting

searching a log file and appending to a .txt file

I'm new to shell scripting and am writing a script to help me log the free memory and hd space on a server. As of now, the script just runs 'df -h' and appends the output to a file and then runs 'top' and appends the output to a log file. What I want to do, is have the script also search the... (3 Replies)
Discussion started by: enator45
3 Replies

8. Shell Programming and Scripting

appending to sed output of one file into the middle of file

hi, i have a file file1 file2 ----------- ----------------- aa bbb ccc 111 1111 1111 ddd eee fff 222 3333 4444 ggg hhh... (5 Replies)
Discussion started by: go4desperado
5 Replies

9. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies

10. UNIX for Dummies Questions & Answers

Appending out to a file

Hi, once again - Ok here it is: a).I used the touch command to create a file (touch directory.list) b).I then added this line: - date '+The date is %a %h %d, %Y %nIt is %I:%M %p' > directory.list -So that this date function would go at the top of my script c).Now I want to display a... (1 Reply)
Discussion started by: Astudent
1 Replies

Featured Tech Videos