is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...
What is a term for a function that when called repeatedly, has the same effect as calling once?
How can I highlight parts in a screenshot
Would the melodic leap of the opening phrase of Mozart's K545 be considered dissonant?
How to disable or uninstall iTunes under High Sierra without disabling SIP
Sometimes a banana is just a banana
Did Amazon pay $0 in taxes last year?
Are small insurances worth it
I can't die. Who am I?
Reason why dimensional travelling would be restricted
How can I handle a player who pre-plans arguments about my rulings on RAW?
Relationship between the symmetry number of a molecule as used in rotational spectroscopy and point group
Inconsistent behaviour between dict.values() and dict.keys() equality in Python 3.x and Python 2.7
Why would the IRS ask for birth certificates or even audit a small tax return?
What is the meaning of "notice to quit at once" and "Lotty points”
is 'sed' thread safe
1970s scifi/horror novel where protagonist is used by a crablike creature to feed its larvae, goes mad, and is defeated by retraumatising him
I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?
Why are special aircraft used for the carriers in the United States Navy?
Create chunks from an array
Difference between 'stomach' and 'uterus'
Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
If there are any 3nion, 5nion, 7nion, 9nion, 10nion, etc.
Redirecting CellPrint output
is 'sed' thread safe
What should someone know about using Python scripts in the shell?Nexenta bash script uses /usr/sun/bin/sed instead of /usr/bin/sedLeft and right square brackets treated differently by sed/bashsystemd daemon & python getting the wrong timesed multiple statements within a single command not workingHow to use sed to substitute strings which has “” in it?Python process can't create a file in a directory, keeps getting `permission denied` IOErrorIs a light weight process attached to a kernel thread in Linux?Extract every 2 lines from a 40 lines file and create a new fileRHEL upload / pull scripts from people without access to the server itseld
If I have a shell/python script that uses sed to modify a file in place based on user inputs, and then two users run the same script at the same time or approx. same time, is 'sed' thread safe ? Or perhaps it is not an issue because the file_descripor that was opened by the first thread will be used to lock the file anyway ? thx
linux sed python
add a comment |
If I have a shell/python script that uses sed to modify a file in place based on user inputs, and then two users run the same script at the same time or approx. same time, is 'sed' thread safe ? Or perhaps it is not an issue because the file_descripor that was opened by the first thread will be used to lock the file anyway ? thx
linux sed python
2
Slightly more info wanted. How do you usesed
from Python (and why, can't Python do things like that fairly effortlessly?).
– Kusalananda
4 hours ago
add a comment |
If I have a shell/python script that uses sed to modify a file in place based on user inputs, and then two users run the same script at the same time or approx. same time, is 'sed' thread safe ? Or perhaps it is not an issue because the file_descripor that was opened by the first thread will be used to lock the file anyway ? thx
linux sed python
If I have a shell/python script that uses sed to modify a file in place based on user inputs, and then two users run the same script at the same time or approx. same time, is 'sed' thread safe ? Or perhaps it is not an issue because the file_descripor that was opened by the first thread will be used to lock the file anyway ? thx
linux sed python
linux sed python
edited 4 hours ago
Jeff Schaller
42.9k1159137
42.9k1159137
asked 4 hours ago
terreysterreys
1613
1613
2
Slightly more info wanted. How do you usesed
from Python (and why, can't Python do things like that fairly effortlessly?).
– Kusalananda
4 hours ago
add a comment |
2
Slightly more info wanted. How do you usesed
from Python (and why, can't Python do things like that fairly effortlessly?).
– Kusalananda
4 hours ago
2
2
Slightly more info wanted. How do you use
sed
from Python (and why, can't Python do things like that fairly effortlessly?).– Kusalananda
4 hours ago
Slightly more info wanted. How do you use
sed
from Python (and why, can't Python do things like that fairly effortlessly?).– Kusalananda
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
I'm not going to nitpick on the awful terminology, but yes, GNU sed with its -i
("in-place") flag could be safely used by more than one process at the same time without any extra locking, because sed
is not actually modifying the file in-place, but it's redirecting the output to a temporary file, and if everything goes well, it will rename(2)
(move) the temporary file to the original file, and the rename(2)
is guaranteed to be atomic:
$ strace sed -i s/o/e/g foo.txt
open("foo.txt", O_RDONLY) = 3
...
open("./sedDe80VL", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
...
read(3, "foon", 4096) = 4
...
write(4, "feen", 4) = 4
read(3, "", 4096) = 0
...
close(3) = 0
close(4) = 0
rename("./sedDe80VL", "foo.txt") = 0
At any point, foo.txt
will refer either to the complete original file or to the complete processed file, never to something in between the two.
1
But what can happen is that two copies ofsed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was anothersed
working on the file at the same time. The changes made by one of thesed
processes would be lost.
– ilkkachu
4 hours ago
2
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
4
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the secondsed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.
– ilkkachu
3 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504810%2fis-sed-thread-safe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not going to nitpick on the awful terminology, but yes, GNU sed with its -i
("in-place") flag could be safely used by more than one process at the same time without any extra locking, because sed
is not actually modifying the file in-place, but it's redirecting the output to a temporary file, and if everything goes well, it will rename(2)
(move) the temporary file to the original file, and the rename(2)
is guaranteed to be atomic:
$ strace sed -i s/o/e/g foo.txt
open("foo.txt", O_RDONLY) = 3
...
open("./sedDe80VL", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
...
read(3, "foon", 4096) = 4
...
write(4, "feen", 4) = 4
read(3, "", 4096) = 0
...
close(3) = 0
close(4) = 0
rename("./sedDe80VL", "foo.txt") = 0
At any point, foo.txt
will refer either to the complete original file or to the complete processed file, never to something in between the two.
1
But what can happen is that two copies ofsed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was anothersed
working on the file at the same time. The changes made by one of thesed
processes would be lost.
– ilkkachu
4 hours ago
2
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
4
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the secondsed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.
– ilkkachu
3 hours ago
add a comment |
I'm not going to nitpick on the awful terminology, but yes, GNU sed with its -i
("in-place") flag could be safely used by more than one process at the same time without any extra locking, because sed
is not actually modifying the file in-place, but it's redirecting the output to a temporary file, and if everything goes well, it will rename(2)
(move) the temporary file to the original file, and the rename(2)
is guaranteed to be atomic:
$ strace sed -i s/o/e/g foo.txt
open("foo.txt", O_RDONLY) = 3
...
open("./sedDe80VL", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
...
read(3, "foon", 4096) = 4
...
write(4, "feen", 4) = 4
read(3, "", 4096) = 0
...
close(3) = 0
close(4) = 0
rename("./sedDe80VL", "foo.txt") = 0
At any point, foo.txt
will refer either to the complete original file or to the complete processed file, never to something in between the two.
1
But what can happen is that two copies ofsed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was anothersed
working on the file at the same time. The changes made by one of thesed
processes would be lost.
– ilkkachu
4 hours ago
2
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
4
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the secondsed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.
– ilkkachu
3 hours ago
add a comment |
I'm not going to nitpick on the awful terminology, but yes, GNU sed with its -i
("in-place") flag could be safely used by more than one process at the same time without any extra locking, because sed
is not actually modifying the file in-place, but it's redirecting the output to a temporary file, and if everything goes well, it will rename(2)
(move) the temporary file to the original file, and the rename(2)
is guaranteed to be atomic:
$ strace sed -i s/o/e/g foo.txt
open("foo.txt", O_RDONLY) = 3
...
open("./sedDe80VL", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
...
read(3, "foon", 4096) = 4
...
write(4, "feen", 4) = 4
read(3, "", 4096) = 0
...
close(3) = 0
close(4) = 0
rename("./sedDe80VL", "foo.txt") = 0
At any point, foo.txt
will refer either to the complete original file or to the complete processed file, never to something in between the two.
I'm not going to nitpick on the awful terminology, but yes, GNU sed with its -i
("in-place") flag could be safely used by more than one process at the same time without any extra locking, because sed
is not actually modifying the file in-place, but it's redirecting the output to a temporary file, and if everything goes well, it will rename(2)
(move) the temporary file to the original file, and the rename(2)
is guaranteed to be atomic:
$ strace sed -i s/o/e/g foo.txt
open("foo.txt", O_RDONLY) = 3
...
open("./sedDe80VL", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
...
read(3, "foon", 4096) = 4
...
write(4, "feen", 4) = 4
read(3, "", 4096) = 0
...
close(3) = 0
close(4) = 0
rename("./sedDe80VL", "foo.txt") = 0
At any point, foo.txt
will refer either to the complete original file or to the complete processed file, never to something in between the two.
edited 4 hours ago
answered 4 hours ago
mosvymosvy
7,7821530
7,7821530
1
But what can happen is that two copies ofsed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was anothersed
working on the file at the same time. The changes made by one of thesed
processes would be lost.
– ilkkachu
4 hours ago
2
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
4
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the secondsed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.
– ilkkachu
3 hours ago
add a comment |
1
But what can happen is that two copies ofsed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was anothersed
working on the file at the same time. The changes made by one of thesed
processes would be lost.
– ilkkachu
4 hours ago
2
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
4
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the secondsed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.
– ilkkachu
3 hours ago
1
1
But what can happen is that two copies of
sed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was another sed
working on the file at the same time. The changes made by one of the sed
processes would be lost.– ilkkachu
4 hours ago
But what can happen is that two copies of
sed
start reading the file, make different changes, storing them to their respective temporary files, which are then renamed into place one after other, without regard for the fact that there was another sed
working on the file at the same time. The changes made by one of the sed
processes would be lost.– ilkkachu
4 hours ago
2
2
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
@ilkkachu that will still be completely consistent -- the file will be either modified by both processes in turn (in any order) or by just one of them. At no point will the file contain garbage resulting from both processes modifying it at the same time.
– mosvy
3 hours ago
4
4
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the second
sed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.– ilkkachu
3 hours ago
it won't be total garbage, but having changes lost can still be an issue. The question refers to the file "being locked", and locking would usually refer to the second
sed
process waiting until the first completed. That might just be awful terminology on their part; it's hard to say what they really care about, but that particular issue is still possible.– ilkkachu
3 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504810%2fis-sed-thread-safe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Slightly more info wanted. How do you use
sed
from Python (and why, can't Python do things like that fairly effortlessly?).– Kusalananda
4 hours ago