Shell program to just open a character driver and wait
What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
Echo/touch
seem to just open and close the device immediately after performing the operation. Cat
does not seem to work.
I am using a C application to do the same but was wondering if shell script has some provision for it
linux bash shell-script
add a comment |
What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
Echo/touch
seem to just open and close the device immediately after performing the operation. Cat
does not seem to work.
I am using a C application to do the same but was wondering if shell script has some provision for it
linux bash shell-script
add a comment |
What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
Echo/touch
seem to just open and close the device immediately after performing the operation. Cat
does not seem to work.
I am using a C application to do the same but was wondering if shell script has some provision for it
linux bash shell-script
What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
Echo/touch
seem to just open and close the device immediately after performing the operation. Cat
does not seem to work.
I am using a C application to do the same but was wondering if shell script has some provision for it
linux bash shell-script
linux bash shell-script
asked Dec 10 at 13:14
yashC
1557
1557
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In Bourne-like shells,
exec 3< "$device"
Opens the device on file descriptor 3 of the shell.
That would be more or less equivalent to C's:
fd = open(device, O_RDONLY);
if (fd < 0) handler_error(...);
if (fd != 3) { dup2(fd, 3); close(fd); }
(ksh93
also does a fcntl(3, F_SETFD, FD_CLOEXEC)
on that fd).
To close it: exec 3<&-
In zsh
, ksh93
and bash
, the equivalent of fd = open(device, O_RDONLY)
could also be written as:
exec {fd}< "$device"
Where the file descriptor would be the first free one above 9 and stored in $fd
.
To close it: exec {fd}<&-
Replace <
with >
for O_WRONLY|O_CREAT|O_TRUNC
, and with <>
for O_RDWR|O_CREAT
and >>
for O_WRONLY|O_CREAT|O_APPEND
.
zsh
also has a sysopen
builtin (in the zsh/system
module) where you can specify the flags exactly.
Note that in POSIX compliant shells, exec
being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command
command.
if command exec 3< "$device"; then
do-what-you-need-to-do
else
handle-the-error-yourself
fi
add a comment |
while sleep 3600; do :; done >/dev/your_watchdog
I gather (from the echo
and touch
working and the cat
failing) that the device should be open in write only mode.
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...
– Digital Trauma
Dec 10 at 20:38
sleep: invalid number '1e99'
;-)
– pizdelect
Dec 11 at 2:03
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%2f487113%2fshell-program-to-just-open-a-character-driver-and-wait%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Bourne-like shells,
exec 3< "$device"
Opens the device on file descriptor 3 of the shell.
That would be more or less equivalent to C's:
fd = open(device, O_RDONLY);
if (fd < 0) handler_error(...);
if (fd != 3) { dup2(fd, 3); close(fd); }
(ksh93
also does a fcntl(3, F_SETFD, FD_CLOEXEC)
on that fd).
To close it: exec 3<&-
In zsh
, ksh93
and bash
, the equivalent of fd = open(device, O_RDONLY)
could also be written as:
exec {fd}< "$device"
Where the file descriptor would be the first free one above 9 and stored in $fd
.
To close it: exec {fd}<&-
Replace <
with >
for O_WRONLY|O_CREAT|O_TRUNC
, and with <>
for O_RDWR|O_CREAT
and >>
for O_WRONLY|O_CREAT|O_APPEND
.
zsh
also has a sysopen
builtin (in the zsh/system
module) where you can specify the flags exactly.
Note that in POSIX compliant shells, exec
being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command
command.
if command exec 3< "$device"; then
do-what-you-need-to-do
else
handle-the-error-yourself
fi
add a comment |
In Bourne-like shells,
exec 3< "$device"
Opens the device on file descriptor 3 of the shell.
That would be more or less equivalent to C's:
fd = open(device, O_RDONLY);
if (fd < 0) handler_error(...);
if (fd != 3) { dup2(fd, 3); close(fd); }
(ksh93
also does a fcntl(3, F_SETFD, FD_CLOEXEC)
on that fd).
To close it: exec 3<&-
In zsh
, ksh93
and bash
, the equivalent of fd = open(device, O_RDONLY)
could also be written as:
exec {fd}< "$device"
Where the file descriptor would be the first free one above 9 and stored in $fd
.
To close it: exec {fd}<&-
Replace <
with >
for O_WRONLY|O_CREAT|O_TRUNC
, and with <>
for O_RDWR|O_CREAT
and >>
for O_WRONLY|O_CREAT|O_APPEND
.
zsh
also has a sysopen
builtin (in the zsh/system
module) where you can specify the flags exactly.
Note that in POSIX compliant shells, exec
being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command
command.
if command exec 3< "$device"; then
do-what-you-need-to-do
else
handle-the-error-yourself
fi
add a comment |
In Bourne-like shells,
exec 3< "$device"
Opens the device on file descriptor 3 of the shell.
That would be more or less equivalent to C's:
fd = open(device, O_RDONLY);
if (fd < 0) handler_error(...);
if (fd != 3) { dup2(fd, 3); close(fd); }
(ksh93
also does a fcntl(3, F_SETFD, FD_CLOEXEC)
on that fd).
To close it: exec 3<&-
In zsh
, ksh93
and bash
, the equivalent of fd = open(device, O_RDONLY)
could also be written as:
exec {fd}< "$device"
Where the file descriptor would be the first free one above 9 and stored in $fd
.
To close it: exec {fd}<&-
Replace <
with >
for O_WRONLY|O_CREAT|O_TRUNC
, and with <>
for O_RDWR|O_CREAT
and >>
for O_WRONLY|O_CREAT|O_APPEND
.
zsh
also has a sysopen
builtin (in the zsh/system
module) where you can specify the flags exactly.
Note that in POSIX compliant shells, exec
being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command
command.
if command exec 3< "$device"; then
do-what-you-need-to-do
else
handle-the-error-yourself
fi
In Bourne-like shells,
exec 3< "$device"
Opens the device on file descriptor 3 of the shell.
That would be more or less equivalent to C's:
fd = open(device, O_RDONLY);
if (fd < 0) handler_error(...);
if (fd != 3) { dup2(fd, 3); close(fd); }
(ksh93
also does a fcntl(3, F_SETFD, FD_CLOEXEC)
on that fd).
To close it: exec 3<&-
In zsh
, ksh93
and bash
, the equivalent of fd = open(device, O_RDONLY)
could also be written as:
exec {fd}< "$device"
Where the file descriptor would be the first free one above 9 and stored in $fd
.
To close it: exec {fd}<&-
Replace <
with >
for O_WRONLY|O_CREAT|O_TRUNC
, and with <>
for O_RDWR|O_CREAT
and >>
for O_WRONLY|O_CREAT|O_APPEND
.
zsh
also has a sysopen
builtin (in the zsh/system
module) where you can specify the flags exactly.
Note that in POSIX compliant shells, exec
being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command
command.
if command exec 3< "$device"; then
do-what-you-need-to-do
else
handle-the-error-yourself
fi
edited Dec 10 at 14:08
answered Dec 10 at 13:18
Stéphane Chazelas
299k54563913
299k54563913
add a comment |
add a comment |
while sleep 3600; do :; done >/dev/your_watchdog
I gather (from the echo
and touch
working and the cat
failing) that the device should be open in write only mode.
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...
– Digital Trauma
Dec 10 at 20:38
sleep: invalid number '1e99'
;-)
– pizdelect
Dec 11 at 2:03
add a comment |
while sleep 3600; do :; done >/dev/your_watchdog
I gather (from the echo
and touch
working and the cat
failing) that the device should be open in write only mode.
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...
– Digital Trauma
Dec 10 at 20:38
sleep: invalid number '1e99'
;-)
– pizdelect
Dec 11 at 2:03
add a comment |
while sleep 3600; do :; done >/dev/your_watchdog
I gather (from the echo
and touch
working and the cat
failing) that the device should be open in write only mode.
while sleep 3600; do :; done >/dev/your_watchdog
I gather (from the echo
and touch
working and the cat
failing) that the device should be open in write only mode.
answered Dec 10 at 14:45
pizdelect
31116
31116
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...
– Digital Trauma
Dec 10 at 20:38
sleep: invalid number '1e99'
;-)
– pizdelect
Dec 11 at 2:03
add a comment |
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...
– Digital Trauma
Dec 10 at 20:38
sleep: invalid number '1e99'
;-)
– pizdelect
Dec 11 at 2:03
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...– Digital Trauma
Dec 10 at 20:38
sleep 1e99 >/dev/your_watchdog
would also work, assuming you're not planning on being after 10 to the 91 years or so...– Digital Trauma
Dec 10 at 20:38
sleep: invalid number '1e99'
;-)– pizdelect
Dec 11 at 2:03
sleep: invalid number '1e99'
;-)– pizdelect
Dec 11 at 2:03
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f487113%2fshell-program-to-just-open-a-character-driver-and-wait%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